Modal, keyboard-driven text editor built for speed and efficient editing, available almost everywhere.
Introduction โ
Vim can be quite a confusing program when you start using it. I'm still in the confused state, so bare with me.
If vim is not recognised in PowerShell, add the install folder (e.g. C:\Program Files\Vim\vim92) to your PATH, then reopen the terminal.
[Environment]::SetEnvironmentVariable("Path", $env:Path + ";C:\Program Files\Vim\vim92", "User")Then close and reopen PowerShell, and vim $HOME/ will work.
TIP
Run vimtutor in a terminal for a built-in 30 minute interactive tutorial.
Config โ
Settings live in ~/.vimrc (on Windows: %USERPROFILE%\_vimrc).
set number relativenumber
set expandtab shiftwidth=4 tabstop=4
set ignorecase smartcase
set incsearch hlsearch
set clipboard=unnamedplus
syntax onModes โ
Vim is modal: the same keys do different things depending on the mode.
| Mode | Enter with | Purpose |
|---|---|---|
| Normal | Esc | Navigate and run commands (default mode) |
| Insert | i / a / o | Type text |
| Visual | v / V / Ctrl V | Select characters / lines / block |
| Command-line | : | Run Ex commands (save, quit, search & replace) |
Shortcuts โ
Files & quitting โ
| Action | Shortcut |
|---|---|
| Save | :w |
| Save and quit | :wq or ZZ |
| Quit | :q |
| Quit without saving | :q! |
| Open file | :e path/to/file |
| Reload file from disk | :e! |
Moving around โ
| Action | Shortcut |
|---|---|
| Left / down / up / right | h j k l |
| Next / previous word | w / b |
| End of word | e |
| Start / end of line | 0 / $ |
| First non-blank character | ^ |
| Start / end of file | gg / G |
| Go to line N | :N or NG |
| Half page down / up | Ctrl D / Ctrl U |
| Jump to matching bracket | % |
| Jump back / forward | Ctrl O / Ctrl I |
Editing โ
| Action | Shortcut |
|---|---|
| Insert before / after cursor | i / a |
| Insert at line start / end | I / A |
| New line below / above | o / O |
| Delete character | x |
| Delete line | dd |
| Delete to end of line | D |
| Change word | cw |
| Change inside quotes | ci" |
| Yank (copy) line | yy |
| Paste after / before | p / P |
| Undo / redo | u / Ctrl R |
| Repeat last change | . |
| Indent / unindent line | >> / << |
Search & replace โ
| Action | Shortcut |
|---|---|
| Search forward / backward | /text / ?text |
| Next / previous match | n / N |
| Search word under cursor | * |
| Replace all in file | :%s/old/new/g |
| Replace all with confirmation | :%s/old/new/gc |
| Clear search highlight | :noh |
Windows, tabs & buffers โ
| Action | Shortcut |
|---|---|
| Split horizontally / vertically | :sp / :vsp |
| Move between splits | Ctrl W then h j k l |
| New tab | :tabnew |
| Next / previous tab | gt / gT |
| List buffers | :ls |
| Switch buffer | :b N |
Grammar โ
Vim commands read like a sentence: operator + motion (or text object). Learn a few of each and combine them.
| Command | Meaning |
|---|---|
dw | Delete to next word |
d$ | Delete to end of line |
yiw | Yank inner word |
ci( | Change inside parentheses |
da" | Delete around quotes (including the quotes) |
3dd | Delete 3 lines |
gU + motion | Uppercase |
Common operators: d delete, c change, y yank, > indent, gU / gu upper / lower case. Text objects: iw word, i" quotes, i( parentheses, it HTML tag, ip paragraph. Use i for inner, a for around.
Plugins โ
Plugins can be managed with vim-plug. Setup on Windows:
Where things live โ
| Path | Purpose |
|---|---|
~\_vimrc (C:\Users\<name>\_vimrc) | Config file, read by Vim at startup |
~\vimfiles\ | Config folder: plugins, autoload\plug.vim, colour schemes |
~\vimfiles\plugged\ | Where vim-plug downloads plugins (managed automatically) |
~ is shorthand for your user's home folder. On Linux/macOS the equivalents are ~/.vimrc and ~/.vim/; Windows uses _vimrc and vimfiles because dot-names are not hidden there. Run :version in Vim to see which locations it checks, and :echo $MYVIMRC to see which one it loaded.
Setup โ
Install vim-plug (requires
giton the PATH):powershelliwr -useb https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim |` ni $HOME/vimfiles/autoload/plug.vim -ForceCreate the config file. The block below is file content for
_vimrc, not commands to type into a terminal:powershellvim $HOME/_vimrcPress i, paste the config, press Esc, then
:wq.vimset nocompatible filetype plugin indent on syntax on call plug#begin('~/vimfiles/plugged') Plug 'preservim/vim-markdown' Plug 'junegunn/goyo.vim' Plug 'preservim/vim-pencil' call plug#end() " vim-markdown let g:vim_markdown_folding_disabled = 1 " folding is on by default and can be annoying let g:vim_markdown_frontmatter = 1 " highlight YAML front matter " vim-pencil: enable only for prose filetypes augroup pencil autocmd! autocmd FileType markdown,mkd call pencil#init({'wrap': 'soft'}) augroup END " goyo: toggle distraction-free mode nnoremap <leader>g :Goyo<CR>Reopen Vim and run
:PlugInstall.
| Command | Action |
|---|---|
:PlugInstall | Install plugins listed in _vimrc |
:PlugUpdate | Update installed plugins |
:PlugClean | Remove plugins no longer listed |
Markdown plugins โ
| Plugin | Purpose | Usage |
|---|---|---|
| vim-markdown | Syntax highlighting and navigation for Markdown | :Toc table of contents, :TableFormat, ]] / [[ jump between headers |
| vim-pencil | Better prose writing (soft wrap) | :PencilToggle |
| goyo.vim | Distraction-free writing | :Goyo or \ g, :Goyo 80 sets the width |
TIP
vim-markdown only works with filetype plugin indent on and syntax on in _vimrc. The default leader key is \.