Do you have a moment to talk about our lord and savior, Vim?

Waldir Leoncio

Oslo Centre for Biostatistics and Epidemiology, UiO

2025-05-20

Part 1: Testifying

From dismissal to adoption: a curated timeline

%%{init: {'theme':'dark'}}%%
gantt
    dateFormat YY
    axisFormat %Y
    Lost HPC virginity: milestone, 16, 1y
    "F this weird-*** thing, I'll just use nano" : 15, 9y
    Hired as RSE at UiO: milestone, 20, 1d
    "I will fear no more": milestone, 24, 1d
    (studies Vim furiously): 24, 1y
    "I have seen the light": milestone, 25, 1d

Why should you care about this arcane program?

Or, as normal people would put it:

What’s with you guys and all these MS-DOS programs?

If you’re a minimalist, just use Notepad!

Oh, and look at this beauty of a home page!

But, well, you see…

  1. Some people swear by it.
  2. And we’re computer-savvy people who like keyboard shortcuts… right?
  3. And we’re RSEs, so it’s sort of our job to be on top of such things.

Slow and steady

  • Cheat sheets are a great crutch
  • It is fast even though it doesn’t feel fast
  • Downside: you look less busy
  • You don’t have to choose one text editor

Modes are annoying but useful

Function Noob 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 takes a bit of getting used to
  • Learning so many shortcuts takes some time
    • But you don’t have to learn them all
    • And you don’t have to learn at once
    • And you will memorize the ones you use most often

A new dawn

  • Vim-shortcuts are not just for Vim
  • You start getting annoyed when you don’t see them literally everywhere

Part 2: A crash-course on

Step zero: acquire a pure-text file, bigger is better

Oh yeah, and install and open Vim.

The basics

Important

Made an oopsie? Undo and redo with u and Ctrl+r

Important

Vim shortcuts are case-sensitive: i is not the same as I (a.k.a. Shift+f)!

Tip

Need help? Wanna go down the rabbit hole? Read docs with :help or :h

Tip

Repeat the last action (not movement!) with .. It’s more useful than it seems!

Hand position for coders raised in a barn

  1. Find those bumps on the f and j keys.
  2. Put your index fingers on them.
  3. Feel those bumps. Get comfortable with them.
  4. Don’t make it weird, though.

🎉 Congratulations! Now you can do everything without moving your hands!

Vim 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
Open (edit) a file :e filename
Save (write to) a file (as) :w (:w filename)
Close (quit) a file (drop changes) :q (:q!)
Close and quit :wq (:x)

Moving around

Those f🤬cking arrows…

IBM model M (1986)

ADM-3A (1976)

Tip

j is mapped to “down” because that’s (arguably) the most frequent direction you’ll be moving.

Vim words vs. Vim WORDS

According to (:help word):

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

For example:

ThisIsOneWordAndAlsoOneWORD
^
1

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

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

Moving faster

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

Tip

Add a number before the command to repeat it that many times

🔥 3w moves 3 words forward, 4k moves 4 lines up

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

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

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
  • foldmethod=syntax 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/g
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

Moving fast and erasing 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

Tip

xp swaps two characters (useful for typos!). Wonder why? See next page.

CutDelete, copyyank, and paste

(a.k.a. 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 @@

Expanding and configuring Vim

" Enable relative number in normal and visual mode
set relativenumber
augroup RelativeNumber
  autocmd!
  autocmd InsertEnter * set norelativenumber
  autocmd InsertLeave * set relativenumber
augroup END

Vim shortcuts everywhere

  • 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 material and topics