Vim Notes
These are learning notes when learning vim key bindings
Vim has three modes
INSERT: You can useifor the insert modeVISUAL: You can usevfor visual mode.NORMAL MODE: just pressESCto get into normal mode
Basic Navigation
- In order to get into insert mode use
i. ggtakes you to the first line of the document.Shift-Gtakes you to the last line of the document.- You can also move to the specfic line by
<line number> G. - you can use
xto delete a character. - you can use
uto undo a change. - you can also specific the number of characters you want to delete
<num of char> x - to delete a whole word use
dw. - to delete a line use
dd. - to number of lines use
<num of lines> dd. - to move one word at a time
w.it goes to the beginning character of the word. - to move using last character use
e. - to move number of words use
<num of words> wfor example10 wthis moves 10 words. - to move backward a word you can use
b. - to move number of words backward use
<num of words> b10 b. - use
0to go to the beginning of the line. - you use
pto put a line to desired line number for example you candda line then10 Gand then dopto put the line on line 10 - You can replace a word or a character by using
r. Changeto get into change command you can change words by usingceto get the end of the word.c$to change the entire line- navigating using command
f:(forward <to_word>) and uppercase does this in reverse. for example,fewill take to the character e andFewill take you backward. - to delete a specific line you can do
:<line>d
Different Insert Modes.
Atakes to the end of the line and enters in to insert mode.atakes you into insert mode.Sdeletes the entire line and enters you into the insert mode.sdelete the character and enters you into the insert mode.oenters you into the insert mode in the next line.Oenters you into the insert mode in the preceding line.Cdeletes the entire linec$d$deletes everything from the cursor to the end of the line. You can useDto do the same thing.
Yanking
Its the copy command in the vim editor
- to copy a word just use
ywyou can paste by usingp. yyto copy the entire line.y$to copy from the cursor to the end of the line.Pputs to the preceding line
Searching through the code/doc
/search forward in the doc. then hitnto goto the next instance.?searches backward in the doc.
Replacing
- You can
:%s/oldstring/newstring/gto replace a word - you can also give line numbers
1,2s/old/new/g :%s/old/new/gcgives a prompt.
Executing a command in vim
! <cmd>to execute a commandcntrl-dto see what commands are available
note: you can enable vim mode in zsh by adding bindkey -v to zshrc
VIM Macros
- You can record vim macros by using the
qkey and followed whatever key you want to assign that macro to. - to run the macro number of occurences and key where you saved the macro.
Vim Splits
:splitdoes horizontal split:sp:vsplitdoes vertical split:vscntrl-w ccloses the split.- you can also specific the height when doing a split
10sp <filename> - you can do
cntrl-w Rto swap the split cntrl-w jandcntrl-w kmoves you up and downcntrl-w handcntrl-w lmoves you left and right- you can also resize the splits
vertical resize +5orresize +5for horizontal split. - you can also open terminal in vim. especially in split
:vs | :terminal
Command Mode in Vim.
- you can run shell commands in vim
:! lsthis will print the files of your directory. - you can also read the outputs of the command and add it to the document
:read !date - you can also use ranges when replacing the text
11,16s/old/new/g :g/vim/dthis will delete vim from every line where it finds the word.!g/vim/dthis will delete that will delete that's not vim.g/^/pu/ =\"\n\"puts spaces.g/<word>/m$moves the lines containing that word to the end of the file
Vim Tabs
- You can edit a file in a new tab by
tabedit <filename> - you can
gtto cycle through the tabs.
Few Notes
ddpallows you to swap a line.g/<word>/+1m-2>allows you to indent111,114>:{range} '<,'>' norm A"adds"at the end of the line.:{range} '<,'>' norm I"adds"at the beginning of the line.cgnallows you to change the next instance when doing a search.- you can also set abbreviations
:ab rtfm Read The Friend Manualand usecntrl-vif you don't want to expand it. - Read commandline commands into vim
:r !ls "+yallows you to copy the selection to the clipboard in vim