Skip to main content

Vim Quick Reference

ยท 5 min read

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
KeyFor
vim filenameCreate/open a file with given filename
iEnter insert mode
Esc /Ctrl + cQuit insert mode and
return to command mode
:wqSave and quit Vim
:q!Discard changes and quit Vim
Code Related
KeyFor
gg=GFix code indentation
ctrl + nAuto word completion
:!javac *.javaCompile code without quitting Vim
Navigation
KeyFor
hjklthe equivalent arrow keys
ggGo to top of screen
shift + gGo to the bottom of screen
0Go to start of line
$Go to end of line
Ctrl + fPage down
Ctrl + bPage up
Edit
KeyFor
rreplace a single character
oadd newline above and enter insert mode
Oadd newline below and enter insert mode
yyCopy line
pPaste
ddDelete/cut line
dwDelete/cut word
d$Delete/cut from cursor to end of line
xDelete/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
KeyFor
uUndo
Ctrl + rRedo
:s/foo/bar/gFind 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
KeyFor
lslist 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
tabfor 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.