The Vim Reference
Tags: vim, productivity, programming
Published: 2021-05-22 17:37:33

Recently I decided to learn vim after hearing how much Ben Awad loves it. I though maybe it could boost my productivity too. I am still getting use to it, but right now it seems pretty fun.

Now to the reference. Here are some useful commands and what they do: 

# Using Vim

- Open Vim: `vim filename`
- Closing Vim:
	-  `:wq` -> save and quit
	-  `:q` ->  quit and not save
	-  `:w` -> save and not quit

### Movements
-  Hitting <kbd>gg</kbd> will take you to the top of file
-  <kbd>G</kbd> takes you to the bottom
-  <kbd>}</kbd> and <kbd>{</kbd> navigate code blocks
-  <kbd>o</kbd> adding a new line and put into insert mode
-  <kbd>O</kbd> adding a new line above and put into insert mode
-  <kbd>w</kbd> will jump to next word <kbd>b</kbd> will take you back
-  `:number` will take you to line number
-  <kbd>0</kbd> will take you to the start of the line
-  <kbd>0 + w</kbd>  or <kbd>^</kbd> will take you to the first word of a line
- `h j k l` to move up down left and right 
- <kbd>a</kbd> move over by  1 and set insert mode
- <kbd>A</kbd>  jump to end and set insert mode 

### Editing 
- Hitting "i" in command mode lets you insert 
- Hitting <kbd>shift + i</kbd> will set insert mode at the beginning of the line 
- Then <kbd>esc</kbd> puts you back into command mode 
- Hitting <kbd>dd</kbd>  will delete a line and copy to clip board 
- Hitting <kbd>d + }</kbd> will delete a whole block of code
- Hitting <kbd>10 dd</kbd> will delete 10 lines
- Hitting `u` will undo 
- Hitting <kbd>ctrl + r</kbd> will redo
-  Hitting <kbd>yy</kbd> will copy a line
-  <kbd>p</kbd> to paste bellow <kbd>P</kbd> to paste above
-  <kbd>x</kbd> remove a letter where the cursor is
-  <kbd>number x</kbd> remove number letters before cursor
-  <kbd>~</kbd> swap the case of the letter prefix with number for multiple characters
-  <kbd>.</kbd> redo

-  <kbd>$</kbd> will take you to the end of a line
-  <kbd>W</kbd> will jump spaces and <kbd>B</kbd> reverses that
-  <kbd>f + char</kbd> find and place cursor on char
-  <kbd>t + char</kbd>find and place cursor before char
-  <kbd>%</kbd> take you to and from closing brackets
-  <kbd>d + %</kbd> remove code between brackets
-  <kbd>c + w</kbd> change mode and word will remove a word and set insert mode
-  <kbd>D</kbd> remove everthing from cursor onwards
-  <kbd>c + t + char</kbd> edit from cursor to char
-  <kbd>C</kbd> change whole line
-  <kbd>d + t + char</kbd> delete from cursor to char
-  <kbd>*</kbd> take you to occurances of the word where your cursor is
-  <kbd>;</kbd> take you to next instance after using a find command
-  <kbd>r + char</kbd> replace letter at cursor with char prefix with number for multiple replaces
-  <kbd>R</kbd> allow to replace lots of letters into replace mode
-  <kbd>/word</kbd> highlight all occurances of word

### Macros
- Press <kbd>q</kbd> to start recording then select a hotkey. Now run some commands and hit <kbd>q</kbd> to end. Then hit <kbd>@ hotkey</kbd> to replay a command. Prefix `@` with a number to replace that many times

### Visual Mode: <kbd>V</kbd>
- Select text with movement commands then you can hit:
	- <kbd>d</kbd> to delete
	- After selected <kbd>&gt;</kbd> or <kbd>&lt;</kbd> to indent code.
- <kbd>shift + v</kbd> selects the whole line and puts you into visual mode.
- <kbd>v + move</kbd> select from cursor while move

#### Visual Block Mode
<kbd>ctrl + v</kbd> enter visual block mode. Now you can select columns and hit `r` to replace things.
In visual block mode hitting <kbd>shift i</kbd> will allow you to type and then after you hit <kbd>esc</kbd> the text will appear in all places selected.

---


Thank you for referring to this guide, I hope you found it useful.

---

> Photo by Bich Tran from Pexels