Remember that your vimrc is written in a full-blown programming language! You can probably find ways to refactor it: http://t.co/nGOghojSwxIt’s not just, as Alex, Mathias, and Andrew point out, that you can refactor your .vimrc (though, to be sure, you can—and probably should!). You can also niftily rework Vim, as it were, using this full-blown programming language to redirect its interface (your keyboard) in ways that save you time.
— Vim Links (@VimLinks) January 28, 2015
Here are five excellent ways to do that, which I’ve gradually come across in that unending fountain of Vim wisdom, the World-Wide Web. (Where I can recall a source, I’ve linked to it; also, I’ve sometimes tweaked such snippets for my own I’ll-have-you-to-know completely well-intentioned purposes.) Add the code from each to your .vimrc to try it out.
1. Exit insert mode without using Esc
inoremap jk <Esc>
This is for the “how can you possibly use Vim you’re always at the body shop for new pinky fingers you spend all day hitting Esc” crowd. Your right index finger is on the j key by default, right? And k is right next to that. So this is a brilliantly fast way to get back to normal mode, no Esc required.
Credit: This tactic is crazy-common among Vim masters, of course. I think I first came across the idea in Steve Losh’s epic “Coming Home To Vim”.
2. Allow mispelings when :wq-ing
cabbrev ew :wq
cabbrev qw :wq
:cabbrev is one of the abbreviation commands. It’s specific to commands, so that in the first word of each of these lines, when we type it, will be taken as the second word (the command—in this case, :wq).
3. Switch windows with two keystrokes
noremap <c-j> <c-w>j
noremap <c-k> <c-w>k
noremap <c-l> <c-w>l
noremap <c-h> <c-w>h
I use these all the time. Normally, to switch windows (split windows) in Vim, you have to do Ctrl+W and then a direction key (j, k, h, or l). This changes that to just Ctrl and then the direction.
Credit: Chris McKinnel’s Coderwall protip, “Speed up your already speedy Vim development”.
4. Enter command mode with one keystroke
nnoremap ; :
nnoremap : ;
Likewise this—now, instead of Shift+; to run a command, you can just hit the ; key. YOU’RE SAVING A KEYSTROKE!
(No, seriously—if you use the Vim command line as much throughout the day as I do, you’ll find that every such keystroke adds up. Use this. You’re quite welcome.)
Credit: Also Chris McKinnel.
5. Start an external command with a single bang
nnoremap ! :!
To run an external command from within Vim, we just have to prepend ! at the Vim command line (as in :!date or :!ls). So that makes at least three keystrokes: (Shift+); and Shift+1—unless you use this mapping, in which case you can just hit Shift+1 in normal mode.
6. Auto-save a file when you leave insert mode
autocmd InsertLeave * if expand('%') != '' | update | endif
There are other ways to do this—I used to use something like this variation on #1 above:
inoremap jk <Esc>:w<cr>
Then I saw Romain Lafourcade recommending this autocommand on Stack Overflow, and switched to it right away. (A huge benefit of this approach: it catches, for example, when you leave insert mode via Ctrl+C—the InsertLeave event is triggered, as you might expect, whenever we leave insert mode.)
After the event (InsertLeave) and file pattern (*, or “any file”), we have an if statement which checks for something in the % special character.* If we’re editing a file, this will give us its filename; if not, it will return '' (an empty string). So we check for a non-empty string, and if we get one, we run :update (which saves the file only if it’s been modified—basically like a smarter :w). Then we end the if.
(Worth noting also is the | line separator. In VimL, this functions like the semicolon ; does in many other programming languages, denoting the end of a line and allowing us to continue our code all on the same line of the file, if we wish. We use it here to write all three lines of our autocommand’s code on the one autocommand line.)
UPDATE: See my newer post on this particular trick: “A More Betterer Autosave In Vim.”
What weird tricks do you have in your .vimrc?
* See :help cmdline-special for the whole list of those characters.