VimTip: Search and f(replace)

Pre-tip: When using search and replace in Vim, you don’t need to use slashes

This works just fine:


Did you know $replace doesn’t have to be a literal expression? You can also use Vim functions! For example:


will replace every occurrence of ‘bar’ for its line number. You can get creative and use any other Vimscript function.



Have you ever been working on your plans for world domination but got scared someone else might find them? It happens to me all the time. Or maybe you are so paranoid that you need to encrypt your grocery list. Perhaps you are sharing a semi-private text file through a public service? Good news, Vim has you covered. Just type “:X”. Vim will ask you for a password. Save your file again and voila, your file is now encrypted. Open the same file with Vim to decrypt it. Your plans for world domination are now safe!

Vim tip: custom commands

If you have a function that you use a lot, you may find it interesting to use a custom command for it. Try this:

:command Foo echo('Hola!')

Now invoke the command with ‘:Foo’ and Vim should say hello. Neat, huh? This is especially useful (and dangerous) when combined with cabbrev, like this:

:command! Foobar echo('Nope!')
:cabbrev close Foobar

If you try to :close a document, Vim will now say “Nope!”. Other than using this to mess with someone’s Vim session, you can replace builtin commands with your own tweaked functions. I tend to use that quite frequently in my own .vimrc.

Vim tip: Replacing builtin commands

If you spent a day writting a cool new version of the “tabnew” Vim command, you’ll probably want it to be the default command used to open new tabs. Right?

Luckily, there’s an easy way to replace built in commands with custom ones: cabbrev. cabbrev will do a textual replacement, so if you add “cabbrev tabnew TabNew” to your vimrc, eachtime you type “tabnew” it will be translated to TabNew.

Bonus tip: The command is actually, “abbrev”, not “cabbrev”. The “c” stands for command: it’s telling Vim that you want the abbrev command applied in command mode. You can also use it as “nabbrev” to have it applied in normal mode. “abbrev” it’s a nice way to correct common typos!

Vim tip: autocommands

Whenever you find yourself thinking “I wish Vim could do this automagically for me”, you probably are thinking about autocommands. With autocommands, autocmd for short or au for lazy people, will let you tell Vim, “Hey, use this callback when an event occurs”.

The basic structure is pretty simple: “autocmd Event FileType Action”. So, for example, “autocmd BufEnter *.txt call Rot13()” would tell vim to set a callback on BufEnter, that is whenever you change buffers, for all *.txt files, which will rot13 your text. Feel free to use this for actually useful things, like spell checking or auto indenting.

Vim tip: reload your vimrc

If you’re changing your vimrc, it can get boring to close and restart it only to see the changes applied. Want something quicker? You can “:so %”. So stands for source, so you’ll just be telling vim “include this file”. % happens to be the path to the current file. If you’re not editing your .vimrc but for some reason you still want to reload it, just use “so ~/.vimrc” instead.

Vim tip: “polymorphic” key bindings

If you use vim daily, you probably have a bunch of maps for your most common tasks. You should also remember you can assign the same key to do different things according to which mode you are
in. For example, let’s say you have a mapping to open a new tab:

map <leader>t :tabnew<cr>

You can also map t to open a new tab using the selected text as a filename. You just need to define two mappings:

nmap <leader>t :tabnew<cr>
vmap <leader>t :call Foo()<cr>

nmap stands for normal (mode) map, vmap for visual. How to get the text under the cursor is a bit more complex and out of scope for this vim tip, but you might want to check
Remember to check “:help map” for a list of all mode mappings.

Vim tip: Stop escaping slashes

If you do a lot of search & replace in Vim, eventually you’ll end up escaping a lot of slashes. Whenever you have to replace a path, for example. Isn’t that annoying? After a couple of levels you end up with a horrible “\/path\/to\/foo\/bar” pattern. And if you miss an escape slash, good luck. It’s better to scrap the whole thing and start over.

Luckily, when you are using the ‘s’earch command you can pick a different separator. Instead of typing “%s/\/foo\/bar\/baz\//foo\/bar\//g”, you can simply type “%s#/foo/bar/baz/#foo/bar/#g”. Vim will automagically detect you want to use ‘#’ as a delimiter, and you’ll end up with a much more readable pattern.

Extra tip: this also works in sed

Vim Tip: I want more menus!

As a uber vim geek, you shouldn’t be using a lot of gui menus. Scratch that, you shouldn’t be using any menus at all, period. Still, I’ll admit it’s a bit hard to remember every single shortcut for actions you rarely use.

Say, for example, you like to encrypt your text. Not always, but every once in a while. Enough to make a shortcut for it but not enough to remember what the shortcut is. You can try to grep your ~/.vimrc. You might find something like:

" Encrypt my stuff
map <leader>e ggg?G<CR>

(Yes, that command will actually encrypt your text in Vim. Try it!)

Wouldn’t it be nice if you had a simpler way, though?

Turns out you can add your “encrypt” command to your gui. Then “menu” commands work just like the “map” family, but they create a GUI menu instead. Change your vimrc to something like this:

" Encrypt my stuff
map <leader>e ggg?G<CR>
menu Project.Encrypt ggg?G<CR>

Now if you reload your vimrc you’ll find a new GUI menu created, from which you can easily encrypt your text. Decrypting is left as an exercise to the reader.

Extra tip: Want to try to learn the actual shortcut, like a real vim’er? Then try this:

menu Project.Encrypt<TAB>ggg?G ggg?G<CR>

Everything after the TAB will be right-aligned: you can use that space to annotate the key-combo you should use next time.

As usual, for more info check :help menu

Vim tim: quickly switch from header to impl

Switching from header to implementation in vim takes up precious milliseconds of typing and thinking, so we’d better delegate that to a computer. Instead of typing :tabnew FOO.cpp, just download A (for alternate) from this url:

Add it to your bundles in vim and, for extra magic, just map some key to :AT in your vimrc. I have added this one:

map <F4> :AT<CR>

I don’t know how I lived without this for such a long time.