Brendan Dawes

Analogue + Digital

Process

My Most Used Day-to-Day Vim Features

When you first starting using Vim as your default editor the amount of things to learn can be overwhelming. I've been using Vim for about a year now and after a while things become like muscle memory, even though I'm still learning new things Vim can do every day. There are though some things I use more than others, that have become second-place to me in my daily usage of Vim. Here's a few of them.

Insert above or below a line. Pressing o will insert a new line below your current line and put you in insert mode. Use an uppercase O to insert a line above the current line.

Auto-indent a whole file. Pressing gg=G will auto-indent your whole file. I use this a lot to sort out indenting really quickly. But how do you go back to that last place your were editing? Well...

Jump to the last place you were editing. Pressing gi will jump you back to were you were last editing and put you in insert mode. Great to use this after the previous tip.

Center the current line your editing. Pressing zz centers the current line you're on.

Search. I use this many times a day to jump quickly around a file. Pressing forward slash / when in normal mode will then let you search for a pattern in forward direction. You can then press n to move forward in that search or uppercase N to search backwards. If you want to start searching backwards then use ? instead of /.

Change inside. Probably my most used combination. Say I have "some string" and I want to change what's inside the quotes. Rather than select the words inside the string I can just type ci" which means change inside quotes and Vim selects what's inside the quotes, deletes it and then puts you into insert mode so you simply type you change. Also try yi" which will yank (copy) the selected text inside the quotes. As with all things in Vim you can use this for inside anything, not just quotes.

Toggle case. Pressing ~ in normal mode will toggle the case of the character under the cursor. Works with anything you have selected in visual mode too. To uppercase a selection rather then toggle press U or for lowercase press u.

Open file under the cursor. Pressing gf will open the file path under the cursor.

Insert at the end/beginning of a line. Pressing A will put you into insert mode at the end of the line. Pressing I will put you into insert mode at the beginning of the line.

Navigate the jump-list. Pressing CTRL-o will move you back through the jump list, whilst CTRL-i will move you forward. This is great for quickly bringing up a file you had previously been working on.

Navigating files with NETRW. When I first started using Vim I read many articles that said to use NerdTree to create a project drawer style file browser. However when you start using splits you realise that it no longer makes sense, as pointed out in this great article. Using the built-in plugin NETRW means you can instead navigate files using a much better "card flipping" metaphor. I have triggering NETRW mapped to CTRL-n in my .vimrc like this:

map <c-n> :edit .<CR>

When I press CTRL-n it will bring up the file browser in the same split as my current buffer. To flip back I just press CTRL-^. When using NETRW I can move back through directories with - and create new directories with d. I can create a new file by pressing %. I use this now as the default way to navigate around a project.

These are just a few of the things I use everyday with Vim. I've stayed away from talking about some of the plugins I use (I try not to use that many), but hopefully this might have proved useful to some, and if you haven't already it may make you want to switch to Vim, which I highly recommend.