Vim: what, why and how

Waldir Leoncio

Oslo Centre for Biostatistics and Epidemiology, UiO

2025-10-17

Note

Sit back and relax: these slides will be sent to you after the seminar

(let me know if I forget)

The what and the why

What

  • Vi improved is a 1991 text editor that runs in the terminal (primarily)
  • It is very powerful, but has unusual interface and shortcuts
  • Is the source of many memes and frustration among casual users

“Who’s that lady?”

“Who’s that lady?” “WHO’S THAT LADY?” That’s Margaret F-ing Hamilton, show some respect!

Apollo 11 Guidance Computer source code

6 reasons why

  1. Sometimes you’re on a server without a GUI
  2. Sometimes you don’t have a choice
  3. It is fast even though it doesn’t feel fast
  4. Fewer finger moves = less strain (though you also look less busy)
  5. Vim shortcuts are everywhere!
  6. Why not? You do harder things in your life

How

(a crash-course in however many minutes we have until noon)

Vim quirks

Important

  • Vim shortcuts are case-sensitive: i is not the same as I
  • Uppercase shortcuts are usually a more intense version of the lowercase one

Tip

  • Need help? :help or :h
  • Repeat the last action (not movement!) with .
  • Prefix a command with a number to repeat it that many times: 5j equals j j j j j

Modes: annoying but useful

Comparing a regular editor with Vim

Function Your editor Vim
Move around 4 arrow + mod keys “Every” key (normal mode)
Select text 4 arrow + mod keys “Every” key (visual mode)
Write Char keys Char keys (insert mode)

Changing modes

Mode Key Description
Normal Esc Edit and navigate
Insert i Type text
Visual v Select and edit text
Command : Execute internal commands
External ! Execute external commands

Note

What differentiates Vim from other editors is that Normal mode is the default on Vim, where other editors use “Insert mode” as the default.

Opening, saving, and closing files

Operation Command
Save/write on a file :w
Close/quit (discard changes) :q (:q!)
Write and quit :wq (:x)

Moving around

1976 called, they want their keyboard back

Tip

  • Make your life easier by resting your index fingers on f and j
  • j is mapped to ⬇️ because that’s (arguably) the most frequent direction you’ll be moving

Vim words vs. Vim WORDS

words
alphanumeric sequences (special guest: underscore)
WORDS
sequences of text isolated by whitespace

Examples

ThisIsOneWordAndAlsoOneWORD
^
1

this-is.one-WORD and-this-is-another-WORD
^                ^
1                2

these-@re.seven((words_but0nly1WORD
^    ^ ^ ^^    ^ ^
1    2 3 45    6 7

Moving faster

Operation Key
Move to next word w
Move to the next WORD W
Move back a word b
Move Back a WORD B
Move to the end of a word e
Move to the End of a WORD E

Moving to specific points of a line

Operation Key
find next x fx
Find previous x Fx
go until x tx
go unTil x (backwards) Tx
Repeat last search ;
Undo previous ; ,

Moving to the extreme ends

Operation Key
Start of line 0
First non-whitespace character ^
End of line $
Matching parenthesis/bracket/braces %
Start of file gg
End of file G

Looks weird, but becomes second nature with practice, I promise!

Moving vertically

Operation Key
Go to next/previous sentence (, )
Go up/down a paragraph {, }
Scroll up/down half a screen Ctrl+u, Ctrl+d
Scroll forward/back a full screen Ctrl+f, Ctrl+b
Set cursor to top/zenter/bottom of screen zt, zz, zb
Go to line n nG

Tip

  • :set number/nonumber shows/hides line numbers
  • :set relativenumber/norelativenumber shows/hides relative line numbers

Setting bookmarks

Operation Key
Set bookmark “a” ma
Go to bookmark “a” a
Go to bookmark “a” (exact position) `a

Folding code

Operation Key
open a fold zo
close a fold zc
Reveal document (all folds) zR
Minimize document (all folds) zM

Tip

  • :help foldmethod to customize how folds are created
  • :set foldmethod=syntax or indent if you get “fold not found”

Finding things

Operation Command
Find “text” /text
Find “text” backwards ?text
Find next occurrence of searched text n
Find previous occurrence of searched text N
Find next occurence of text under cursor *
Find previous occurence of text under cursor #

Editing

Replacing things

Operation Command
substitute first text on line with newtext :s/text/newtext
global substitution in the line :s/text/newtext/g
global substitution on 100 % of the file :%s/text/newtext
substitute on the sp.t, next +5 lines :.,+5s/text/newtext
ignore case when substituting :s/text/newtext/i
confirm each substitution :s/text/newtext/c

Tip

  • You can use other separators, like ! instead of /.
  • Feel free to go crazy with regex!
  • See :h :s_flags for more options

Formatting things

Operation Command
Indent >>
Unindent <<
Join lines J
Toggle case g~
Wrap selected text gq

Tip

Always remember the modifiers:

  • Indent the next 3 paragraphs: >3}>
  • Toggle case for the next 3 words: g~3w

Adding things

Entering insert mode like a pro:

Operation Command
Insert text before cursor i
Insert text after cursor (i.e., append) a
Add new line below cursor o
Insert at the first non-whitespace character I
Append to end of line A
Add new line above cursor O

Overwriting things

Operation Key
Delete character at/before cursor x, X
Replace character with a ra
Enter overwrite mode R
Remove and replace character s
Change text c
Replace whole line S
Replace rest of line C
Undo/redo u, Ctrl+r

Tip

xp swaps two characters (useful for typos!)

Cut, copyDelete, yank, and paste

Why go Ctrl+x/c/v when you can go dyp?

Operation Key
delete d, D
yank y, Y
paste p, P

Tip

Always remember the modifiers:

  • Delete two words: d2w
  • Delete previous sentence: d(
  • Yank current paragraph: y}

Selecting things

Operation Command
Visual (character) mode v
Visual line mode V
Visual block mode Ctrl+v

Tip

  • Select text inside parentheses: vi(
  • Select text inside parentheses and parentheses: va(

ClipboardRegisters

Everything you cut, copy or delete is stored in some register.

Register Description
Default (internal, unnamed) register
0 The last yanked text
1-9 The last 9 deleted lines
a-z, A-Z Exclusive registers just for you

Tip

  • :reg shows all registers
  • :help registers for a detailed list of all Vim registers

System clipb… register!

Register Description
+ or * System clipboard (X11, macOS)

Warning

System clipboard is not available in all Vim installations. Look for a + on vim --version | grep clipboard.

Using registers

Type followed by the register name and the command:

Warning

Don’t confuse the command with the " register!

  • p is the same as p
  • Yes, it’s redundant
  • +y to yank to system clipboard
  • +p to paste from system clipboard
  • 0p to paste the last yanked text

Tip

Pasting gone wrong? Check what’s on the 0 register with :reg 0

Macros

For those times when you need to repeat multi-step operations that are too large for ..

Operation Command
Record macro on register “a” qa
Stop recording macro q
Play macro on register “a” @a
Replay last macro @@

Bonus content

Configuring Vim

  • set command (temporary)
  • ~/.vimrc file (persistent)

Vim shortcuts on other software

  • Vimium (browser extension)
  • VSCodeVim 💣
  • Custom keybindings for your OS (good luck! 💣)
  • Command line shells with Vim mode (fish, zsh)
  • Vim-flavored terminal emulator (Alacritty)
  • Vim-flavored file explorer (Ranger)

Extra reading material