Skip to main content

VI or VIM editor

VI/VIM is a simple and powerful editor. If you are accessing a remote workstation or cluster where you don't have the option to launch graphical programs, VIM is a great option. Almost any Linux or UNIX system has VI preinstalled. Recent OS releases will likely have VIM (Vi IMproved) instead of the classic VI editor. You can check the version by:

vi --version

If it is not installed, you can use a package manager:

# Ubuntu / Debian
apt install vim

Open or create a file to read and/or write:

vi file.txt

Modes

VIM is a modal editor — understanding its modes is the key to using it effectively.

Normal (Read-only) Mode

This is the default mode when you open VIM. Press Esc at any time to return to Normal mode. All keyboard shortcuts in the Keyboard Shortcuts section apply here.

Insert Mode

Press i to enter Insert mode. You will see -- INSERT -- at the bottom of the window. Other ways to enter Insert/editing mode:

KeyDescription
iInsert text before the cursor
aAppend text after the cursor
IInsert at the beginning of the current line
AAppend at the end of the current line
oOpen a new line below the cursor and enter Insert mode
OOpen a new line above the cursor and enter Insert mode

While in Insert mode, the following shortcuts are available:

Key bindingDescription
Ctrl+wDelete the word before the cursor
Ctrl+uDelete from cursor to line beginning
Ctrl+tIndent the current line
Ctrl+dOutdent the current line

Replace Mode

Press Shift+r to enter Replace mode. You will see -- REPLACE -- at the bottom of the window. Characters you type overwrite existing text.

Visual Mode

Enter Visual mode from Normal mode using one of:

KeyDescription
vVisual character mode
VVisual line mode
Ctrl+vVisual block mode

Select text using the arrow keys or j/k. Once selected, you can:

  • Press c to clear the selected text and enter Insert mode.
  • Press d to delete the selected text.
  • Press y to yank (copy) the selected text.
  • Press p to paste yanked text at another location in VIM.
  • Press "+y (or "*y on macOS) to copy to the system clipboard, then paste in another program with Ctrl+v (or Command+v on macOS).

Deleting multiple lines: Enter Visual line mode with Shift+v, select lines with arrow keys or j/k, then press d to delete or c to clear and enter Insert mode.

Commenting multiple lines:

  1. Enter Visual block mode: Ctrl+v
  2. Select the first column of the target lines using arrow keys.
  3. Enter Insert mode: Shift+i
  4. Type the comment character, e.g., #
  5. Press Esc.

Un-commenting multiple lines:

  1. Enter Visual block mode: Ctrl+v
  2. Select the comment character(s) across lines using arrow keys.
  3. Press x to delete the selection, then Esc.

Saving and Exiting

First return to Normal mode with Esc, then use any of the following:

CommandDescription
:wSave the file
:qQuit VIM
:wqSave and quit (also Shift+ZZ)
:q!Quit without saving (also Shift+ZQ)
:w !sudo tee %Save a file opened without root privileges

Keyboard Shortcuts

All shortcuts below apply in Normal mode. Press Esc to ensure you are in Normal mode before using them.

Key bindingDescription
ggGo to the beginning of the file
GGo to the end of the file
5GGo to line 5 (replace 5 with any line number)
5HMove to 5 lines below the top of the screen
3LMove to 3 lines above the bottom of the screen
Ctrl+fScroll forward one page
Ctrl+bScroll backward one page
$Go to the end of the line
0Go to the beginning of the line
wMove one word forward
bMove one word backward

Editing

Key bindingDescription
dbDelete one word backward
dwDelete one word forward
d$ / DDelete to the end of the line
d0Delete to the beginning of the line
ddDelete the whole line
5ddDelete 5 lines below the cursor
dGDelete all lines from cursor to end of file
dggDelete all lines from cursor to beginning of file
ccClear the whole line and enter Insert mode
cGClear all lines below cursor and enter Insert mode
cggClear all lines above cursor and enter Insert mode
uUndo (also :u; use :2u to undo twice)
UUndo all line changes (itself undoable with u)
Ctrl+rRedo
Ctrl+lRedraw/reload the screen
.Repeat the last command

Moving Lines

To move a line up or down, use these key combinations in Normal mode:

  • Move line up: dd + k + P
  • Move line down: dd + j + P

How it works: dd deletes the current line into the default register; k (up) or j (down) moves the cursor; P pastes the line above the new cursor position.

Searching

Type / followed by the phrase you are looking for and press Enter. Press n to jump to the next match, or Shift+n to go to the previous match. Search backwards by starting with ? instead of /.

Command-line Options and Settings

Line Numbers and Display

CommandDescription
:set nuShow line numbers
:set nonuHide line numbers

Spell Checking

CommandDescription
:set spellEnable spell checking
:set nospellDisable spell checking
:set spellfile=~/.vim/en.utf-8.addSet a custom user spell file
z=Show spelling suggestions
zgAdd word under cursor to dictionary
zugRemove word from the dictionary

File Explorer

Browse files and folders using VIM's built-in file explorer:

:E

Comparing Files

Open two files side-by-side to compare differences:

vi -d file.txt file_edit.txt

Getting Help

:help gg
:help CTRL-V

vimrc

Customize your editor by creating a ~/.vimrc file. Below is a sample configuration with commonly used settings:

syntax on
set tabstop=4
set ruler
colorscheme darkblue
set textwidth=80
set cc=81
set autoindent
set spell
hi SpellBad ctermbg=Yellow
hi SpellBad ctermfg=DarkRed
set spellfile=~/.vim/en.utf-8.add

" Syntax highlight for custom file types
au BufRead,BufNewFile *.in set filetype=fortran
au BufRead,BufNewFile *.out set filetype=fortran
au BufRead,BufNewFile matplotlibrc set filetype=python

set hlsearch
hi Search ctermbg=LightBlue
hi Search ctermfg=black

Tabs and Indentation

Convert tabs to spaces by adding the following to your .vimrc:

set tabstop=2 shiftwidth=2 expandtab

Then convert any existing tabs in an open file with:

:retab

To set a different tab width for a specific file type:

aug python
" ftype/python.vim overwrites this
au FileType python setlocal ts=4 sts=4 sw=4 noexpandtab
aug end

Plugins

VIM's functionality can be extended with plugins. The popular plugin manager vim-plug makes installation and management straightforward.

Installing vim-plug

curl -fLo ~/.vim/autoload/plug.vim --create-dirs \
https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim

Adding Plugins

Add a plug block to your .vimrc:

call plug#begin('~/.vim/plugged')
Plug 'junegunn/goyo.vim'
call plug#end()

Save the file with :w, then install the listed plugins:

:PlugInstall

Customizing a Plugin

You can modify or add settings to an installed plugin directly. For example, to automatically activate Limelight alongside Goyo, open:

~/.vim/plugged/goyo.vim/autoload/goyo.vim

and add Limelight (along with any other settings such as set spell) at the appropriate location in that file.