The editor that withstood the test of time
Editing at the speed of thought
Marek Šuppa
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
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
StackOverflow survey: https://insights.stackoverflow.com/survey/2019#technology
Image from https://devrant.com/rants/83706/few-text-editors-learning-curves-emacs-won-the-competition
Despite what the internet says, it's not that difficult. Here are a few options:
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
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
Optimize for reading code/text, not writing
Be programmable by itself (keystrokes are composable commands)
Avoid the mouse (too slow) or even the arrow keys (too much hand movement)
Optimize for reading code/text, not writing
Be programmable by itself (keystrokes are composable commands)
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
Optimize for reading code/text, not writing
Be programmable by itself (keystrokes are composable commands)
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.
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
NORMAL
NORMAL
INSERT
NORMAL
INSERT
VISUAL
NORMAL
INSERT
VISUAL
COMMAND
:
, for instance :q!
Everything starts and ends in the NORMAL mode. From there you can
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
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
h
, j
, k
and l
h
, j
, k
and l
Images from https://tonsky.me/blog/cursor-keys/
The history is rather simple: the terminal Bill Joy wrote vi
on did not have any arrow keys...
Movement on line level:
^
, 0
or <Home>
^
goes to the first non-blank character$
or <End>
Movement on line level:
^
, 0
or <Home>
^
goes to the first non-blank character$
or <End>
Movement on word level:
w
b
Movement on line level:
^
, 0
or <Home>
^
goes to the first non-blank character$
or <End>
Movement on word level:
w
b
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!
Movement on character level:
h
or <Left>
l
or <Right>
Movement on character level:
h
or <Left>
l
or <Right>
Movement to a specific character:
fx
x
Fx
x
Movement on character level:
h
or <Left>
l
or <Right>
Movement to a specific character:
fx
x
Fx
x
The cursor is on r
:
This is a very nice sentence!
After pressing ft
:
This is a very nice sentence!
Movement across lines:
k
or <Up>
l
or <Down>
gg
G
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
d{motion}
{motion}
dw
is delete word and d$
is delete to end of linec{motion}
{motion}
(like cw
-- change word)d{motion}
and then i
x
/ X
dl
/ dh
)s
xi
)y{motion}
{motion}
to the "register"p
/ P
cc
/ dd
/ yy
xp
rx
x
u
/ <Ctrl-R>
.
Movements are "nouns" because they refer to a specific part of the text.
Movements are "nouns" because they refer to a specific part of the text.
Edits are "verbs" because they act on nouns.
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.
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
2w
5k
14gg
d2w
df)
)
yG
3dd
4u
The lookup capabilities are based on regular expressions.
<CR>
basically means pressing Enter
.
The lookup capabilities are based on regular expressions.
<CR>
basically means pressing Enter
.
/{regexp}<CR>
/ ?{regexp}<CR>
{regex}
from the current line downwards / upwardsn
N
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 |
You can switch to it in multiple ways:
i
)a
for append)A
)You can switch to it in multiple ways:
i
)a
for append)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.
You can switch to it in multiple ways:
i
)a
for append)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+[
It can operate on various levels:
Characters (by pressing v
)
Lines (by pressing <Shift+v>
)
Blocks (by pressing <Ctrl+v>
)
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.
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.
Most of them are the same as in the NORMAL mode.
I
/ A
d
/ c
/ y
p
or P
u
/ U
/ ~
rx
x
>
/ <
:
/ <Esc>
(twice)
:
/ <Esc>
(twice)
Up / Down arrows let you go through the command history
<Tab>
does autocompletion:
/ <Esc>
(twice)
Up / Down arrows let you go through the command history
<Tab>
does autocompletionSome 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: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}/
: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 occurrencec
makes Vim ask you to confirm the substitutioni
makes the regular expression case-insensitive: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 occurrencec
makes Vim ask you to confirm the substitutioni
makes the regular expression case-insensitive&
can be used to repeat the last substitution
\r
as newline:!{cmd}
{cmd}
in a shell:r!{cmd}
{cmd}
and paste its stdout to the currently opened file (buffer):!{cmd}
{cmd}
in a shell:r!{cmd}
{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}
{cmd}
and replace them with the output:%!{cmd}
%
) through {cmd}
:'<,'>!{cmd}
{cmd}
!
is pressed in VIRTUAL modenl
Adds line numbers to each input line
-s
sets the separator (<Tab>
by default)
nl -s :
-w
sets how many columns are used for line numbers (6 by default)
nl -w 1
$ cat file.txtAdamBeatriceCynthiaDavidEmma$ nl file.txt 1 Adam 2 Beatrice 3 Cynthia 4 David 5 Emma
$ nl -w 1 file.txt1 Adam2 Beatrice3 Cynthia4 David5 Emma$ nl -w 1 -s ":" file.txt1:Adam2:Beatrice3:Cynthia4:David5:Emma
shuf
outputs a random permutation of the input lines
-r
repeat lines from the input
-n
print out at most n lines
$ cat file.txt1 Adam2 Beatrice3 Cynthia4 David5 Emma$ shuf file.txt2 Beatrice3 Cynthia1 Adam5 Emma4 David$ shuf -n 6 file.txt2 Beatrice3 Cynthia1 Adam5 Emma4 David
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
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 |