Skip to content
Return to Knowledge

Modal, keyboard-driven text editor built for speed and efficient editing, available almost everywhere.

PlatformWindows, macOS, Linux

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.

powershell
[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).

vim
set number relativenumber
set expandtab shiftwidth=4 tabstop=4
set ignorecase smartcase
set incsearch hlsearch
set clipboard=unnamedplus
syntax on

Modes โ€‹

Vim is modal: the same keys do different things depending on the mode.

ModeEnter withPurpose
NormalEscNavigate and run commands (default mode)
Inserti / a / oType text
Visualv / V / Ctrl VSelect characters / lines / block
Command-line:Run Ex commands (save, quit, search & replace)

Shortcuts โ€‹

Files & quitting โ€‹

ActionShortcut
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 โ€‹

ActionShortcut
Left / down / up / righth j k l
Next / previous wordw / b
End of worde
Start / end of line0 / $
First non-blank character^
Start / end of filegg / G
Go to line N:N or NG
Half page down / upCtrl D / Ctrl U
Jump to matching bracket%
Jump back / forwardCtrl O / Ctrl I

Editing โ€‹

ActionShortcut
Insert before / after cursori / a
Insert at line start / endI / A
New line below / aboveo / O
Delete characterx
Delete linedd
Delete to end of lineD
Change wordcw
Change inside quotesci"
Yank (copy) lineyy
Paste after / beforep / P
Undo / redou / Ctrl R
Repeat last change.
Indent / unindent line>> / <<

Search & replace โ€‹

ActionShortcut
Search forward / backward/text / ?text
Next / previous matchn / 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 โ€‹

ActionShortcut
Split horizontally / vertically:sp / :vsp
Move between splitsCtrl W then h j k l
New tab:tabnew
Next / previous tabgt / 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.

CommandMeaning
dwDelete to next word
d$Delete to end of line
yiwYank inner word
ci(Change inside parentheses
da"Delete around quotes (including the quotes)
3ddDelete 3 lines
gU + motionUppercase

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 โ€‹

Good tutorial

Plugins can be managed with vim-plug. Setup on Windows:

Where things live โ€‹

PathPurpose
~\_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 โ€‹

  1. Install vim-plug (requires git on the PATH):

    powershell
    iwr -useb https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim |`
      ni $HOME/vimfiles/autoload/plug.vim -Force
  2. Create the config file. The block below is file content for _vimrc, not commands to type into a terminal:

    powershell
    vim $HOME/_vimrc

    Press i, paste the config, press Esc, then :wq.

    vim
    set 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>
  3. Reopen Vim and run :PlugInstall.

CommandAction
:PlugInstallInstall plugins listed in _vimrc
:PlugUpdateUpdate installed plugins
:PlugCleanRemove plugins no longer listed

Markdown plugins โ€‹

PluginPurposeUsage
vim-markdownSyntax highlighting and navigation for Markdown:Toc table of contents, :TableFormat, ]] / [[ jump between headers
vim-pencilBetter prose writing (soft wrap):PencilToggle
goyo.vimDistraction-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 \.

Home ยท Hobi ยท Blog
Made with โค๏ธ in