I really love vim. They say people struggle to quit vim, but I can't quit using vim – it's too addictive! <audience groaning intensifies>
I’ve been using vim since I started using linux back in the knoppix 3.x days and I wanted to share a quick list of vim related tips. I’ll start off easy and get more advanced as I go.
Basic
Opening Vim:
To open Vim in your terminal, simply type
vim
. To open a specific file, typevim filename
.
Vim modes:
Vim has several modes, but the most important ones are Normal mode, Insert mode, and Command-line mode.
Normal mode: The default mode when you start Vim. You can use commands and navigate the text without editing it.
Insert mode: In this mode, you can insert text like a regular text editor. To enter Insert mode, press
i
in Normal mode.Command-line mode: Used to execute commands, save files, and more. Press
:
in Normal mode to enter Command-line mode.
Basic navigation:
In Normal mode, you can navigate using the following keys:
h
- move leftj
- move downk
- move upl
- move right You can also use the arrow keys, but usingh
,j
,k
,l
allows you to keep your hands on the home row.
Inserting text:
Press
i
to enter Insert mode and type as you would in any other text editor. To exit Insert mode and return to Normal mode, pressEsc
.
Saving and quitting:
In Normal mode, press
:
to enter Command-line mode, then:To save a file, type
w
and pressEnter
.To quit without saving, type
q
and pressEnter
.To save and quit, type
wq
and pressEnter
.To force quit without saving, type
q!
and pressEnter
.
Basic editing:
In Normal mode, you can perform various editing operations, such as:
dd
- delete the current lineyy
- yank (copy) the current linep
- paste the yanked text after the current lineu
- undo the last changeCtrl + r
- redo the last undone change
Searching:
In Normal mode, you can search for text:
/search_term
- search forward for the term?search_term
- search backward for the termn
- move to the next occurrence of the search termN
- move to the previous occurrence of the search term
Pro
Visual block selection:
To select a block of text, first press
Ctrl + v
in Normal mode to enter Visual Block mode. Then, move the cursor to define the rectangular selection. You can useh
,j
,k
,l
, or the arrow keys to move the cursor.I really really really really love visual block mode. Block editing in Vim can be powerful and efficient when working with columns or multiple lines.
Inserting text:
With a visual block selected, press
I
to insert text before the selected block. Type the text you want to insert, then pressEsc
. Vim will insert the text on each line of the block.
Appending text:
Similarly, with a visual block selected, press
A
to append text after the selected block. Type the text you want to append, then pressEsc
. Vim will append the text on each line of the block.
Deleting a block:
To delete the selected block of text, press
d
orx
in Visual Block mode. This will delete the rectangular selection.
Changing text:
To change the selected block of text, press
c
in Visual Block mode. This will delete the block and enter Insert mode, allowing you to type new text. After typing, pressEsc
to apply the change to all lines in the block.
Indenting and dedenting:
To indent or dedent a visual block, select the block in Visual Block mode and press
>
to indent or<
to dedent. You can also prefix these commands with a number to specify the number of indentation levels, like3>
for three levels of indentation.
Columnar search and replace:
Perform a search and replace operation within the selected visual block by first selecting the block and then typing
:s/search/replace/g
. PressEnter
to execute the command.
Advanced
Macros:
Macros allow you to record a series of keystrokes and replay them to automate repetitive tasks. To record a macro, press
q
followed by a register name (a lowercase letter) in Normal mode, e.g.,qa
. Perform the desired actions, then pressq
again to stop recording. To replay the macro, press@
followed by the register name, e.g.,@a
.
Split windows:
Vim supports split windows, allowing you to view and edit multiple files or different parts of the same file simultaneously. Use the following commands to manage split windows:
:split
or:sp
- open a horizontal split:vsplit
or:vs
- open a vertical splitCtrl + w
followed byh
,j
,k
, orl
to navigate between splits
Tab pages:
Tab pages are another way to work with multiple files. Each tab page can contain multiple split windows. Use the following commands to manage tab pages:
:tabnew
or:tabe
- open a new tab pagegt
or:tabn
- go to the next tab pagegT
or:tabp
- go to the previous tab page:tabc
- close the current tab page
Folding:
Folding allows you to collapse and expand sections of code or text, making it easier to navigate and manage large files. There are several folding methods available in Vim, including manual, syntax-based, and expression-based folding. Use the following commands to manage folds:
za
- toggle the fold at the cursorzR
- open all foldszM
- close all foldszj
- move to the next foldzk
- move to the previous fold
Text objects:
Text objects allow you to operate on specific portions of text, such as words, sentences, paragraphs, or blocks. They can be used with commands like
d
,c
,y
, andv
. Examples of text objects include:aw
- a wordas
- a sentenceap
- a paragrapha(
ora)
- a block enclosed by parenthesesa[
ora]
- a block enclosed by square brackets
Command-line window:
The command-line window allows you to edit and navigate command-line history. Access it by pressing
q:
in Normal mode. You can edit and execute previous commands or search through your command history.
Persistent undo:
Vim can store your undo history across sessions, allowing you to undo and redo changes even after closing and reopening a file. To enable this feature, add the following lines to your
.vimrc
file:set undofile
set undodir=~/.vim/undodir
Ensure that the
undodir
directory exists or create it if necessary.
vimspert
Omni completion:
Vim offers context-aware autocompletion through omni completion. Press
Ctrl + x
followed byCtrl + o
in Insert mode to trigger omni completion. For programming languages, you may need to install additional plugins or configure Vim to provide relevant suggestions.
Quickfix list and location list:
Vim's quickfix list stores errors, warnings, or search results from various sources, such as compilers or grep-like tools. The location list is similar but specific to individual windows. Use the following commands to work with quickfix and location lists:
:copen
- open the quickfix window:cclose
- close the quickfix window:cnext
- move to the next item in the quickfix list:cprev
- move to the previous item in the quickfix list:lopen
,:lclose
,:lnext
,:lprev
- same commands for location lists
Vim sessions:
Vim sessions allow you to save your current editing environment, including open files, window layout, and options, so you can quickly restore it later. Use the following commands to manage sessions:
:mksession session_name.vim
- save the current session:source session_name.vim
- restore a saved session
Marks:
Marks are used to bookmark specific positions in a file or across multiple files. Lowercase marks are local to a file, while uppercase marks are global. Use the following commands to work with marks:
m{a-zA-Z}
- set a mark at the cursor position, e.g.,ma
ormA
'{a-z}
or '{a-z}
- jump to a lowercase mark in the current file'{A-Z}
or '{A-Z}
- jump to an uppercase mark in any file
Mappings:
Mappings allow you to define custom keybindings or modify existing ones in Vim. You can create mappings for different modes using commands like
:map
,:nmap
,:imap
,:vmap
, etc. For example, to mapjj
toEsc
in Insert mode, add the following line to your.vimrc
file:inoremap jj <Esc>
Registers:
Registers are storage locations that can hold text, macros, or other data. Vim has multiple registers, such as unnamed, named, numbered, read-only, and expression registers. To access or manipulate registers, use the following commands:
"{a-z0-9}%#:-=.
- specify a register, e.g.,"ayy
to yank the current line into registera
:reg
or:registers
- display the contents of all registers:echo @a
- display the contents of a specific register, e.g., registera
Plugins:
Vim has a vibrant plugin ecosystem that can extend its functionality. Popular plugin managers include Vim Plug, Pathogen, and Vundle. Some notable Vim plugins are NERDTree (file explorer), CtrlP (fuzzy file finder), and YouCompleteMe (code completion).
Checkout
https://vimawesome.com/ for some truely epic plugins.
Conclusion
This is just a brief introduction to using Vim. There are many more commands, features, and customizations available. To learn more about Vim, you can refer to its extensive documentation by typing vimtutor
in the terminal! And NO! I won’t tell you how to quit vim, its too good to stop using.
Pressing ESC in vim is akin to repeatedly nudging the gearstick to ensure you're in neutral
:qa! = get me the hell out of here