Vim Magic
Unlock the power of Vim with tips and tricks to enhance your text editing workflow
Published
- 3 min read
I’ve been using the Vim editor for a while, and I really like it. However, it’s true that the learning curve is steep. With so many options, even after years of use, you can still discover new tricks.
This is a compilation of my favorite commands, which I’ll continue updating as I discover more (think of this as my personal quick reference).
Normal Mode
Editing Magic
vi"– Select everything inside". Also works with parentheses, brackets, tags, etc.di"– Delete everything inside".da"– Delete everything inside"and include the surrounding"as well.CTRL+v– Start visual mode, selecting columns.=<next position>– Indent to the next position.zz– Scroll the current line to the center of the window.zt– Scroll the current line to the top of the window.zb– Scroll the current line to the bottom of the window.cs"'– Change surrounding double quotes to single quotes (requires surround.vim)./\csearchString– Perform a case-insensitive search./\CsearchString– Perform a case-sensitive search.
Marks
mk– Mark the current position ask(or any other lowercase letter).mK– Mark the current position asK. Uppercase marks are global and persist across files.'k– Jump to the position marked ask.
Folds
zf'k– Fold lines up to thekmark.zo– Open folded code.zc– Close folded code.za– Toggle folded code.
Sessions
:mks ~/.vim/foo.vim– Save the current Vim session.:source ~/.vim/foo.vim– Restore a saved session.
Tabs
gT– Go to the previous tab.gt– Go to the next tab.:tabfind <TAB>– Find a file with autocompletion and open it in a new tab.
Splitting
:vsplit– Split the screen vertically.:split– Split the screen horizontally.
Other Commands
:echo $VIM– Display the path to the Vim configuration file (usually~/.vimrc).:!command– Run a Unix command.:!!– Replace the selection with the output of the last command.:e .– Open the file explorer in the current directory.:find *.js <TAB>– Find files with the.jsextension using autocompletion.:b <TAB>– Reopen previously opened files from the buffer.:ls– List previously opened files.:vert diffs otherfile– Diff the current file againstotherfile.
Edit Mode
<CTRL>n– Autocomplete suggestions.
Custom Settings
Save these settings in your ~/.vimrc file:
syntax on– Enable syntax highlighting.set number– Show line numbers.set autoindent– Enable auto-indentation.set path+=**– Include subdirectories when searching for files.set wildmenu– Display a menu helper for file/directory autocompletion.set nocompatible– Ignore compatibility with older versions ofvi.set smartcase– Perform case-insensitive searches unless capital letters are used.set wildignore+=**/node_modules/**– Exclude directories (e.g.,node_modules) from searches.set wrap linebreak nolist– Enable word wrap.
Exporting Your Configuration
You can save and synchronize your Vim configuration across machines by turning your ~/.vim directory into a Git repository. Push it to a platform like GitHub.
You can find my repository here, with instructions in the README for setup.
…and Of Course
:q!– Don’t get stuck in Vim. Exit without saving. :)
References
- How to Do 90% of What Plugins Do (YouTube)
- Mastering the Vim Language (YouTube)
- Vim + Tmux (YouTube)
- Vim Documentation
- Why I Love Vim: Lesser Known Features