~ 4 min
Vim magic
Vim magic
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
Edition magic
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+v
Start visual mode, selecting columns=<next position>
Indentzz
Scroll the current line to the center of the windowzt
Scroll the current line to the top of the windowzb
Scroll the current line to the bottom of the windowcs"'
Change the surrounding double quotes by simple quotes (surround.vim is required)/\csearchString
Perform a case insensitive search/\CsearchString
Perform a case sensitive search
Marks
mk
mark the current position ask
(any other lowercase letter will do)mK
mark the current position asK
. Uppercase means that it is a global mark, and it will be available even if you currently have a different file opened.'k
jump to the position previously marked ask
Folds
zf'k
fold lines until thek
markzo
open the folded codezc
close the folded codeza
toggle the folded code
Sessions
:mks ~/.vim/foo.vim
Save the current vim session:source ~/.vim/foo.vim
Restore the previously saved session
Tabs
gT
Go to the previous tabgt
Go to the next tab:tabfind <TAB>
Find a file with autocompletion and open it in a new tab
Splitting
vsplit
Split verticallysplit
Split horizontally
Other commands
:echo $VIM
Show the path to the vimrc configuration file (usually just~/.vimrc
):!command
Run 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.js
using autocompletion:b <TAB>
Open a file that has been previously opened (and is still in the buffer):ls
List the previously opened files:vert diffs otherfile
Diff the current file withotherfile
In edit mode
<CTRL>n
Autocomplete
Custom settings
These settings can be saved into the vimrc file.
syntax on
Activate the syntax highlightingset number
Show the line numbersset autoindent
Autoindentationset path+=**
Include subdirectories when finding a fileset wildmenu
Menu helper when using tab finding files or directoriesset nocompatible
Ignore compatibility with ancient vi versionsset smartcase
Always perform case insensitive search unless you search for capital lettersset wildignore+=**/node_modules/**
Ignore directory when finding filesset wrap linebreak nolist
Activate 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 :)