Linux

vim

Vi IMproved, a highly configurable text editor.

#linux #editor #text

Modes

Vim is a modal editor. Understanding which mode you are in is crucial.

ModeDescription
NormalDefault mode. Used for navigation and simple editing. Press Esc to return here from any mode.
InsertFor explicitly typing and modifying text. Press i to enter.
VisualFor highlighting/selecting text. Press v to enter. Use arrow keys to extend/reduce selection.
Command LineFor executing commands (Saving, Exiting, splitting). Press : from Normal mode to enter.

Opening, Closing, and Saving

CommandDescription
vim FILE_NAMEOpen or create a file in Vim from your terminal.
:wSave file.
:qQuit.
:wq or :x!Save modifications and quit.
:q! or :ZQQuit the file without saving (force quit).

Navigation (Normal Mode)

KeyAction
h j k lLeft, Down, Up, Right (keeps hands on the home row).
wJump forward to start of word.
bJump backward to start of word.
0Start of line.
$End of line.
ggGo to first line.
GGo to last line.
:LINE_NUMBERJump to a specific line number (e.g., :42).
:$Jump to the last line (Command Line Mode equivalent to G).

Basic Editing

KeyAction
iInsert text before the cursor.
aAppend text after the cursor.
oInsert a new line below current line and switch to Insert Mode.
OInsert a new line above current line and switch to Insert Mode.
xDelete character under the cursor.
ddDelete the current line or highlighted text.
y or yyCopy (yank) highlighted text or the current line.
pPaste previously copied/deleted text after cursor.
rReplace a single character.
uUndo last action.
Ctrl + rRedo last action.

Search and Replace

CommandDescription
/patternSearch forward for pattern (e.g., :/SEARCH_KEYWORD).
nJump to the next match.
NJump to the previous match.
:%s/old/new/gReplace all occurrences of old with new throughout the file.

Split Mode (Window Management)

Vim allows you to view multiple files simultaneously by splitting your terminal view.

CommandDescription
:split FILE_NAMEHorizontally open another file while one is already open.
:vsplit FILE_NAMEVertically open another file while one is already open.
Ctrl + w wJump between different split windows.
:qClose the currently active split window.

Advanced Power-User Tricks

Combining Commands (The "Vim Language")

Vim commands act like a language (Verb + Noun).

  • d (Delete) + w (Word) = dw (Deletes the current word).
  • c (Change) + w (Word) = cw (Deletes word and drops you into Insert mode).
  • d (Delete) + $ (End of line) = d$ (Deletes everything from cursor to end of line).
  • y (Yank) + p (Paste) + p = ypp (Duplicates the current line).

Visual Block Mode

Want to edit multiple lines at the exact same time?

  1. Press Ctrl + v to enter Visual Block Mode.
  2. Select multiple lines using j or k.
  3. Press Shift + I (Capital I), type your text (it will only appear on the first line).
  4. Press Esc and the text will magically propagate to all selected lines!

Essential Configuration (~/.vimrc)

Create or edit ~/.vimrc to make Vim much more modern by default:

" Show line numbers (Default for navigation)
set number

" Show relative line numbers (Makes jumping with '10j' or '5k' incredibly easy)
" set relativenumber

" Enable syntax highlighting
syntax on

" Highlight search results dynamically as you type
set hlsearch
set incsearch

" Convert tabs to 4 spaces
set tabstop=4
set shiftwidth=4
set expandtab

" Enable mouse support for resizing splits and clicking cursor
set mouse=a