Philosophy
- Optimise 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.
Vim modes
- NORMAL (
<ESC>
, sometimes double)- Default mode the editor starts in; one should spend most of the time here
- Each keypress is equivalent to an editor command
- INSERT (
i
)- "Normal" mode of other editors
- Each keypress actually inserts a character to a given file
- VISUAL (
v
)- 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"
- Technically known as Command-line or Cmdline mode
- You use the
<ESC>
key a lot when using Vim: consider remapping Caps Lock to Escape (macOS instructions).
Links
- Presentation from Marek Suppa
- Why should you learn Vim in 2020: a nice reflection on the question may of you are asking
- Why does Vim use hjkl a nice historical explanation of a rather strange phenomenon of hjkl.
- Your problem with Vim is that you don't grok vi: probably one of the most famous StackOverflow answers of all time on how Vim relates to Vi and how you can learn much from well designed technologies that haven't changed much in the past.
- Cheatsheet & Best Vim Tips
- Vim Golf: real Vim ninjas count every keystroke – do you?
- Customise vim by editing
~/.vimrc
, e.g. like this, or installing useful plugins - Once you get you to it, you might want to use it everywhere
Snippets
Normal:
- Basic movement:
hjkl
(left, down, up, right) - Words:
w
(next word),b
(beginning of word),e
(end of word) - Lines:
0
(beginning of line),^
(first non-blank character),$
(end of line) - Screen:
H
(top of screen),M
(middle of screen),L
(bottom of screen) - Scroll:
Ctrl-u
(up),Ctrl-d
(down) - File:
gg
(beginning of file),G
(end of file) - Line numbers:
:{number}<CR>
or{number}G
(line {number}) fx
: jump to the next occurrence of characterx
/{regexp}<Enter>
: look up{regex}
from the current line downwards (withn
for next)u
: undoy
: copy/yankp
: paste
Insert:
d2w
delete two words or3dd
delete three lines below ordf)
delete until you find)
(= noun, verb, modifier); generallyd{motion}
a
: append to the rightA
: append to the end of line
Visual
v
: characters<Shift+v>
: lines<Ctrl+v>
: blocks
Command:
:q!
: quit without saving:x
: quit with saving:history
,:help command
:s/{regexp}/{string}/{mod}
: substitute matched{regexp}
for{string}
with given{mod}
(g
for replace all,c
with confirmation,i
for case insensitive) whilst\r
is the new line:!{cmd}
: execute{cmd}
:r!{cmd}
: execute{cmd}
and paste its stdout to the currently opened file (buffer):%!{cmd}
: pass the contents of the whole file (%
) through{cmd}
:sp
,:svp
: split windows
Alternatives
• nano
: best for beginners and quick edits
• emacs
: highly extensible and powerful, for advanced users
• vim
: efficient for those familiar with its modal editing
• ed
: minimal and historical, not practical for most users
• cat
: utility for viewing files, not editing