Vim starts in normal mode, where keys move and edit. Press Esc if you're not sure which mode you're in. Commands starting with : are typed in normal mode and finished with Enter, and so are searches with / and ?. Ctrl-r means hold Ctrl and press r. Keys such as dd are typed one after the other. Almost everything here works the same in Neovim. File names and words such as notes.txt and error are examples.

Install and start #

  • Check which version of Vim is installedLinux, macOS, Windows
    vim --version
  • Install Vim
    Debian, Ubuntu
    sudo apt install vim
    Red Hat, Fedora
    sudo dnf install vim-enhanced
    Alpine
    apk add vim
  • Install Vim with Homebrew, newer than the one macOS includesmacOS
    brew install vim
  • Install Vim on WindowsWindows
    winget install vim.vim
  • Install a Vim that can copy to the system clipboardDebian, Ubuntu
    sudo apt install vim-gtk3
  • Check Vim can use the system clipboard (look for +clipboard)Linux, macOS
    vim --version | grep clipboard
  • Open a file, or create it if it doesn't existLinux, macOS, Windows
    vim notes.txt
  • Open a file at line 42Linux, macOS, Windows
    vim +42 notes.txt
  • Open a file at the last lineLinux, macOS, Windows
    vim + app.log
  • Open a file at the first match of a wordLinux, macOS, Windows
    vim +/error app.log
  • Open a file read-onlyLinux, macOS, Windows
    vim -R notes.txt
  • Open two files side by sideLinux, macOS, Windows
    vim -O a.txt b.txt
  • Open several files in tabsLinux, macOS, Windows
    vim -p a.txt b.txt
  • Edit a system file as root, keeping your own Vim settingsLinux, macOS
    sudoedit /etc/ssh/sshd_config
  • Recover a file after Vim or the computer crashedLinux, macOS, Windows
    vim -r notes.txt
  • Start Vim without your settings or pluginsLinux, macOS, Windows
    vim --clean
  • Make Vim the editor Git uses for commit messagesLinux, macOS, Windows
    git config --global core.editor vim
  • Make Vim the default editor for crontab, sudoedit and other toolsLinux, macOS
    export EDITOR=vim
  • Start the built-in 30-minute Vim tutorialLinux, macOS, Windows
    vimtutor

Modes, quitting and saving #

  • Go back to normal mode, whatever you were doingLinux, macOS, Windows
    Esc
  • Quit VimLinux, macOS, Windows
    :q
  • Exit Vim without saving changesLinux, macOS, Windows
    :q!

    Warning: Throws away every change since you last saved.

  • Save and quit VimLinux, macOS, Windows
    :wq
  • Save only if there are changes, then quit VimLinux, macOS, Windows
    :x
  • Save and quit Vim from normal mode, without typing a colonLinux, macOS, Windows
    ZZ
  • Exit Vim without saving, from normal modeLinux, macOS, Windows
    ZQ

    Warning: Throws away every change since you last saved.

  • Quit every window and tabLinux, macOS, Windows
    :qa
  • Exit everything without saving any changesLinux, macOS, Windows
    :qa!

    Warning: Throws away the unsaved changes in every open file.

  • Save the fileLinux, macOS, Windows
    :w
  • Save a copy under a new name and keep editing the originalLinux, macOS, Windows
    :w backup.txt
  • Save under a new name and carry on editing the new fileLinux, macOS, Windows
    :saveas notes-v2.txt
  • Save every changed fileLinux, macOS, Windows
    :wa
  • Save and quit every fileLinux, macOS, Windows
    :wqa
  • Save a file you opened without sudoLinux, macOS
    :w !sudo tee % > /dev/null

    Warning: Works in Vim but not in Neovim. Use sudoedit next time.

  • Save only lines 10 to 20 to another fileLinux, macOS, Windows
    :10,20w part.txt
  • Start typing before the cursor (insert mode)Linux, macOS, Windows
    i
  • Start typing after the cursorLinux, macOS, Windows
    a
  • Start typing at the start of the lineLinux, macOS, Windows
    I
  • Start typing at the end of the lineLinux, macOS, Windows
    A
  • Open a new line below and start typingLinux, macOS, Windows
    o
  • Open a new line above and start typingLinux, macOS, Windows
    O
  • Type over the existing text (replace mode)Linux, macOS, Windows
    R
  • Run one normal-mode command without leaving insert modeLinux, macOS, Windows
    Ctrl-o
  • Show the file name, line number and how far through you areLinux, macOS, Windows
    Ctrl-g

Open and switch files #

  • Open another fileLinux, macOS, Windows
    :e config.yaml
  • Reload the file from disk, throwing away your changesLinux, macOS, Windows
    :e!

    Warning: Throws away every change since you last saved.

  • Browse the files in the current folderLinux, macOS, Windows
    :Explore
  • Open the file whose name is under the cursorLinux, macOS, Windows
    gf
  • Insert another file's contents below the cursorLinux, macOS, Windows
    :r header.txt
  • Insert the output of a command below the cursorLinux, macOS
    :r !date
  • List open files (buffers)Linux, macOS, Windows
    :ls
  • Switch to buffer 2Linux, macOS, Windows
    :b 2
  • Switch to an open file by part of its nameLinux, macOS, Windows
    :b conf
  • Go to the next or previous open fileLinux, macOS, Windows
    :bn :bp
  • Go back to the file you were in beforeLinux, macOS, Windows
    Ctrl-^
  • Close the current file but keep Vim openLinux, macOS, Windows
    :bd
  • Find a file by name in this folder or below, and open itLinux, macOS, Windows
    :set path+=** :find settings.py
  • Change Vim's working folderLinux, macOS, Windows
    :cd ~/projects/site
  • Show Vim's working folderLinux, macOS, Windows
    :pwd

Moving around #

  • Move left, down, up and rightLinux, macOS, Windows
    h j k l
  • Move down 5 linesLinux, macOS, Windows
    5j
  • Jump to the next word, or back a wordLinux, macOS, Windows
    w b
  • Jump to the end of the wordLinux, macOS, Windows
    e
  • Jump by words separated only by spaces, skipping punctuationLinux, macOS, Windows
    W B E
  • Go to the start of the lineLinux, macOS, Windows
    0
  • Go to the first character of the line that isn't a spaceLinux, macOS, Windows
    ^
  • Go to the end of the lineLinux, macOS, Windows
    $
  • Go to the top of the fileLinux, macOS, Windows
    gg
  • Go to the bottom of the fileLinux, macOS, Windows
    G
  • Go to line 42Linux, macOS, Windows
    :42
  • Go halfway through the fileLinux, macOS, Windows
    50%
  • Go to column 20 of the lineLinux, macOS, Windows
    20|
  • Scroll down or up half a screenLinux, macOS, Windows
    Ctrl-d Ctrl-u
  • Scroll down or up a whole screenLinux, macOS, Windows
    Ctrl-f Ctrl-b
  • Move to the top, middle or bottom of the screenLinux, macOS, Windows
    H M L
  • Scroll so the cursor's line is in the middle of the screenLinux, macOS, Windows
    zz
  • Scroll so the cursor's line is at the top or bottom of the screenLinux, macOS, Windows
    zt zb
  • Jump to the matching bracketLinux, macOS, Windows
    %
  • Jump to the next or previous paragraphLinux, macOS, Windows
    } {
  • Jump to the next x on the lineLinux, macOS, Windows
    fx
  • Jump to just before the next x on the lineLinux, macOS, Windows
    tx
  • Repeat the last f or t jump, forwards or backwardsLinux, macOS, Windows
    ; ,
  • Go back to where you were before a jump, or forward againLinux, macOS, Windows
    Ctrl-o Ctrl-i
  • Go back to where you last changed somethingLinux, macOS, Windows
    g;

Marks #

  • Set mark a at the cursorLinux, macOS, Windows
    ma
  • Jump to the line with mark aLinux, macOS, Windows
    'a
  • Jump to the exact spot of mark aLinux, macOS, Windows
    `a
  • Set a mark you can jump to from any file (capital letter)Linux, macOS, Windows
    mA
  • Go back to the line you were on before the last jumpLinux, macOS, Windows
    ''
  • Go to the spot of the last changeLinux, macOS, Windows
    `.
  • List all marksLinux, macOS, Windows
    :marks
  • Delete mark aLinux, macOS, Windows
    :delmarks a

Editing #

  • Delete the character under the cursorLinux, macOS, Windows
    x
  • Delete the lineLinux, macOS, Windows
    dd
  • Delete 3 linesLinux, macOS, Windows
    3dd
  • Delete to the end of the wordLinux, macOS, Windows
    dw
  • Delete to the end of the lineLinux, macOS, Windows
    D
  • Delete to the end of the fileLinux, macOS, Windows
    dG
  • Delete the text inside quotesLinux, macOS, Windows
    di"
  • Delete the text inside bracketsLinux, macOS, Windows
    di(
  • Delete the brackets and everything in themLinux, macOS, Windows
    da(
  • Delete the text inside an HTML or XML tagLinux, macOS, Windows
    dit
  • Change a word (delete it and start typing)Linux, macOS, Windows
    cw
  • Change the whole word the cursor is inLinux, macOS, Windows
    ciw
  • Change the whole lineLinux, macOS, Windows
    cc
  • Change to the end of the lineLinux, macOS, Windows
    C
  • Change the text inside quotesLinux, macOS, Windows
    ci"
  • Replace the character under the cursor with xLinux, macOS, Windows
    rx
  • Join the next line onto this oneLinux, macOS, Windows
    J
  • Switch the case of the character under the cursorLinux, macOS, Windows
    ~
  • Make the line lowercase or UPPERCASELinux, macOS, Windows
    guu gUU
  • Indent the line, or take the indent offLinux, macOS, Windows
    >> <<
  • Fix the indentation of the whole fileLinux, macOS, Windows
    gg=G
  • Add 1 to the number under the cursor, or take 1 awayLinux, macOS, Windows
    Ctrl-a Ctrl-x
  • Repeat the last changeLinux, macOS, Windows
    .
  • Swap this line with the one belowLinux, macOS, Windows
    ddp
  • Swap two charactersLinux, macOS, Windows
    xp
  • Delete lines 10 to 20Linux, macOS, Windows
    :10,20d
  • Delete every line that contains a wordLinux, macOS, Windows
    :g/DEBUG/d

    Warning: Check the pattern with a search first. Undo with u if it takes too much.

  • Delete every line that doesn't contain a wordLinux, macOS, Windows
    :v/ERROR/d

    Warning: Check the pattern with a search first. Undo with u if it takes too much.

  • Delete empty lines, including ones with only spacesLinux, macOS, Windows
    :g/^\s*$/d
  • Remove spaces and tabs from the ends of linesLinux, macOS, Windows
    :%s/\s\+$//e
  • Sort the linesLinux, macOS, Windows
    :sort
  • Sort the lines and remove duplicatesLinux, macOS, Windows
    :sort u
  • Sort lines by the number at the startLinux, macOS, Windows
    :sort n
  • Reverse the order of the linesLinux, macOS, Windows
    :g/^/m0
  • Wrap long lines in a paragraph to the text widthLinux, macOS, Windows
    gqip
  • Insert a character by its Unicode number, such as an en dashLinux, macOS, Windows
    Ctrl-v u2013

Undo and redo #

  • UndoLinux, macOS, Windows
    u
  • RedoLinux, macOS, Windows
    Ctrl-r
  • Undo every change on the line you just editedLinux, macOS, Windows
    U
  • Go back to how the file was 10 minutes agoLinux, macOS, Windows
    :earlier 10m
  • Go forward in time againLinux, macOS, Windows
    :later 10m
  • Go back to how the file was when you last savedLinux, macOS, Windows
    :earlier 1f
  • Keep undo history after you close the fileLinux, macOS, Windows
    :set undofile

Copy and paste #

  • Copy (yank) the lineLinux, macOS, Windows
    yy
  • Copy 3 linesLinux, macOS, Windows
    3yy
  • Copy the wordLinux, macOS, Windows
    yiw
  • Copy to the end of the lineLinux, macOS, Windows
    y$
  • Copy the whole fileLinux, macOS, Windows
    :%y
  • Paste after the cursor, or below the lineLinux, macOS, Windows
    p
  • Paste before the cursor, or above the lineLinux, macOS, Windows
    P
  • Paste the last thing you copied, not the last thing you deletedLinux, macOS, Windows
    "0p
  • Copy the line into register aLinux, macOS, Windows
    "ayy
  • Paste from register aLinux, macOS, Windows
    "ap
  • Copy the line to the system clipboardLinux, macOS, Windows
    "+yy
  • Copy the selection to the system clipboardLinux, macOS, Windows
    "+y
  • Paste from the system clipboardLinux, macOS, Windows
    "+p
  • Copy the whole file to the system clipboardLinux, macOS, Windows
    :%y +
  • Paste while typing, without leaving insert modeLinux, macOS, Windows
    Ctrl-r "
  • Show what's in every registerLinux, macOS, Windows
    :registers
  • Copy lines 5 to 10 to below line 20Linux, macOS, Windows
    :5,10t 20
  • Move lines 5 to 10 to below line 20Linux, macOS, Windows
    :5,10m 20
  • Move the current line to the top of the fileLinux, macOS, Windows
    :m 0

Find and replace #

  • Replace the first match on the lineLinux, macOS, Windows
    :s/old/new/
  • Replace every match on the lineLinux, macOS, Windows
    :s/old/new/g
  • Replace every match in the fileLinux, macOS, Windows
    :%s/old/new/g
  • Replace every match in the file, asking each timeLinux, macOS, Windows
    :%s/old/new/gc
  • Replace in lines 10 to 20Linux, macOS, Windows
    :10,20s/old/new/g
  • Replace in the lines you've selectedLinux, macOS, Windows
    :'<,'>s/old/new/g
  • Replace whole words onlyLinux, macOS, Windows
    :%s/\<old\>/new/g
  • Replace, ignoring caseLinux, macOS, Windows
    :%s/old/new/gi
  • Replace text containing slashes, such as a pathLinux, macOS, Windows
    :%s#/var/www#/srv/www#g
  • Replace with the last word you searched forLinux, macOS, Windows
    :%s//new/g
  • Put each comma-separated item on its own lineLinux, macOS, Windows
    :%s/,/\r/g
  • Put the matched text back in the replacementLinux, macOS, Windows
    :%s/v[0-9]\+/release &/g
  • Swap two words using groupsLinux, macOS, Windows
    :%s/\(\w\+\) \(\w\+\)/\2 \1/
  • Repeat the last replace on this lineLinux, macOS, Windows
    &
  • Repeat the last replace on every lineLinux, macOS, Windows
    g&
  • Replace in every .conf file below this folder, and save themLinux, macOS, Windows
    :args **/*.conf :argdo %s/old/new/ge | update

    Warning: Saves every file it changes. Commit or back up first.

Visual mode (selecting) #

  • Select charactersLinux, macOS, Windows
    v
  • Select whole linesLinux, macOS, Windows
    V
  • Select a block (columns)Linux, macOS, Windows
    Ctrl-v
  • Select everythingLinux, macOS, Windows
    ggVG
  • Select the paragraphLinux, macOS, Windows
    vip
  • Select inside brackets or bracesLinux, macOS, Windows
    vi( vi{
  • Select inside quotesLinux, macOS, Windows
    vi"
  • Select the last selection againLinux, macOS, Windows
    gv
  • Jump to the other end of the selectionLinux, macOS, Windows
    o
  • Copy, delete or change the selectionLinux, macOS, Windows
    y d c
  • Indent the selection, or take the indent offLinux, macOS, Windows
    > <
  • Make the selection UPPERCASE or lowercaseLinux, macOS, Windows
    U u
  • Add # to the start of several linesLinux, macOS, Windows
    Ctrl-v jj I# Esc
  • Add ; to the end of several linesLinux, macOS, Windows
    Ctrl-v jj $A; Esc
  • Run a normal-mode command on every selected lineLinux, macOS, Windows
    :'<,'>norm A;
  • Sort the selected linesLinux, macOS, Windows
    :'<,'>sort

Windows and tabs #

  • Split the window and open a file aboveLinux, macOS, Windows
    :split other.txt
  • Split the window and open a file alongsideLinux, macOS, Windows
    :vsplit other.txt
  • Move to the next windowLinux, macOS, Windows
    Ctrl-w w
  • Move to the window left, below, above or rightLinux, macOS, Windows
    Ctrl-w h Ctrl-w j Ctrl-w k Ctrl-w l
  • Make all windows the same sizeLinux, macOS, Windows
    Ctrl-w =
  • Make the window 20 lines tallLinux, macOS, Windows
    :resize 20
  • Make the window 80 columns wideLinux, macOS, Windows
    :vertical resize 80
  • Close the windowLinux, macOS, Windows
    :close
  • Close every window except this oneLinux, macOS, Windows
    :only
  • Open a file in a new tabLinux, macOS, Windows
    :tabnew other.txt
  • Go to the next or previous tabLinux, macOS, Windows
    gt gT
  • Close the tabLinux, macOS, Windows
    :tabclose
  • Open a terminal inside VimLinux, macOS, Windows
    :terminal

Compare files (diff) #

  • Compare two files side by sideLinux, macOS, Windows
    vim -d old.conf new.conf
  • Jump to the next or previous differenceLinux, macOS, Windows
    ]c [c
  • Take the other file's version of this differenceLinux, macOS, Windows
    do
  • Send this version to the other fileLinux, macOS, Windows
    dp
  • Recalculate the differences after editingLinux, macOS, Windows
    :diffupdate
  • Compare two windows you already have open (run in each)Linux, macOS, Windows
    :diffthis
  • Stop comparingLinux, macOS, Windows
    :diffoff!
  • See what you've changed since you last savedLinux, macOS
    :w !diff % -

Macros #

  • Start recording a macro into register aLinux, macOS, Windows
    qa
  • Stop recordingLinux, macOS, Windows
    q
  • Play macro aLinux, macOS, Windows
    @a
  • Play the last macro againLinux, macOS, Windows
    @@
  • Play macro a 10 timesLinux, macOS, Windows
    10@a
  • Play macro a on lines 5 to 20Linux, macOS, Windows
    :5,20norm @a
  • Play macro a on every selected lineLinux, macOS, Windows
    :'<,'>norm @a
  • See what's in macro aLinux, macOS, Windows
    :reg a

Folding #

  • Fold by indentationLinux, macOS, Windows
    :set foldmethod=indent
  • Fold a paragraph you chooseLinux, macOS, Windows
    zfap
  • Open or close the fold under the cursorLinux, macOS, Windows
    za
  • Open every foldLinux, macOS, Windows
    zR
  • Close every foldLinux, macOS, Windows
    zM
  • Delete the fold under the cursor, keeping the textLinux, macOS, Windows
    zd

Settings #

  • Show line numbersLinux, macOS, Windows
    :set number
  • Show line numbers relative to the cursorLinux, macOS, Windows
    :set relativenumber
  • Hide line numbersLinux, macOS, Windows
    :set nonumber norelativenumber
  • Turn on syntax highlightingLinux, macOS, Windows
    :syntax on
  • Tell Vim what kind of file it is, for highlightingLinux, macOS, Windows
    :set filetype=yaml
  • Use 4 spaces when you press TabLinux, macOS, Windows
    :set expandtab tabstop=4 shiftwidth=4
  • Turn the tabs in the file into spacesLinux, macOS, Windows
    :retab
  • Show tabs and spaces at the ends of linesLinux, macOS, Windows
    :set list
  • Stop long lines wrappingLinux, macOS, Windows
    :set nowrap
  • Let the mouse move the cursor and select textLinux, macOS, Windows
    :set mouse=a
  • Show what you're typing in normal mode, at the bottom rightLinux, macOS, Windows
    :set showcmd
  • Turn on spell checking in British EnglishLinux, macOS, Windows
    :set spell spelllang=en_gb
  • Jump to the next misspelt wordLinux, macOS, Windows
    ]s
  • Show spelling suggestions for the wordLinux, macOS, Windows
    z=
  • Change the colour schemeLinux, macOS, Windows
    :colorscheme desert
  • Check what a setting is set toLinux, macOS, Windows
    :set tabstop?
  • See which file last changed a settingLinux, macOS, Windows
    :verbose set tabstop?
  • Turn a setting back to Vim's defaultLinux, macOS, Windows
    :set tabstop&
  • Save the file with Unix line endings, from Windows onesLinux, macOS, Windows
    :set fileformat=unix :w
  • Open your settings file (vimrc)Linux, macOS, Windows
    :e $MYVIMRC
  • Create your settings file
    Linux, macOS
    vim ~/.vimrc
    Windows
    vim ~/_vimrc
  • Reload your settings without restarting VimLinux, macOS, Windows
    :source $MYVIMRC
  • Good starter settings for your vimrcLinux, macOS, Windows
    set number syntax on filetype plugin indent on set expandtab tabstop=4 shiftwidth=4 set ignorecase smartcase incsearch hlsearch set showcmd
  • Use the space bar as your leader key (add to vimrc)Linux, macOS, Windows
    let mapleader = " "
  • Make Space then w save the file (add to vimrc)Linux, macOS, Windows
    nnoremap <leader>w :w<CR>
  • Install a plugin with Vim's built-in packagesLinux, macOS
    git clone https://github.com/tpope/vim-surround ~/.vim/pack/plugins/start/vim-surround

Help and shell commands #

  • Open Vim's helpLinux, macOS, Windows
    :help
  • Get help on a commandLinux, macOS, Windows
    :help :wq
  • Get help on a keyLinux, macOS, Windows
    :help dd
  • Search all of the helpLinux, macOS, Windows
    :helpgrep undo
  • Follow a link in the help, and come backLinux, macOS, Windows
    Ctrl-] Ctrl-o
  • Run a shell command without leaving Vim
    Linux, macOS
    :!ls
    Windows
    :!dir
  • Drop to a shell, then type exit to come backLinux, macOS, Windows
    :sh
  • Sort the whole file with the system sort commandLinux, macOS
    :%!sort
  • Format a JSON file neatly (needs jq installed)Linux, macOS, Windows
    :%!jq .
  • Count the words, lines and characters in the fileLinux, macOS, Windows
    g Ctrl-g
  • Show the last error messages againLinux, macOS, Windows
    :messages
  • Find out why Vim starts slowlyLinux, macOS, Windows
    vim --startuptime startup.log