Philosophy of Vim
When programming, you spend most of your time reading/editing, not writing. For this reason, Vim is a modal editor: it has different modes for inserting text vs manipulating test.
Vim avoids the use of the mouse and using the arrow keys. The end result is an editor that can match the speed at which you think.
Modal editing
- Normal
<ESC>
: for moving arounding a file and making edits - Insert
i
: for inserting text - Replace
R
: for replacing text - Visual (plain
v
, lineV
, or block<C-v>
): for selecting blocks of text - Command-line
:
: for running a command
Basics
Inserting text
Buffers, tabs, and windows
[Vedio-Note]
Command-line
:q
quit (close window):w
save (“write”):wq
save and quite:e {name of file}
open file for editing:ls
show open buffers:help {topic}
open help-
:help :w
opens help for the:w
command
-
:help w
opens help for thew
movement
Vim’s interface is a programming language
Keystrokes (with mnemonic name) are commands, and these commands compose.
Movement
Movements in Vim are also called ‘nouns’, because they refer to chunks of text.
Normal modes:
- 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}) - Misc:
%
(corresponding item) - Find:
f{character}
,t{character}
,F{character}
,T{character}
-
- find/to forward/forward/backward {character} on the current line
-
,
/;
for navigating matches
- Search:
/{regex}
,n
/N
for navigating matches
Selection
Visual modes:
Can use movement keys to make selection.
Edits
Vim’s editing commands are also called “verbs”, because verbs act on nouns.
[vedio]
Counts
You can combine nouns and verbs with a count, which will perform a given action a number of times.
3w
move 3 words forward5j
move 5 lines down7dw
delete 7 words
Modifiers
You can use modifiers to change the meaning of a noun. Some modifiers are i
, which means “inner” or “inside”, and a
, which means “around”.
ci(
change the contents inside the current pair of parenthesesda'
delete a single-quoted string, including the surrounding single quotes
Demo
[Vedio]
Customizing Vim
Vim is customized through a plain-text configuration file in ~/.vimrc
(containing Vimscript commands).
We recommend using a well-documented basic config because it fixes some of Vim’s quirky default behavior. Download our config here and save it to ~/.vimrc
.
You can look at people’s dotfiles on GitHub for inspiration, for example, your instructors’ Vim configs (Anish, Jon (uses neovim), Jose). There are lots of good blog posts on this topic too. Try not to copy-and-paste people’s full configuration, but read it, understand it, and take what you need.