Vim Tips

This document is intended as a fairly minimal quick-reference aimed at someone who generally uses another text editor, but occasionally has the need to use Vim.

For a more detailed quick-reference guide, just use the Vim command :help quickref.

Installation

On a Debian system, install the vim package for a basic Vim installation. Use the command apt-cache show vim for more information. Look at the other available packages using the command apt-cache search -n 'vim-*'.

Configuration

Usually a Vim installation configures Vim such that it is run to be fairly compatible with the original Vi by default. To set up some good defaults for a better Vim experience, copy the installed default configuration file to create your own vim configuration file. To see where Vim looked for configuration files, use the :version command.

Use the :!echo $VIMRUNTIME command to determine the runtime location. Copy $VIMRUNTIME/vimrc_example.vim to the location of your private configuration file. For Unix and macOS this is ~/.vimrc. See :help initialization for more details.

See :help vimrc_example.vim for details of the example configuration file.

The example configuration file executes $VIMRUNTIME/defaults.vim. If a system-wide configuration file sets the skip_defaults_vim variable, the defaults configuration file exits without applying all its lovely configurations. You can check whether the variable is set by entering the command :set skip_defaults_vim. If the variable is not set, you will see an error message that it is an unknown option.

To prevent the defaults configuration file from exiting early, modify your ~/.vimrc to unset the variable before defaults.vim is sourced. Add the following line before the source command:

unlet! skip_defaults_vim

Quit and restart Vim to read the new configurations.

To aid debugging any issues, use the :scriptnames command to list all the start-up scripts in the order they were sourced.

See also Debug unexpected option settings

Common Keys

  • <ESC> - Enter normal mode
  • h,j,k,l - cursor movement
  • x delete character
  • i - insert text
  • a - insert text after the cursor
  • A - append text to end of line
  • r - replace character
  • R - replace multiple characters
  • o - open a line below the cursor and enter insert mode
  • O - open a line above the cursor and enter insert mode
  • 0 - go to beginning of line
  • $ - go to end of line
  • u - undo
  • U - restore line to original state
  • CTRL-R - redo
  • ce - change to end of word
  • c$ - change to end of line
  • y - yank (copy) text highlighted with visual mode
  • yw - yank word
  • yy - yank entire line
  • "+y - copy to + register (which may be the system clipboard, depending on Vim compilation option) See Accessing the system clipboard for more information
  • p - put (paste) last deleted text after cursor
  • "+p - paste from + register (which may be the system clipboard, depending on Vim compilation option) See Accessing the system clipboard for more information
  • J - join lines, inserting spaces
  • gJ - join lines, without inserting spaces
  • CTRL-G - show location in the file and file status
  • G - move to the end of the file
  • [number] G - move to line number
  • gg - move to beginning of file
  • CTRL-F - scroll window forwards
  • CTRL-B - scroll window backwards
  • CTRL-D - scroll window half-down
  • CTRL-U - scroll window half-up
  • CTRL-Y - scroll the window down N lines
  • CTRL-E - scroll the window up N lines
  • CTRL-W CTRL-W - jump between windows

Deleting

  • dd - delete line
  • dw - delete until the start of the next word
  • de - deleted to the end of the current word
  • d$ - delete to end of line

Typing a number before the command, repeats it that many times.

To delete columns, use CTRL-V to start visual block selection, then d to delete the selected block.

Common Commands

  • :e - edit a file
  • :w - save file
  • :w [filename] - save file as [filename]
  • :q - quit (close window)
  • :q! - quit without saving
  • :qa! - Exit Vim entirely abandoning all changes
  • :x or :wq - save and quit
  • :r [filename] insert contents of [filename]
  • :help
  • :! [command] - execute external shell command - (:help !cmd)
  • /[text] - search for [text]
  • /[text]\c - search for [text] ignoring case
  • n - repeat search
  • N - repeat search in opposite direction
  • ? [text] - search backwards
  • nohlsearch - temporarily disable highlighted search results (abbreviates to noh)
  • CTRL-O - return to previous location, repeat to go back further
  • CTRL-I - jump forward to next location
  • % - jump to matching parenthesis

See also Searching

Swapping Characters, Words and Lines

  • xp - swaps the current character with the next one
  • xP - swaps the current character with the previous one
  • ddp - swaps the current line with the next one
  • ddkP or ddkkp - swaps the current line with the previous one
  • dawwP dawelp - swaps the current word with the next one
  • dawbP - swaps the current word with the previous one

See Vim Tips Wiki—Swapping characters, words and lines for more information.

Substitution

  • :s/old/new - replace first occurrence of old
  • :s/old/new/g - replace all occurrences
  • :#,#s/old/new/g - replace all between specified # line numbers
  • :%s/old/new/g - replace all occurrences in whole file
  • :%s/old/new/gc - prompt whether to replace each occurrence
  • :%s/\s+$//e - remove all trailing whitespace from file. See Remove unwanted spaces

Word Wrapping

  • :set textwidth=78
  • gqap - format paragraph
  • gq$ - format to end of line
  • gqG - format paragraphs to end of file

Windows

  • :sp[lit] [filename] - split the window in two, optionally editing the specified file in a new window
  • CTRL-W s - split window horizontally
  • CTRL-W v - split window vertically
  • CTRL-W j - move to the window below this one
  • CTRL-W k - move to the window above this one
  • CTRL-W w - switch between windows
  • CTRL-^ - switch back to the previous buffer
  • CTRL-W c - close the current window (like :close)
  • CTRL-W q - quit the current window (like :quit)
  • CTRL-W = - make all windows the same size
  • CTRL-W - - decrease the current window size by N
  • CTRL-W + - increase the current window size by N
  • CTRL-W o - make the current window the only one on the screen
  • CTRL-W r - rotate windows downwards/rightwards
  • CTRL-W x - exchange the current window with the next

Buffers

  • :e[dit] [filename] - edit the specified file
  • :buffers or :ls - list buffers
  • :buffer or :b 2 - switch to buffer 2
  • :b# - switch to previous buffer
  • :buffer or :b [partial-filename] - switch to buffer matching partial-filename
  • :bn[ext] - switch to the next buffer
  • :bp[revious] - switch to the previous buffer
  • :bun[load] - unload the current buffer
  • :set hidden - allow switching buffers without saving
  • :badd filename - add a file to a buffer without opening it
  • :bdelete 2 - delete buffer 2
  • :bdelete partial-filename - delete buffer matching partial-filename

Tabbed Pages

See https://stackoverflow.com/questions/53664/how-to-effectively-work-with-multiple-files-in-vim

  • :tabe[dit] [filename] - open the specified filename in another tab
  • :tabn[ext] - switch to the next tab
  • :tabp[previous] - switch to the previous tab

Misc

  • v - visual selection (:help visual-mode)
  • CTRL-v - visual block mode (:help visual-block)
  • . - repeat the last change command
  • gUw - upper case to end of word
  • guw - lower case to end of word
  • g~w - switch case to end of word
  • gUU - upper case entire line
  • guu - lower case entire line
  • g~~ - switch case of entire line
  • fx - find character x (can combine with movement commands)
  • Fx - find character x, searching to the left
  • tx - go to character x (stops one short)
  • Tx - go left to character x (stops one short)
  • `` - go back to position prior to last jump
  • CTRL-O - Jump to older postion
  • CTRL-I - Jump to newer position
  • ma - mark position as "a"
  • `a - jump to the "a" mark
  • 'a - jump to the beginning of the line containing the "a" mark
  • :le [indent] - left align to [indent], default 0
  • :%le [indent] - left align to [indent] with the range set to all lines, a quick way to remove all leading whitespace
  • :marks - List the marks
  • :help q - Help on recording macros into a register
  • :help x - Get help on the "x" command
  • :help
  • :help help
  • :help user-manual
  • :help scroll-cursor
  • :help ruler
  • :help spell
  • :set background=dark - if colours hard to see on dark background
  • :set background=light - if colours hard to see on light background
  • :read filename - read a file (:help :read)
  • :read !ls -l ~ - reads the output of the command into the buffer

Options

  • :help options
  • :set ic - ignore case in searches
  • :set noic - disable ignore case
  • :set hls is - set hlssearch (highlight matching phrases) and incsearch (show partial matches) during searches
  • :set tw - set textwidth, e.g. :set tw=78
  • :set ts - set tabstop, e.g. :set ts=4
  • :set sts - set softtabstop, e.g. :set sts=0 to disable inserting spaces when tabbing
  • :set sw - set shiftwidth, e.g. :set sw=4 to set the number of spaces for autoindent
  • :set noet - set noexpandtab to disable replacing tabs with spaces
  • :set list - show tabs as CTRL-I and end-of-line as $ - useful to see issues with tabs and trailing spaces
  • :set nolist - disable showing tabs as CTRL-I and end-of-line as $
  • :help > - help on indending blocks

Sorting Lines

Use visual block mode to select the lines being sorted, then execute the external sort command :!sort. See :help usr_12.txt and navigate to section 12.3, 'Sort a list' for more information.

Recovering After a Crash

Start Vim on the file that suffered the crash with the -r option:

$ vim -r \[filename\]

Modelines

Modelines can be used to automatically set options. e.g.

vim: set tw=78 ts=4 sts=0 sw=4 noet ft=markdown norl:

See :help auto-setting and :help modeline. On a Debian system this is disabled by default due to security concerns. You will need to add set modeline in ~/.vimrc to enable modelines.

Plugins

Swift

See Also:


-- Frank Dean - 17 Dec 2019

Related Topics: CreatingDocuments, DocumentFormats, EmacsTips, LaTeX, Markdown, PanDoc