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!
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.
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!
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.
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.
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 http://vim.wikia.com/wiki/Mapping_keys_in_Vim_-_Tutorial_%28Part_1%29.
Remember to check “:help map” for a list of all mode mappings.
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
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
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: http://www.vim.org/scripts/script.php?script_id=31
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.
I admit it, hitting control enter to jump to a definition is a very useful feature of fancy GUIs. Fortunately, it’s not exclusive to fancy GUIs, apparently it’s been available using ctags too for a little while (if you consider the last 20 or 30 years to be little, that is).
Just add this magic spell to your .vimrc; next time you open a file in a project with a tags file generated just press ctrl-enter with your cursor over the definition you wish to find:
map <C-CR> :tab split<CR>:exec("tag ".expand("<cword>"))<CR>