Home > Wiki > Vim Articles

All articles are licensed under CC0 1.0 making them public domain.

My config #

Last updated February 2021

Save as "~/.vimrc".

NOTE: The config assumes a light terminal (like the default on macOS), if you use a dark terminal switch to set background=dark in the file. I also change this on the fly using the Vim prompt when dark mode is toggled on my Mac.

syntax on
set tabstop=2
set shiftwidth=2
set softtabstop=2
set expandtab
set autoindent
set number
set backspace=indent,eol,start
set noswapfile

set splitbelow
set splitright

set background=light
set belloff=all
set spell spelllang=en_us

set smartcase

Movement #

Last updated February 2021

Moving the cursor

  • Arrow keys
  • k (up) / j (down) / h (left) / l (right)

Moving the cursor quickly on a line

  • Move one word forward - w
  • Move one word backwards - b

Move to line x

  • xgg
  • :x

Moving to significant positions in a file

  • Moving to the start of a file - gg
  • Moving to the end of a file - G

Moving to significant positions on a line

  • Moving to the start of the line - 0
  • Moving to the end of the line - $

Position markers

Position markers can help you keep track of positions within a file.


Tabs #

Last updated February 2021

Open a new tab

  • :tabnew filename
  • vim -p file1 file2 file3

Switching between tabs

  • :tabnx
  • gt in normal mode
  • :tabfirst / :tablast

Move a tab

  • :tabmx

Close a tab

  • :tabclose
  • :tabclosex
  • :q

Splits #

Last updated February 2021

Opening a split

  • Open a horizontal split - :split file
  • Open a vertical split - :vsplit file

Moving in a split

  • Use ctrl-w combined with any movement key, e.g. ctrl-w [right arrow]

Resizing splits

  • Resize the active split - :resize vertical/horizontal x
  • Equal the size of splits - ctrl-w =

Closing a split

  • Close a split like any file - :q

Searching #

Last updated February 2021

Making a Search

  • Search forward with /search term
  • Search backwards with ?search term

Navigating Matches

  • Use n to go to the next match
  • Use N to go to the previous match

Case sensitivity

By default searches are case sensitive, this can be disabled with :set ignorecase and then enabled once again with :set noignorecase.


Replacing #

Last updated February 2021

NOTE: This document is written like a tutorial. It's recommended by the author you read it in sequence.

Template for replacing looks like the following:

:[range]s/{pattern}/{string}/[flags] [count]

Replacing on the current line

  • Replacing the first occurrence - :s/find/replace/
  • Replacing all occurrences - :s/find/replace/g

Replacing throughout the whole document

The commands used for replacing on the current line apply by adding prefixing the range with a %. For example this will replace all occurrences throughout the document:

:%s/find/replace/g

Flags

Placed at the end of the command. They stack, for example: gic.

  • g - Will replace all occurrences in range.
  • i - Case insensitive search, without search is case sensitive by default.
  • c - Step through each replace, use y to replace, l to replace and match, n to skip, q to quit the replacements, a to replace the rest without confirmation.

Indentation #

Last updated February 2021

Indenting a selection

  1. Enter visual mode with key V
  2. Use key > or < for increased or decreased indentation
  3. Use key . to repeat

Reindenting selection or whole document

The key = will reindent the selection. The key combination gg=G will reindent the whole document, '' will then return to last position.

Reindenting isn't working in a specific file type

Enabling indenting for the specific file type is required in some cases. Enter the following commands into Vim:

:filetype indent on
:set filetype=html
:set smartindent

Position markers #

Last updated February 2021

Position markers allow you mark a position within a document and quickly jump back to the exact position or line.

  1. mx will create a marker at your cursor's position assigned to key x.
  2. `x will take you back to the exact position, 'x will take you to the line of the marker.

Managing markers

All your markers can be listed with :marks. A single marker can be deleted with :delmarks x or d'x.


Surround line(s) with HTML/XML tags #

Last updated February 2021

  1. Download and install vim-surround.
  2. Select line with visual block mode.
  3. Type :norm yss<p><CR>, in this example a paragraph tag is used.