Instant Savings In Vim
Posted on May 23, 2013
I save text files hundreds of times every day. I hope to have a long career developing software, so anything I do hundreds of times a day needs to be as efficient as possible.
To speed up this oft-used task I added this little bit to my
" Save all the things. nmap <Leader>s :w<CR> imap <Leader>s <ESC>:w<CR>
So what do these mappings do? Whether I’m in insert mode or normal mode, the key sequence
,s saves the file.
nmap defines a mapping in normal mode.
<Leader>s is the left hand side of this mapping. It’s what triggers the mapping.
:w<CR> is the right hand side of the mapping. This is what is executed when the mapping is triggered.
So when I type
,s vim executes
:w<CR>, saving the file.
imap defines a mapping in insert mode. When in insert mode, we need to escape to normal mode before writing the file. Hence, the prepending
<ESC> on the right hand side of the mapping.
To see more that you can do with mapping keys, Vim’s
:help map is a great place to start reading.