Vim Reference
Thursday, May 28, 2020
- h j k l → basic movement keys … 10j
- b w B W → move back/forward by token/word
- e E → move to end of the token/word
- 0 → jump to first character of line
- $ → jump to end of the line
- ^ → jump to first non-whitespace character of line
- _ → jump to first non-whitespace character of line
- g_ → jump to last non-whitespace character of line
- ctrl+u ctrl+d → scroll up/down half page
- ctrl+y ctrl+e → scroll up/down line by line
- ctrl+b ctrl+f → scroll up/down full page
- H M L → move to the top/middle/bottom of the screen
- ’’ → jump back to where you just were
- ctrl+o ctrl+i → move backward/forward through the jump history
- {} → move to the beginning/end of the paragraph
- () → move to the beginning/end of the sentence
- % → jump to the corresponding (, {, [
- 50% → jump to the line at the 50% of the file
- :42 → jump to the line 42
- 42G → jump to the line 42
- 10| → move to column 10
- f<char> → move to next <char> on line → ; next , previous occurrence
- F<char> → move to previous <char> on line
- t<char> → move before next <char> on line
- T<char> → move before previous <char> on line
Movement in insert mode
- shift+right arrow → go to the right, word by word
- shift+left arrow → go to the left, word by word
Moving screen
- zz → scroll the view, that cursor is at center
- zt → (top) scroll the view, that cursor is at top
- zb → (bottom) scroll the view, that cursor is at bottom
Marking location
- m<char> → create mark <char> to the current location ma
- '<char> → move to the line containing mark <char> 'a
- `<char> → move to the precise location of mark <char> `a
Macros
- q<char> command q → create macro <char> containing command
- qa0vwhr_jq → create macro a which in visual mode replace everything from begining of line to the first word with underscore
- @<char> → use macro <char>
- 2@<char> → use macro <char> two times
- @@ → use last macro
- :reg → shows all registers / macros
Registers
- "<char> command → create register <char> containing output of command
- "ay/foo → yanking a copy of the text from here to the next line containing “foo” into a register
- "ap → pastes a copy of the a register’s content into the text after cursor
- "+yw → copy to system’s clipboard
- "+p → paste from system’s clipboard
Search
To search for similar word under the cursor type / then press ctrl-r and ctrl-w ← this will copy the word under the cursor to command line. Now you can edit it and press enter. To return to your previous possition hit ctrl-o.
- /<char> → search for <char> forwords
- / → repeat the last search
- 5/pattern → search for fifth occurrence of the pattern
- n → to go to next occurrence
- N → to go to previous occurrence
- ggn → to go to first occurrence
- GN → to go to last occurrence
- ?<char> → search for <char> backwords
- n → to go to previous occurrence
- N → to go to next occurrence
- # * → find the previous/next occurrence of the exact word under the cursor (search → search)
- g# g* → find the previous/next occurrence of the word under the cursor (search → searching)
- 6* → search for sixth occurrence of the current word under the cursor
Editing
- i a I A → insert/append at/after cursor/beginning/end of line
- o O → open new line below/above the current line
- o<esc> → add a blank line
- cw cW → correct token/world following the cursor
- cc → correct line by clearing and then entering insert mode
- C → correct till end of the line
- dd → delete line
- D → delete till end of the line - same as d$
- ct cf ci ca → correct up to or including specific characters
- dt df di da → delete up to or including specific characters
- s → delete character at the cursor ant then enter insert mode 5s
- yy → copy line
- yw yW → copy token/word
- p P → paste before/after cursor
- u ctrl+r → undo and redo
- . → repeat the previous edit command
- r → replace one character
- R → replace till the escape key is hit
Indent
- >> → indent a line to the right
- << → indent a line to the left
- 5<< → indent 5 lines to the left
- >} → indent one paragraph to the right
- =i} → auto indent everything inside a paragraph
- >aB → indent a Block
- == → auto indent a line
- gg=G → select whole file and autoindent
- ggvG= → select whole file and autoindent in visual mode
Correcting Text
Combination with c or d
- t<char> exclusive match: continue up to the next <char> on this line
- f<char> inclusive match: continue up to the next <char> on this line
- i<char> exclusive inner match: apply to the text bounded by <char>
- a<char> inclusive inner match: apply to the text bounded by <char>
Say that I have the code below, and I want to completely replace the code inside the map() with something else:
signal.map(count -> String.format(“%d cookies, ah ah ah”, count));
The first thing I need to do is get my cursor anywhere inside the parentheses belonging to map(). The exact command I use depends on where I end up:
If the cursor is just inside the open paren for map(), I could use “cf)”. This corrects all text up to and including the next “)” on this line.
Say the cursor is on the word “format”. I would do “ci(”. Vim will search backward to find the first open paren, then search forward to find its match, and correct the text between (but not including) those characters.
Maybe my cursor was already closest to the word “cookies.” To break out of the inner parentheses, I would need to add a count and do “2ci(”. This is almost identical to the last example, except that the 2 is needed due to the nested parentheses.
Some usefull commands:
- dw → delete to the end of a word
- diw → delete the entire word at the cursor
- cit → change the content inside an HTML tag
- cip → change the content inside a paragraph
- ci" → change the content inside a “…”
- y4j → copy 4 lines
Working with files
- vim file.txt → open file test.txt
- vim :e test.txt → open file test.txt from vim
- vim :wq test.txt → save as test.txt
Tabs
- gt → go to next tab
- gT → go to previous tab
- 2gt → go to second tab
Explore file system
- :e. → open file explorer at current working directory
- :sp. → open file explorer in split at current working directory
- :vs. → open file explorer in vertical split at current working directory
- :Se → open file explorer in split at directory of current file
Visual mode
- Ctrl + v → visual block
- v → visual
- vW → select word
- vjjjJ → select three lines and join them to one
Search and replace
- :%s/foo/bar/g → change each ‘foo’ to ‘bar’ in all lines
- :s/foo/bar/g → change each ‘foo’ to ‘bar’ in the current line only)
- :%s/foo/bar/gc → change each ‘foo’ to ‘bar’, but ask for confirmation first
- :%s/<foo>/bar/gc → change only whole words exactly matching ‘foo’ to ‘bar’
- :%s/foo/bar/gci → change each ‘foo’ to ‘bar’ case insensitive
- :%s/foo/bar/gcI → change each ‘foo’ to ‘bar’ case sensitive
- :%s/foo//gn → show count number of matches of ‘foo’
- :%s///gn → show count number of matches of last searched pattern (if * used on some word)