Vim basic commands
Cheat sheet
https://vim.rtorr.com/
Vim’s Automatic Marks
` `` ` Position before the last jump within current file
‘. Location of last change
‘^ Location of last insertion
‘[ Start of last change or yank
‘] End of last change or yank
‘< Start of last visual selection
‘> End of last visual selection
Changing mode
- Insert mode:
i - Replace mode:
R - Visual mode:
v - Line visual mode:
V - Block mode:
<C-v>or<C-V> - Command line mode:
:
Movement
- Basic movement:
hjkl(left, down, up, right) - Words:
w(next word),b(beginning of word),e(end of word) - Lines:
0(beginning of line),^or_(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/backward {character} on the current line
,/;for navigating matches
- Search:
/{regex},n/Nfor navigating matches
Edits
ienter Insert mode- but for manipulating/deleting text, want to use something more than backspace
o/Oinsert line below / aboved{motion}delete {motion}- e.g.
dwis delete word,d$is delete to end of line,d0is delete to beginning of line
- e.g.
c{motion}change {motion}- e.g.
cwis change word - like
d{motion}followed byi
- e.g.
xdelete character (equal dodl)ssubstitute character (equal toxi)- Visual mode
+manipulation- select text,
dto delete it orcto change it
- select text,
uto undo,<C-r>to redoyto copy / “yank” (some other commands like d also copy)pto paste- Lots more to learn: e.g.
~flips the case of a character dGwill delete all lines until end of file.
Count
3wmove 3 words forward5jmove 5 lines down7dwdelete 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 parenthesesci[change the contents inside the current pair of square bracketsda'delete a single-quoted string, including the surrounding single quotesyiwcopy the current word
Shell
Open shell from vim
:shellopen shell window:ter[minal]will open shell in a separated window(Neovim)
Etc
-
To turn off highlighting until the next search:
:noh -
Or turn off highlighting completely:
set nohlsearch -
Search replace in an interactive mode
- adding the flag
cin command prompt
1
:%s/foo/bar/gc
- adding the flag
-
Indent multiple lines
select lines with visual mode then type> -
Repeat last changes
The.command repeats the last change made in normal mode.dwcan be repeated
The
@:command repeats the last command-line change:s/old/new/can be repeated
-
Replacing a word with a copied word
yiw-> move cursor to another location ->ciw<C-r>0<Esc>: replacing a word with a copied word.- we can repeat with
.command
-
Search the current word
*to search the word#to search the word in backward direction
-
Adding double quotes for selected text with a visual mode
- If vim support the surround, select text with a visual mode then press
S" - surround mode,
:set surround
- If vim support the surround, select text with a visual mode then press
-
Display searching a result in the middle of screen
- after search, type
zzwill display in the middle of screen :set scrolloff=10will always have 10 lines before and after.:set so=10is shortcut- https://vim.fandom.com/wiki/Make_search_results_appear_in_the_middle_of_the_screen
- after search, type
-
Copy in a different buffer
"ayy: copy the current line in buffer a."ap: paste the contents in a buffer a at the cursor.
-
Add indentation from the current line until the end of the file
>G
-
Show search history
q\
-
Scroll off 8 lines
set scrolloff=8
-
Do not lose the yanked buffer on visual mode
vnoremap <leader>p "_dP
-
Always copy into system clipboard
vnoremap <leader>y "+y
-
Moving selected text up/down
vnoremap J :m '>+1<CR>gv=gvvnoremap K :m '<-2<CR>gv=gv