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:
| Key | Description |
|---|---|
| i | Insert text before the cursor |
| a | Append text after the cursor |
| I | Insert at the beginning of the current line |
| A | Append at the end of the current line |
| o | Open a new line below the cursor and enter Insert mode |
| O | Open a new line above the cursor and enter Insert mode |
While in Insert mode, the following shortcuts are available:
| Key binding | Description |
|---|---|
| Ctrl+w | Delete the word before the cursor |
| Ctrl+u | Delete from cursor to line beginning |
| Ctrl+t | Indent the current line |
| Ctrl+d | Outdent 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:
| Key | Description |
|---|---|
| v | Visual character mode |
| V | Visual line mode |
| Ctrl+v | Visual 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:
- Enter Visual block mode: Ctrl+v
- Select the first column of the target lines using arrow keys.
- Enter Insert mode: Shift+i
- Type the comment character, e.g.,
# - Press Esc.
Un-commenting multiple lines:
- Enter Visual block mode: Ctrl+v
- Select the comment character(s) across lines using arrow keys.
- Press x to delete the selection, then Esc.
Saving and Exiting
First return to Normal mode with Esc, then use any of the following:
| Command | Description |
|---|---|
:w | Save the file |
:q | Quit VIM |
:wq | Save 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.
Navigation
| Key binding | Description |
|---|---|
| gg | Go to the beginning of the file |
| G | Go to the end of the file |
| 5G | Go to line 5 (replace 5 with any line number) |
| 5H | Move to 5 lines below the top of the screen |
| 3L | Move to 3 lines above the bottom of the screen |
| Ctrl+f | Scroll forward one page |
| Ctrl+b | Scroll backward one page |
| $ | Go to the end of the line |
| 0 | Go to the beginning of the line |
| w | Move one word forward |
| b | Move one word backward |
Editing
| Key binding | Description |
|---|---|
| db | Delete one word backward |
| dw | Delete one word forward |
| d$ / D | Delete to the end of the line |
| d0 | Delete to the beginning of the line |
| dd | Delete the whole line |
| 5dd | Delete 5 lines below the cursor |
| dG | Delete all lines from cursor to end of file |
| dgg | Delete all lines from cursor to beginning of file |
| cc | Clear the whole line and enter Insert mode |
| cG | Clear all lines below cursor and enter Insert mode |
| cgg | Clear all lines above cursor and enter Insert mode |
| u | Undo (also :u; use :2u to undo twice) |
| U | Undo all line changes (itself undoable with u) |
| Ctrl+r | Redo |
| Ctrl+l | Redraw/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
| Command | Description |
|---|---|
:set nu | Show line numbers |
:set nonu | Hide line numbers |
Spell Checking
| Command | Description |
|---|---|
:set spell | Enable spell checking |
:set nospell | Disable spell checking |
:set spellfile=~/.vim/en.utf-8.add | Set a custom user spell file |
| z= | Show spelling suggestions |
| zg | Add word under cursor to dictionary |
| zug | Remove 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.