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.
:qquit:wsave:wqsave and quit:e{name of file}open file for editing:lsshow open buffershelp {topic}open helphelp :wopens help for thewcommandhelp wopens help for thewmovement
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/Nfor 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.
ienter Insert modeo/Oinsert line below/aboved{motion}delete {motion}dwis delete word,d$is delete to end of line,d0is delete to beginning of line
c{motion}change {motion}`cwis change word
xdelete character(equal tocl)- Visual mode + manipulation
- select text,
dto delete it orcto change it`
- select text,
uto undo,<C-r>to redoyto copy/"yank"(some oher commands likedalso copy)pto 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:
3wmove 3 words forward5jmove 5 lines down7wddelete 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/:vspto split windows- Can habe multiple views of the same buffer.
Macros
q{character}to strat recording a macro in resister{character}qto stop recording@{character}replays the macro
...