Quantcast
Viewing all articles
Browse latest Browse all 5

Vim demystified: ten commands you can start using today

These commands are not only extremely useful but also mnemonic and easy to learn for a huge payoff in keystroke savings. These are the common compound commands I use every day, and I think it’s easier to dive in this way than to try to learn vim key by key.

Read Me First! Save your wrists, stop using ESC in vim
yaw yank around word – copy the current word, no matter where your cursor is inside the word
ci” change inside quote (or any other enclosure). Example: ci( to change something surrounded by parentheses. But what’s really cool is that you can invoke this from anywhere inside a line of code, so even if the string you want to change is far away, typing ci” will put your right inside the string, ready to change it.
ctX change till X (any character); and in general the use of tX where X is any character, to move to that character. You can combine the motion with another command such as dtX (delete till X), ytX (yank till X), or vtX (visually select till X).
gf

go to file – open the file under the cursor (useful for traversing file references inside codebases)

* (Shift-8) Takes you to the next occurrance of the word you’re currently looking at (often the best way to navigate to the definition of a method, if witin the same file)
Ctrl-6 Jumps you back to the previous file you were editing. I use this so often I remapped it to just the capital letter Z:
nnoremap <silent> Z <C-^>
Ctrl-]

Again, immensly useful for traversing codebases, this command relies on exuberant ctags (set up ctags) to take you to method and class definitions. I remap this to simply leader capital F:

nnoremap <silent> F <C-]>

I believe all highly used commands should be a single letter, as close as possible to home row.

mX
‘X

You can bookmark a file very quickly by using m plus any capital letter. Then to go back to the file at any time, hit and the same letter again.

The commands in this section are based on custom aliases. Please check that the alias doesn’t overload a key you’re used to.
K For super fast code navigation, nothing beats the git grep plugin combined with an alias to grep the Kurrent word under the cursor:

nnoremap <silent> K :GitGrep <cword><CR>

vv
ss
Splitting windows is a common and every day task, so you shouldn’t spend extra time typing to do so. Just double tap vv or ss:
nnoremap <silent> vv <C-w>v
nnoremap <silent> ss <C-w>s
Window navigation using J,K,I,M Bonus: A very common task for which the default key bindings involve way too many keystrokes. Use directional style keys to move between windows:
nnoremap <silent> H <C-w>h
nnoremap <silent> L <C-w>l
nnoremap <silent> I <C-w>k
nnoremap <silent> M <C-w>j

If you liked this post, you might want to watch my dotfiles repo on github. My dotfiles follow a keystroke minimizing principle where all common tools are only a few characters long, and are in the process of being cleaned up in order to release as a product targeting keystroke saving enthusiasts (this is especially important for those concerned with RSI prevention).

The post Vim demystified: ten commands you can start using today appeared first on Yan Pritzker.


Viewing all articles
Browse latest Browse all 5

Trending Articles