vim
BASIC
Inserting text
Press i
to enter Insert mode.Now behabes like any other text editor,until you press<ESC>
to return Normal mode.
Buffers,tabs,and windows
Vim maintains a set of open files,called "buffers".Ant time we open an existing file or create a new one using Vim,a buffer will be allocated as the in-memory representation of said file.Any changes we make will be tracked within the buffer.When we are ready to update the file,the buffer can be written to disk. A vim seesion has a number of tabs, each of which has a number of windows. Each windows show a single buffer. Unlike other programs you are familiar with, like web browsers, there is not a 1-to-1 corrsepondence between buffers and windows,even within the same tab,This can be quite handy,for example,to view two deifferent parts of a files at the same time.nt
Command-line
Command mode can be entered by typing:
in Normal mode.Your cursor will jump to the command line at the bottom of the screen upon pressing:
. This mode has many functionities,including openning, saving, and closing files, and quitting Vim.
:q
quit:w
save:wq
save and quit:e{name of file}
open file for editing:ls
show open buffershelp {topic}
open helphelp :w
opens help for thew
commandhelp w
opens help for thew
movement
Vim's interface is a programming language
The most impritant idea in vim is that Vim's interface itself ia a programming language.Ketstrokes(with mnmonic names)are ccommands,and these commands compose.This enables efficient movement and edits,especilly once the commands become muscle memory.
Movement
The movement that used to navigate the buffer,are also called"nouns", because they refer to chunks of text.
- Basic movement:
hjkl
(left,down,up,right) - Words:
w
(next word),b
(beginning of word),e
(end of word) - Lines:
0
(beginning of line),^
(firsy 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/toward/backward{character}on the current line
,
/;
for navigating matches
- Search:
/{regex},``n
/N
for navigating matches
Selection
Visual modes:
Visual mode allows you to target a group of code that doesn't follow a distinguishable pattern and edit them
- Visual:
v
- Visual Line:
V
- Visual Block:
Ctrl-v
Edits
Vim's editing commands are also called"verbs",because verbs act on nouns.
i
enter Insert modeo
/O
insert line below/aboved{motion}
delete {motion}dw
is delete word,d$
is delete to end of line,d0
is delete to beginning of line
c{motion}
change {motion}`cw
is change word
x
delete character(equal tocl
)- Visual mode + manipulation
- select text,
d
to delete it orc
to change it`
- select text,
u
to undo,<C-r>
to redoy
to copy/"yank"(some oher commands liked
also copy)p
to paste- Lots more to learn:
~
flips the case of a chararcte
Counts
You can combine nouns and verbs with a count, which will perform a given action a number of times.likes:
3w
move 3 words forward5j
move 5 lines down7wd
delete 7 words
Adancnced Vim
Search and repalce
:s
(substitude)command
* %s/foo/bar/g
* replace foo with bar globally in file
* %s/\[.*\](\(.*\))/\1/g
* replace named Markdown links with plain URLs`
Multiple windows
:sp
/:vsp
to split windows- Can habe multiple views of the same buffer.
Macros
q{character}
to strat recording a macro in resister{character}
q
to stop recording@{character}
replays the macro
...