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.
|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|
|gg=G||Fix code indentation|
|ctrl + n||Auto word completion|
|:!javac *.java||Compile code without quitting Vim|
|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|
|r||replace a single character|
|o||add newline above and enter insert mode|
|O||add newline below and enter insert mode|
|d\$||Delete/cut from cursor to end of line|
|x||Delete/cut a character|
For copying a block of code:
shift + vto start selecting visually by navigating up or down. The block of code will be selected, then press
yto copy. Afterward, simply press
|Ctrl + r||Redo|
|:s/foo/bar/g||Find and replace|
"foo" with "bar"
for all "foo"
To navigate between tabs:
gtto go to next tab
3gtto go to different tab by number
1gtgoes to the first tab from the left
To get out of Vim for a while:
ctrl + zto suspend activity
fg(foreground) to come back to Vim
|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|
I will be updating this article to add/reflect my latest list of frequently used commands. Bye now!
*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.