~ 4 min
I’ve been using the vim editor for a while now, and I really like it. But it’s true that the learning curve is very steep. There are so many options, that even after years of using it, you can still discover lots of new tricks.
This is a compilation of my favorite commands. I will keep updating this post in the future whenever I discover new interesting stuff (this is kind of a personal quick reference).
In normal mode
vi"Select everything inside
". It also works with parenthesis, brackets, tags…
di"Delete everything inside
". Same as before.
da"Delete everything inside
", including the surrounding
CTRL+vStart visual mode, selecting columns
zzScroll the current line to the center of the window
ztScroll the current line to the top of the window
zbScroll the current line to the bottom of the window
cs"'Change the surrounding double quotes by simple quotes (surround.vim is required)
/\csearchStringPerform a case insensitive search
/\CsearchStringPerform a case sensitive search
mkmark the current position as
k(any other lowercase letter will do)
mKmark the current position as
K. Uppercase means that it is a global mark, and it will be available even if you currently have a different file opened.
'kjump to the position previously marked as
zf'kfold lines until the
zoopen the folded code
zcclose the folded code
zatoggle the folded code
:mks ~/.vim/foo.vimSave the current vim session
:source ~/.vim/foo.vimRestore the previously saved session
gTGo to the previous tab
gtGo to the next tab
:tabfind <TAB>Find a file with autocompletion and open it in a new tab
:echo $VIMShow the path to the vimrc configuration file (usually just
:!commandRun a unix command
:!!Swap the selection by the stdout of the last command
:e .Open the file explorer on the current directory, in order to edit a file
:find *.js <TAB>Find a file with extension
:b <TAB>Open a file that has been previously opened (and is still in the buffer)
:lsList the previously opened files
:vert diffs otherfileDiff the current file with
In edit mode
These settings can be saved into the vimrc file.
syntax onActivate the syntax highlighting
set numberShow the line numbers
set path+=**Include subdirectories when finding a file
set wildmenuMenu helper when using tab finding files or directories
set nocompatibleIgnore compatibility with ancient vi versions
set smartcaseAlways perform case insensitive search unless you search for capital letters
set wildignore+=**/node_modules/**Ignore directory when finding files
set wrap linebreak nolistActivate word wrap
Exporting your configuration
You can save your custom vim configuration to a repository and synchronize it with any other machine.
In order to do that, just initialize your
~/.vim directory as a git repository,
and push it to the cloud (e.g.: to GitHub). You can find mine here
(follow the instructions on the README in order to set it up).
…and of course
:q!Don’t get trapped. Leave without saving :)