2

I recently got into touch-typing as I thought it might help me in my work and increase my productivity (I am currently a programmer, sometimes editing text files). Oftentimes I find myself switching from vim to terminal to run a command or open a different file.

I was wondering, how do you guys get around having to move your hands all over the keyboard when pressing arrow keys to get commands in terminal from history (what I usually do is edit some file with vim, compile, run, if it does not work I go back into vim editing, then save/close and arrow key up to get the compile and run commands from history). The only problem with this is I cannot keep my hands on the home row (obviously because I have to move to arrow keys).

How do touch-typists get around this? Is there some trick I might be missing?

P.S.: If there is any tool that might allow me to never leave vim, or other tools that you think might help me, please let me know.

P.S.S.: The shell I'm currently using is zsh.

Alex Gh
  • 23
  • 2

3 Answers3

4

Shortcuts from shell

you don't "have to" use the arrow keys to use the history in shells like zsh.

  • <c-p> previous history command.
  • <c-n> next history command.
  • <c-r> history search.

or you can add some improvements:

  • use fzf instead of regular history search.
  • change fzf shortcuts.
  • change zsh shortcuts if necessary (I only added <c-t> and <c-s> to move one word back and forth, because I don't like to switch between Alt and Ctrl keys, and I almost never need transpose-chars).
  • run bindkey -v in zsh to get vim-like shortcuts.

be aware that I also "cheat" a lot to make arrow keys easier to use:

  • I use a special keyboard named Ergodox EZ, it's splitted, ortholinear, ergonomic, programmable, with several keys on each thumb. And with this keyboard, I can easily press <Up>, <Down>, <Esc>, Super (window key) and e with my left thumb, <Left>, <Right>, <Return>, Ctrl and obviously <Space> with the right thumb, so I don't really have problems with using arrow keys a few times.
  • I have my own keyboard layout, with 8 levels instead of 4 (I can press Shift, Altgr and the right control key to access them), and I have some other arrow keys on level5. I can also lock (with ISO_Level5_Lock) this level if I need to press arrow keys only for a few minutes.
  • I use TMUX, with shortcuts to go to another pane or to redo the last command on the next pane while I stay where I am.
  • With vim, :! can be used to do a shell command.
  • With neovim, there is a terminal mode.
ewen-goisot
  • 251
  • 1
  • 8
2

To add to the existing answer:

For shell specifically:

  • I use vi keys in bash (set -o vi) and zsh (bindkey -v with some extra goodies), so I don’t touch arrows that much (Escape + jk works)
  • Also, the Ctrl-r reverse incremental search is good
  • Heavy use of tmux, with more vi-like bindings

For vim:

  • Easy mapping to run :suspend
  • Easy mapping to run :terminal (actually, for some languages it starts a repl, based on my config, but it defaults to a shell otherwise; see vim-simpl)
  • With tmux, I can easily open a new shell anywhere that’s not tied to vim, so I have lots of options.

So the short answer is—I certainly leave vim (it’s not an Emacs that you have to be in 24/7), but I prefer to make everything else feel a bit like vim (because my fingers live for vim keys now).

D. Ben Knoble
  • 26,070
  • 3
  • 29
  • 65
0

Another option is to use full Vim power for terminal commands.

Just enter:

q: 

and split pane opens where one can enter terminal commands prepending them with ! (press enter to execute). It's Vim, so navigation, editing, searching etc working as expected (this is actually history of : commands)

I mainly use Python (no compilation needed) so I tend to have this workflow:

  • write code in .py file

  • run current file using

    :!clear && python3 %
    
  • observe result

  • hit return and be back in current file

Next time I need to run file I enter:

q:kk<enter>

where kk denotes navigation a couple of rows up in history depending how many : commands have been issued between different runs of file (to reach the command I gave earlier to run file).

If I am sure that there has been none then I would do repeat last command with:

:!!

Making adjustments/editing the commands in history is very easy as this is 'full' Vim. Fore example search for command in history with /, navigate with n between matches, make edits in normal mode or insert additional text in insert mode on suitable row and press enter.

As for terminal, additionally to suggested vi keybindings I have added visual clues to distinguish between different modes.

On MacOS I added these rows to my .bashrc:

# set vi navigation and jj as switch to normal
set -o vi
bind '"jj":vi-movement-mode'

And to display + (insert mode) or : (normal mode) at the beginning of the prompt I added following to .inputrc:

set editing-mode vi
set show-mode-in-prompt on
set vi-ins-mode-string \1\e[6 q\2 +
set vi-cmd-mode-string \1\e[2 q\2 :

My prompt is very minimalistic, one of below, depending of the mode I am in:

: >
+ >
guntbert
  • 1,245
  • 1
  • 13
  • 27