The current standard for editing text is to use a graphical tool (Sublime Text, gedit, Word).
You press a key, text appears, Backspace deletes a character, and you move around either with the arrow keys or with a mouse click.
Nothing groundbreaking so far.
Vim completely changes the paradigm and is bewildering to newcomers.
So why bother learning a new way to edit text?
Simple: you won't always have access to a graphical tool.
If you work on servers, you'll find yourself in the dark world of the terminal and the command line. Better to have a few weapons in hand when that day comes!
In Vim you have 2 modes (3 in reality, but we'll come back to that):
- EDIT [ESC] : I manipulate existing text
- INSERT [i] : I type text (nothing new under the sun)
This is exactly where Vim shines: its edit mode!
It would be too easy to use the arrow keys! Instead, we use:
- ↑ k : up
- ↓ j : down
- ← h : left
- → l : right
- → e : end of word
- → E : next whitespace / end of line
- ← b : beginning of word
- ← B : previous whitespace / beginning of line
- :[NUM] : go to line [NUM]
There are two main commands for modifying existing text:
- d : delete -> stays in edit mode
- c : change -> switches to insert mode
Having these commands is great, but how do you use them? With a context or an object!
Contexts:
- i : inside
- t : till
Objects:
- w : word
- p : paragraph
- [KEY] : any arbitrary character
A few examples:
- di{ : deletes everything inside the next {}
- cw : change the current word
- d[KEY1][KEY2] : delete from KEY1 up to KEY2
Let's say you want to modify this code:
function main(){
// main prints hello world
console.log("hello world")
}
Place your cursor here:
function main(){
// main prints hello world
->> console.log("hello world")
}
A quick di{ or di} and — magic:
function main(){
}
Still here? In that case, here is a list of commands that will let you switch between edit and insert mode and speed up your work in Vim:
- o : insert a new line below the cursor
- G : go to the end of the file
- $ : go to the end of the line
- { or } : jump to the beginning or end of a paragraph (text block with no blank line)
- CTRL+(u/d) : scroll half a screen height (up, down) d’une demie hauteur d’écran (up, down)
I made a mistake! → no worries:
- u : undo (equivalent to CTRL+Z)
- CTRL+r : redo
I'm fed up, delete everything!
- cc / dd : delete an entire line (with or without switching to INSERT mode)
- d[number]d : delete [number] lines
Copy and paste:
- y : yank/copy (same usage as c/d commands)
- p : paste (same usage as c/d commands)
On the internet you can find an incredible number of methods for exiting Vim.
For example: unplug your computer → effective but impractical.
Here is the proper method!
- :w : write (i.e., save)
- :q : quit (finally!)
- :wq : save and quit
- :q! : quit and discard all changes
As mentioned earlier, there is a 3rd mode in Vim: Visual mode [v], which lets you select text as you move the cursor.
There are actually three variants of Visual mode:
- v : character-wise visual mode — select text character by character
- V : line-wise visual mode — select entire lines at once
- CTRL+v : block visual mode — select a rectangular block of text (column editing)
Once you have a selection, you can apply commands to it:
- y : yank (copy) the selection
- d : delete the selection
- c : change the selection (deletes it and enters insert mode)
- > : indent the selection
- < : un-indent the selection
Imagine you want to add // at the beginning of several lines to comment them out.
Place your cursor on the first line, press CTRL+v to enter block mode, then move down with j to select the lines, press I (capital i) to insert at the beginning of the block, type //, and press ESC — done!
// const a = 1
// const b = 2
// const c = 3
You can search through text using several commands:
- f[key] : jump to the first occurrence of [key] on the current line
- ; : next occurrence
- , : previous occurrence
- /[foo] : search for a pattern across the whole file
- n : next match
- N : previous match
Vim has a powerful substitution command that works like a find-and-replace:
:s/old/new/ : replace the first occurrence of "old" with "new" on the current line
:s/old/new/g : replace all occurrences on the current line
:%s/old/new/g : replace all occurrences in the entire file
:%s/old/new/gc : replace all occurrences in the file, asking for confirmation each time
The % means "the whole file", and the g flag means "global" (all occurrences on each line, not just the first).
You have a file that uses foo everywhere and you want to rename it to bar:
:%s/foo/bar/gc
Vim will highlight each match and ask: replace with bar (y/n/a/q/l)?
y→ yes, replace this onen→ no, skip this onea→ all, replace all remaining without askingq→ quit substitution
Marks let you bookmark a position in a file and jump back to it instantly.
- m[a-z] : set a mark at the current cursor position (e.g., ma, mb, mc…)
- `[a-z] : jump to the exact line and column of mark [a-z]
- '[a-z] : jump to the beginning of the line of mark [a-z]
- `` : jump back to the position before your last jump
- '' : jump back to the line before your last jump
You're deep inside a long file editing one function, but you need to quickly check another function far below.
- Press
mato mark your current position. - Navigate to the other function and make your changes.
- Press
`ato teleport straight back to where you were.
Macros let you record a sequence of keystrokes and replay them as many times as you like — perfect for repetitive edits.
- q[a-z] : start recording a macro into register [a-z] (e.g., qa)
- q : stop recording (press q again when done)
- @[a-z] : replay the macro stored in register [a-z]
- @@ : replay the last macro again
- [N]@[a-z] : replay the macro [N] times (e.g., 10@a)
You have a list of plain words and you want to wrap each one in quotes:
apple
banana
cherry
- Place your cursor on
apple. - Press
qato start recording into registera. - Press
Ito insert at the beginning, type", pressESC. - Press
Ato append at the end, type", pressESC. - Press
jto move to the next line. - Press
qto stop recording. - Press
2@ato replay the macro on the next 2 lines.
Result:
"apple"
"banana"
"cherry"
Vim lets you split your screen to view and edit multiple files at the same time.
- :split [file] (or :sp) : open a horizontal split
- :vsplit [file] (or :vsp) : open a vertical split
- :new : open a new empty horizontal split
- :vnew : open a new empty vertical split
If you don't provide a filename, the split opens with the current file.
- CTRL+w w : cycle to the next split
- CTRL+w h/j/k/l : move left / down / up / right between splits
- CTRL+w = : make all splits equal size
- CTRL+w +/- : increase or decrease the height of the current split
Every file you open in Vim is loaded into a buffer. You can have many buffers open even without splits.
- :ls (or :buffers) : list all open buffers
- :b [N] : switch to buffer number [N]
- :b [name] : switch to a buffer by partial filename
- :bn : go to the next buffer
- :bp : go to the previous buffer
- :bd : close (delete) the current buffer
Open two files side by side to compare them:
:vsplit other_file.js
Then use CTRL+w l and CTRL+w h to jump between the left and right panes.
Vim supports a huge range of plugins and settings through its .vimrc configuration file.
Here are some useful articles and plugins to explore:
-
Split windows - https://vim.works/2019/04/10/split-window-aesthetics/
-
Distraction-free writing - https://www.hamvocke.com/blog/distraction-free-writing/