Vim Quick Reference
Tags: computerscience, vim, codenewbie, java
Published: 2020-10-11 15:05:53

The initial struggle with Vim is very real, speaking from the perspective of a VScode user. However, after using Vim for a few months, I definitely feel much better and more comfortable with Vim. 

Especially for shorter Java programs, one will come to appreciate the simplicity and power of Vim in no time. I won't be making Vim my main code editor in the near future, but I definitely do not hate it anymore.

In this article, I hope to note down the **absolutely necessary** commands needed to use Vim as a programming "IDE". Nothing fancy :joy:

P.S There are far comprehensive/detailed tutorials out there, such as vimtutor(if you have Vim installed, simply type `vimtutor` in the command line, it will bring you through some basic lessons). What I hope to achieve here is to share my most helpful commands. I won't be explaining much for this is more of a reference that sieves out the essential commands than to introduce them formally.

---
### Super Basic Understanding Of Vim
When we start with something like `vim Sample.java` and open up the standard Vim editing window, we are going to be in the **Command mode** where the keys we type/press are supposed to be commands that Vim can understand. This is like a control center where we speak jargon that Vim is listening out for.

The other very important mode that we need to know about is the **Insert Mode**. This is where we type the text that is meant to be in the body of our program. This is the mode that we are in when we open up a typical text editor such as Notepad or Microsoft Word.

When you are done with your changes in insert mode, you have to return to command mode before you can issue a quit command.

I prefer using `Ctrl + c` to return to command mode from insert mode because the Esc key can be quite far from the normal typing position and `Ctrl + c` is within reach.

---
<center>Basics</center>

| Key          | For                                             |
|--------------|-------------------------------------------------|
| vim filename | Create/open a file with given filename          |
| i            | Enter insert mode                               |
| Esc /Ctrl + c| Quit insert mode and <br>return to command mode |
| :wq          | Save and quit Vim                               |
| :q!          | Discard changes and quit Vim                    |

<center>Code Related</center>

| Key  | For                  |
|------|----------------------|
| gg=G | Fix code indentation |
| ctrl + n | Auto word completion |
| :!javac *.java | Compile code without quitting Vim|
<center>Navigation</center>

| Key       | For                    |
|-----------|------------------------|
| hjkl   | the equivalent arrow keys |
| gg        | Go to top of screen    |
| shift + g | Go to the bottom of screen |
| 0         | Go to start of line    |
| $         | Go to end of line      |
| Ctrl + f  | Page down              |
| Ctrl + b  | Page up                |

<center>Edit</center>

| Key | For                    |
|-----|------------------------|
| r   | replace a single character|
| o   | add newline above and enter insert mode|
| O   | add newline below and enter insert mode|
| yy  | Copy line              |
| p   | Paste                  |
| dd  | Delete/cut line        |
| dw  | Delete/cut word        |
| d$  | Delete/cut from cursor to end of line        |
| x   | Delete/cut a character |

For copying a block of code:
- press `shift + v` to start selecting visually by navigating up or down. The block of code will be selected, then press `y` to copy. Afterward, simply press `p` to paste.

<center>Common Tasks</center>

| Key          | For                                                   |
|--------------|-------------------------------------------------------|
| u            | Undo                                                  |
| Ctrl + r     | Redo                                                  |
| :s/foo/bar/g | Find and replace<br>"foo" with "bar"<br>for all "foo" |

<center>Multiple tabs</center>
To open a new file in another tab:
- use `:tabe filename`
- e.g. `:tabe test.java`

To navigate between tabs:
- use `gt` to go to next tab
- use `1gt`, `2gt`, `3gt` to go to different tab by number
- e.g. `1gt` goes to the first tab from the left

To get out of Vim for a while:
- use `ctrl + z` to suspend activity
- use `fg` (foreground) to come back to Vim

<center>Common UNIX commands</center>

| Key                | For                                      |
|--------------------|------------------------------------------|
| ls                 | list files in current folder             |
| cd \<directory\>     | enter this folder                      |
| cd ..              | go out of current folder                 |
| rm \<file\>          | delete this file                     |
| mkdir \<directory\>  | make new folder                        |
| mv \<file1\> \<file2\> | Rename file1 as file2                |
| cp \<file1\> \<file2\> | copy file1 to file2                  |
| tab                | for auto completion of possible commands |

---
### The End

I will be updating this article to add/reflect my latest list of frequently used commands. Bye now!

![gif bye](https://media.giphy.com/media/Gzvo3F4TBUm1W/giphy.gif)

*5/12/2020 P.S. I have updated pretty much whatever I found useful, will continue to update this list in the future as this list also serves as my own notes.