vim also started as vi improved is a very popular text editor for programmers. Often found itself in a battle with emacs, vim is actively developed and used by developers across the world. There is a hesitancy however with using vim mainly because of the learning curve involved.
I often come back to this idea quite often but I was the person who critiqued trying out new things always: Because they don't fit the cutting-edge tech or someone said something about it. Only when you are willing to explore and go hands-on with something you are passionate about, will you move forward. As a programmer it is essential to build habits and workflows around your tools, the better you are at your tools the more efficient you will be.
Going back vi is just an alias to vim, which means vi-improved.
It can be started by running vi
in the terminal.
To edit a particular file in vim, you can run the following command:
vi file.txt
There are 2 main modes in vim:
Command mode: When you first enter vim you are always in command mode.
Insert mode: This is where you can enter text and edit your file.
When you are in command mode you cannot start typing text as you would normally in other editors, you have to be in insert mode to edit your file, to do this press the i
key to enter insert mode. You should see something like -- INSERT --
in the bottom left of your terminal window.
After entering text in insert mode, you can press escape to go back to command mode and then start moving around the file using the arrow keys or h-j-k-l
keys. The h-j
keys are used to move left and right while the j-k
keys are used to move up and down.
These letters or vim commands can be prefixed by a certain number to execute the command a repeated number of times:
5j
would move the cursor five rows down8k
would move the cursor 8 rows up
You can move the cursor between words using the following commands:
w
for moving to the beginning of the next wordb
for moving to the beginning of the previous worde
for moving the end of the current wordshift + w
for moving to the beginning of the next word after whitespaceshift + b
for moving to the beginning of the previous word after whitespaceshift + e
for moving to the end of the current word before whitespace
Beginning/end of line movements:
0
to place the cursor at the start of the line$
to place the cursor at the end of the line
There are patterns and motions (movement around vim) that can make you blazing fast in your coding process which is what makes it a beloved text editor around the world. Some examples of these commands are the d$
command where everything after your cursor till the end of the line is deleted and the dd
command which deletes the entire line. These commands can get you started with editing files in vim.
By far the biggest problem that everybody has faced the first time they've experimented with vim is "How do I save or quit the file?" That's when the panic starts to set in and no matter what you've pressed you don't seem to get out of vim. The following are simple ways to do this:
:w
to save the file:q
to quit the file:wq
to save and quit the file:q!
to quit the file without saving
You can undo any changes by pressing u
in command mode. Once again these basic commands should give you a good start and it only gets more and more complex which can't be explored in this introduction.
Vim can feel very plain for people who switch over from IDEs like VScode, Eclipse, IntelliJ etc. With no IDE-like features such as auto-completion, plugins, or LSP vim is quite barebones but other editors like Neovim solve these issues.
To know more about vim I would recommend checking out the Vim FAQ and running the vimtutor
command in your terminal to start practicing with vim.