+ - 0:00:00
Notes for current slide
Notes for next slide

Vim

The editor that withstood the test of time

Editing at the speed of thought

Marek Šuppa

1 / 60

Why learn UNIX Vim today?

Despite what it may look like, it is still being used rather extensively.

See for yourself: https://insights.stackoverflow.com/survey/2021#most-popular-technologies-new-collab-tools

2 / 60

Vim

  • A text editor ubiquitous on *NIX systems

  • An extension of the vi editor, part of the original UNIX

  • Simple at its core, but very extensible

  • User friendly to programmers and system administrators

  • The 5th most popular development environment in 2021 and 2022 according to StackOverflow survey

3 / 60

Vim

  • A text editor ubiquitous on *NIX systems

  • An extension of the vi editor, part of the original UNIX

  • Simple at its core, but very extensible

  • User friendly to programmers and system administrators

  • The 5th most popular development environment in 2021 and 2022 according to StackOverflow survey


Opening it (with a file) is very easy, just run

vim file.txt

from your shell

4 / 60

Exiting Vim II

Despite what the internet says, it's not that difficult. Here are a few options:

7 / 60

Exiting Vim II

Despite what the internet says, it's not that difficult. Here are a few options:

  • :q or :q!

    • q stands for "quit" (you can also run :quit if you want)

    • by default, Vim won't quit if there are unsaved changes

    • you can force it to do so by appending ! to the command

8 / 60

Exiting Vim II

Despite what the internet says, it's not that difficult. Here are a few options:

  • :q or :q!

    • q stands for "quit" (you can also run :quit if you want)

    • by default, Vim won't quit if there are unsaved changes

    • you can force it to do so by appending ! to the command

  • :wq, :x and :wq!

    • w stands for "write" and makes Vim write the changes to the opened file

    • wq is a combination of the "write" and "quit" commands

    • x is a quick shortcut for wq

    • appending ! makes Vim ignore warnings about overwriting

9 / 60

Vim philosophy

  1. Optimize for reading code/text, not writing

  2. Be programmable by itself (keystrokes are composable commands)

  3. Avoid the mouse (too slow) or even the arrow keys (too much hand movement)

10 / 60

Vim philosophy

  1. Optimize for reading code/text, not writing

  2. Be programmable by itself (keystrokes are composable commands)

  3. Avoid the mouse (too slow) or even the arrow keys (too much hand movement)

In practice this means using different "modes of operation" for different kinds of tasks

11 / 60

Vim philosophy

  1. Optimize for reading code/text, not writing

  2. Be programmable by itself (keystrokes are composable commands)

  3. Avoid the mouse (too slow) or even the arrow keys (too much hand movement)

In practice this means using different "modes of operation" for different kinds of tasks

There are 12 of them in total but we'll only go through the most important ones.

12 / 60
  • Vim can be profoundly alien to people who only used "standard programming tools"

  • Many of the ideas encapsuled in it are very valuable outside of it as well (similar to UNIX) -- that's why we teach it

Vim modes

  • NORMAL

    • the default mode the editor starts in
    • each keypress is equivalent to an editor command
13 / 60

Vim modes

  • NORMAL

    • the default mode the editor starts in
    • each keypress is equivalent to an editor command
  • INSERT

    • the "normal" mode of other editors
    • each keypress actually inserts a character to a given file
14 / 60

Vim modes

  • NORMAL

    • the default mode the editor starts in
    • each keypress is equivalent to an editor command
  • INSERT

    • the "normal" mode of other editors
    • each keypress actually inserts a character to a given file
  • VISUAL

    • allows you to take a selection of text and apply various transformations
15 / 60

Vim modes

  • NORMAL

    • the default mode the editor starts in
    • each keypress is equivalent to an editor command
  • INSERT

    • the "normal" mode of other editors
    • each keypress actually inserts a character to a given file
  • VISUAL

    • allows you to take a selection of text and apply various transformations
  • COMMAND

    • lets you run commands that control the whole editor in its "command line"
    • commands start with :, for instance :q!
    • technically known as Command-line or Cmdline mode
16 / 60

Vim modes II

Everything starts and ends in the NORMAL mode. From there you can

17 / 60

Vim modes II

Everything starts and ends in the NORMAL mode. From there you can

  • Press i to start the INSERT mode

  • Press v to start the VISUAL mode

  • Press : to start the COMMAND mode

18 / 60

Vim modes II

Everything starts and ends in the NORMAL mode. From there you can

  • Press i to start the INSERT mode

  • Press v to start the VISUAL mode

  • Press : to start the COMMAND mode

To get back to the NORMAL mode, press ESC key (twice in the COMMAND mode).

Image from CS61a article on Vim

19 / 60

Normal mode

20 / 60

Movement in NORMAL mode

  • Instead of arrow keys, Vim uses h, j, k and l

21 / 60

Movement in NORMAL mode

  • Instead of arrow keys, Vim uses h, j, k and l

  • The reason why is efficiency: they are directly on your home row:

Images from https://tonsky.me/blog/cursor-keys/

22 / 60

The history is rather simple: the terminal Bill Joy wrote vi on did not have any arrow keys...

https://catonmat.net/why-vim-uses-hjkl-as-arrow-keys

Movement in NORMAL mode II

Movement on line level:

  • ^, 0 or <Home>
    • Jump to the beginning of the line
    • ^ goes to the first non-blank character
  • $ or <End>
    • Jump to the end of the line
23 / 60

Movement in NORMAL mode II

Movement on line level:

  • ^, 0 or <Home>
    • Jump to the beginning of the line
    • ^ goes to the first non-blank character
  • $ or <End>
    • Jump to the end of the line

Movement on word level:

  • w
    • Jump forward to the beginning of a word
  • b
    • Jump backwards to the beginning of a word
24 / 60

Movement in NORMAL mode II

Movement on line level:

  • ^, 0 or <Home>
    • Jump to the beginning of the line
    • ^ goes to the first non-blank character
  • $ or <End>
    • Jump to the end of the line

Movement on word level:

  • w
    • Jump forward to the beginning of a word
  • b
    • Jump backwards to the beginning of a word

The cursor is on r:

This is a very nice sentence!

After pressing w:

This is a very nice sentence!

After pressing b:

This is a very nice sentence!
25 / 60

Movement in NORMAL mode III

Movement on character level:

  • h or <Left>
    • Move one character to the left
  • l or <Right>
    • Move one character to the right
26 / 60

Movement in NORMAL mode III

Movement on character level:

  • h or <Left>
    • Move one character to the left
  • l or <Right>
    • Move one character to the right

Movement to a specific character:

  • fx
    • Jump to the next occurence of character x
  • Fx
    • Jump to the previous occurence of character x
27 / 60

Movement in NORMAL mode III

Movement on character level:

  • h or <Left>
    • Move one character to the left
  • l or <Right>
    • Move one character to the right

Movement to a specific character:

  • fx
    • Jump to the next occurence of character x
  • Fx
    • Jump to the previous occurence of character x

The cursor is on r:

This is a very nice sentence!

After pressing ft:

This is a very nice sentence!
28 / 60

Movement in NORMAL mode IV

Movement across lines:

  • k or <Up>
    • Move to the line above
  • l or <Down>
    • Move to the line below
  • gg
    • Move to the first line of the file
  • G
    • Move to the last line of the file
29 / 60

Editing in NORMAL mode

Edit commands are the equivalent of text edit you'd do with a mouse.

It is here where Vim starts to look like a programming language.

  • o / O
    • Insert line below / above
  • d{motion}
    • delete {motion}
    • for instance dw is delete word and d$ is delete to end of line
  • c{motion}
    • change {motion} (like cw -- change word)
    • a shortcut for pressing d{motion} and then i
  • x / X
    • delete character after / before (same as dl / dh)
  • s
    • substitute character (same as xi)
  • y{motion}
    • "yank" (copy) everything until {motion} to the "register"
    • delete and copy also put the changed text to the "register"
  • p / P
    • paste from the "register" after / before cursor
30 / 60

More edit commands in NORMAL mode

  • cc / dd / yy

    • change (remove and edit), delete (cut) or "yank" (copy) the entire line
  • xp

    • swap two adjacent characters
      1. delete character under cursor and move to the next
      2. put deleted character to register
      3. paste deleted character from register
  • rx

    • replace current character with x
  • u / <Ctrl-R>

    • undo / redo a change
  • .

    • repeat last change
31 / 60

Vim's NORMAL commands as a language

Movements are "nouns" because they refer to a specific part of the text.

32 / 60

Vim's NORMAL commands as a language

Movements are "nouns" because they refer to a specific part of the text.

Edits are "verbs" because they act on nouns.

33 / 60

Vim's NORMAL commands as a language

Movements are "nouns" because they refer to a specific part of the text.

Edits are "verbs" because they act on nouns.

Combined with "modifiers" like counts, they comprise a powerful language.

34 / 60

Vim's NORMAL commands as a language

Movements are "nouns" because they refer to a specific part of the text.

Edits are "verbs" because they act on nouns.

Combined with "modifiers" like counts, they comprise a powerful language.

  • 8l
    • Move eight characters to the left
  • 2w
    • Move two words forward
  • 5k
    • Move 5 lines up
  • 14gg
    • Go to the 14th line
  • d2w
    • Delete two words
  • df)
    • Delete until you find )
  • yG
    • "Yank" (copy) everything until the end of the file
  • 3dd
    • Delete tree lines (below)
  • 4u
    • Undo the last 4 changes
35 / 60

Lookup in NORMAL mode

The lookup capabilities are based on regular expressions.

<CR> basically means pressing Enter.

36 / 60

Lookup in NORMAL mode

The lookup capabilities are based on regular expressions.

<CR> basically means pressing Enter.

  • /{regexp}<CR> / ?{regexp}<CR>

    • look up {regex} from the current line downwards / upwards
  • n

    • repeat the search in the same direction
  • N

    • repeat the search in the opposite direction
37 / 60

Lookup-supported regular expressions

expr description
. any character
[ ] character class (or [^ ])
^ beginning of the line
$ end of the line
\? match once or not at all
\+ match one and more times
* match zero or more times
\{2,7} two to seven matches
38 / 60

Insert mode

39 / 60

INSERT mode

You can switch to it in multiple ways:

  • At the character you are currently at (i)
  • Move cursor to the right (a for append)
  • Move cursor at the end of the line (A)
40 / 60

INSERT mode

You can switch to it in multiple ways:

  • At the character you are currently at (i)
  • Move cursor to the right (a for append)
  • Move cursor at the end of the line (A)

You can move in it with arrow keys. But it is much more effective to go back to NORMAL mode and move in there.

41 / 60

INSERT mode

You can switch to it in multiple ways:

  • At the character you are currently at (i)
  • Move cursor to the right (a for append)
  • Move cursor at the end of the line (A)

You can move in it with arrow keys. But it is much more effective to go back to NORMAL mode and move in there.

To get out of it (and get back to NORMAL mode), you can use

  • Esc
  • Ctrl+C
  • Ctrl+[
42 / 60

Visual mode

43 / 60

Visual mode

It can operate on various levels:

  • Characters (by pressing v)

  • Lines (by pressing <Shift+v>)

  • Blocks (by pressing <Ctrl+v>)

44 / 60

Visual mode

It can operate on various levels:

  • Characters (by pressing v)

  • Lines (by pressing <Shift+v>)

  • Blocks (by pressing <Ctrl+v>)

The selection itself can be changed using movement commands.

45 / 60

Visual mode

It can operate on various levels:

  • Characters (by pressing v)

  • Lines (by pressing <Shift+v>)

  • Blocks (by pressing <Ctrl+v>)

The selection itself can be changed using movement commands.

With o you can swap the position from which the selection is moving.

46 / 60

VISUAL mode commands

Most of them are the same as in the NORMAL mode.

  • I / A

    • insert / append typed text to each line of the selection
  • d / c / y

    • delete / change (delete and insert) / yank (copy) the selection
    • deleted or yanked (copied) text can then be pasted with p or P
  • u / U / ~

    • uppercase / lowercase / swap the case of the selection
  • rx

    • replace app characters in the selection with character x
  • > / <

    • increase / decrease the indent ("TAB-ization") of the selection
47 / 60

Command (Cmdline) mode

48 / 60

COMMAND (Cmdline) mode

  • : / <Esc> (twice)

    • Get in / out of the COMMAND mode
49 / 60

COMMAND (Cmdline) mode

  • : / <Esc> (twice)

    • Get in / out of the COMMAND mode
  • Up / Down arrows let you go through the command history

    • <Tab> does autocompletion
50 / 60

COMMAND (Cmdline) mode

  • : / <Esc> (twice)

    • Get in / out of the COMMAND mode
  • Up / Down arrows let you go through the command history

    • <Tab> does autocompletion

Some helpful commands:

  • :history shows the complete history in a listing
  • :help command shows the help page for a given command
  • :syntax on / :syntax off turns on / off syntax highlighting
51 / 60

Text substitution in COMMAND mode

  • When coming from the VISUAL mode, the command is applied on the selection -- otherwise on the whole text.
52 / 60

Text substitution in COMMAND mode

  • When coming from the VISUAL mode, the command is applied on the selection -- otherwise on the whole text.

:s/{regexp}/{string}/{mod}

  • substitutes matched {regexp} for {string}

  • also works on text selected in VISUAL mode :'<,'>s/{regexp}/{string}/

  • and on the whole text using %, for instance :%s/{regexp}/{string}/

  • and from the 5th to the last line :5,$s/{regexp}/{string}/

53 / 60

Text substitution in COMMAND mode

  • When coming from the VISUAL mode, the command is applied on the selection -- otherwise on the whole text.

:s/{regexp}/{string}/{mod}

  • substitutes matched {regexp} for {string}

  • also works on text selected in VISUAL mode :'<,'>s/{regexp}/{string}/

  • and on the whole text using %, for instance :%s/{regexp}/{string}/

  • and from the 5th to the last line :5,$s/{regexp}/{string}/

  • {mod} modifies the replace behavior

    • g replaces all, not just first occurrence
    • c makes Vim ask you to confirm the substitution
    • i makes the regular expression case-insensitive
54 / 60

Text substitution in COMMAND mode

  • When coming from the VISUAL mode, the command is applied on the selection -- otherwise on the whole text.

:s/{regexp}/{string}/{mod}

  • substitutes matched {regexp} for {string}

  • also works on text selected in VISUAL mode :'<,'>s/{regexp}/{string}/

  • and on the whole text using %, for instance :%s/{regexp}/{string}/

  • and from the 5th to the last line :5,$s/{regexp}/{string}/

  • {mod} modifies the replace behavior

    • g replaces all, not just first occurrence
    • c makes Vim ask you to confirm the substitution
    • i makes the regular expression case-insensitive
  • & can be used to repeat the last substitution

  • when it comes to replacing, Vim interprets \r as newline
55 / 60

Shell commands in COMMAND mode

  • :!{cmd}

    • execute {cmd} in a shell
  • :r!{cmd}

    • execute {cmd} and paste its stdout to the currently opened file (buffer)
56 / 60

Shell commands in COMMAND mode

  • :!{cmd}

    • execute {cmd} in a shell
  • :r!{cmd}

    • execute {cmd} and paste its stdout to the currently opened file (buffer)

Similar concept can be used to filter the file we are editing:

  • :3,5!{cmd}

    • pass lines 3 to 5 to the stdin of {cmd} and replace them with the output
  • :%!{cmd}

    • pass the contents of the whole file (%) through {cmd}
  • :'<,'>!{cmd}

    • pass the contents of the selection through {cmd}
    • happens automatically when ! is pressed in VIRTUAL mode
57 / 60

Useful commands

58 / 60

nl

Adds line numbers to each input line

  • -s sets the separator (<Tab> by default)

    • for instance nl -s :
  • -w sets how many columns are used for line numbers (6 by default)

    • for instance nl -w 1
$ cat file.txt
Adam
Beatrice
Cynthia
David
Emma
$ nl file.txt
1 Adam
2 Beatrice
3 Cynthia
4 David
5 Emma
$ nl -w 1 file.txt
1 Adam
2 Beatrice
3 Cynthia
4 David
5 Emma
$ nl -w 1 -s ":" file.txt
1:Adam
2:Beatrice
3:Cynthia
4:David
5:Emma
59 / 60

shuf

  • outputs a random permutation of the input lines

  • -r repeat lines from the input

  • -n print out at most n lines

$ cat file.txt
1 Adam
2 Beatrice
3 Cynthia
4 David
5 Emma
$ shuf file.txt
2 Beatrice
3 Cynthia
1 Adam
5 Emma
4 David
$ shuf -n 6 file.txt
2 Beatrice
3 Cynthia
1 Adam
5 Emma
4 David
60 / 60

Why learn UNIX Vim today?

Despite what it may look like, it is still being used rather extensively.

See for yourself: https://insights.stackoverflow.com/survey/2021#most-popular-technologies-new-collab-tools

2 / 60
Paused

Help

Keyboard shortcuts

, , Pg Up, k Go to previous slide
, , Pg Dn, Space, j Go to next slide
Home Go to first slide
End Go to last slide
Number + Return Go to specific slide
b / m / f Toggle blackout / mirrored / fullscreen mode
c Clone slideshow
p Toggle presenter mode
t Restart the presentation timer
?, h Toggle this help
Esc Back to slideshow