DEV Community

Liu Yongliang
Liu Yongliang

Posted on • Updated on • Originally published at yongliangliu.com

Vim Quick Reference

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 😂

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.


Basics
Key For
vim filename Create/open a file with given filename
i Enter insert mode
Esc /Ctrl + c Quit insert mode and
return to command mode
:wq Save and quit Vim
:q! Discard changes and quit Vim
Code Related
Key For
gg=G Fix code indentation
ctrl + n Auto word completion
:!javac *.java Compile code without quitting Vim
Navigation
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
Edit
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.
Common Tasks
Key For
u Undo
Ctrl + r Redo
:s/foo/bar/g Find and replace
"foo" with "bar"
for all "foo"
Multiple tabs

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
Common UNIX commands
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

*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.

Top comments (0)