Have you ever heard about Vim? You might know it only as the text editor you can't even close if you don't know a key combination. But, once you know Vim, you can edit text files at the speed of light. Let's see why you should learn it and how to start using it!
What is Vim?
From Vim's official page, Vim
"...is a highly configurable text editor built to make creating and changing any kind of text very efficient".
Vim is short for Vi IMproved. Vim is a fork of the Unix vi editor. Vim is free and open-source. It dates back to the times where the arrow keys and the Esc key were in the middle row of keyboards.
Vim is a command line editor. However, you can find it these days outside the command line with a graphical user interface. You can even bring the Vim experience to some IDE's using plugins or extensions.
What makes Vim different?
Vim is a distraction-free text editor. You won't find fancy toolbars full of icons. You will find a blank canvas ready to start.
Vim works in three modes: normal, insert, and visual. In each mode you can perform only certain type of actions. In normal mode, you can run commands on your text; copy and paste, for example. In insert mode, you can type words and symbols. In visual mode, you can select text.
Vim includes the concept of macros. You can record a sequence of actions and repeat it over a stream of text. Using macros, you don't need to retype the same key combination over and over on the text you want to edit.
Vim integrates with your command line. If you need to compile, run your tests or do any other action in the command line, you can do it without leaving your editor. Even you can import the output of a command right into your text.
Vim can be extended and customized. You can bring your favorite color scheme and define your own key shortcuts. There are lots of plugins to enhance your experience with Vim.
Why you should learn it? A note on Productivity
Vim helps you to stay away from your mouse. It reduces the small context switching of reaching your mouse making you a bit faster. You can move inside your files without leaving your keyboard. Your hands will be in the middle row of your keyboard.
Vim can go to almost anywhere in a file with a few keystrokes. You can go to the previous or next word, to the beginning or end of your line and file, to any character in the current line.
Vim uses text object motions. You can copy, change or delete anything inside parenthesis, braces, brackets and tags.
Let's say your cursor is at the beginning of a line and you need to change the parameter list of a method. How would you do it? What keystrokes do you need?
Without Vim, you place your cursor in the opening parenthesis with the mouse. Then, while holding
Shift, you press the right arrow until the closing parenthesis.
But, with Vim, you need fewer keystrokes. You go to the opening parenthesis with
f(. Then press
ci( to change everything inside the parenthesis. Faster, isn't it?
How to start learning Vim
Vim has a step learning curve. Don't try to do your everyday work with Vim right from the beginning. It can be frustrating and unproductive.
Start editing one of your files from the command line or from an online Vim emulator outside of your everyday work.
If you are working in an Unix-based operating system, chances are you have Vim already installed. If not, you can install it from its official site or with a package manager, such as Brew, apt-get or chocolatey, depending on your operating system.
How to exit Vim
Let's answer the question from the beginning of the post once and for all.
First, how can you exit Vim if, by any change, you end up inside it? Did you try to commit from the command line without changing the default editor? Press
:q! and Enter. It will exit Vim and discard any changes. That's it!
To exit saving your changes, press
:wq and Enter. Also, you can press
ZZ and Enter. Your changes will be saved this time.
How to enter and exit modes
You need to switch back and forth between the three modes: normal, insert and visual modes. In a normal text editor, you work in the three modes at the same time. But with Vim, you need to switch between them.
To open a file with Vim, from the command line type
vim <your-file>. Change
<your-file> with the actual name of the file. When you open a file, you will be in normal mode.
To start editing your file, you need to switch to insert mode. Press
i. You will see the
-- INSERT -- label in the status bar. Now, you can type any text. To exit insert mode, press
Esc. You're back to normal mode.
To select text, switch to visual mode pressing
v. The status bar will display
-- VISUAL --. You can select lines of text like using the mouse. Again, to switch back to normal mode, you need
Esc. Don't worry once you're used to these modes, you will switch almost unconsciously.
How to move through a file
With Vim you can use the arrow keys to move your cursor around. But, to keep your hands in the middle row, you can use
h, j, k, l instead of Left, Down, Up and Right, respectively.
Instead of using arrow keys to move the cursor one position at a time, learn some basic motions. These are some of them:
||Go to the previous word|
||Go to the next word|
||Go to the beginning of the current line|
||Go to the end of the current line|
||Go to the beginning of the file|
||Go to the end of the file|
||Go to line with number
||Go to the next instance of
||Go to the beginning of the current line and enter insert mode|
||Go to the end of the current line and enter insert mode|
||Go to the previous paragraph|
||Go to the next paragraph|
For example, if you forgot to add a semicolon in a line, in normal mode you would need to type
||Go to the line number
||Go to the end of the line and enter insert mode|
||Go back to normal mode|
How to copy and paste
A common task while programming and editing text in general is to copy and paste. Vim can copy and paste too. Well, how do we copy and paste with Vim? Vim calls these actions yank and put.
To copy, enter visual mode, select some text and press
y. Then move to the desired place and press
p. Vim will put the yanked text below the cursor position. You can copy an entire line with
What about cut and paste? Vim deletes and puts. To cut and paste, instead of pressing
y to copy, you need to use
d to delete. Then,
p to put the cut text in the desired location. Similarly, to delete an entire line, you need
d, you can change some text with
c. It will remove the selected text and enter insert mode.
You can use
c to edit text inside parenthesis
'" and tags
<>. These patterns of text are called text objects.
Text objects are useful to edit text inside or around some programming constructs. Parameter list, elements of an array, an string, a method body and everything inside an HTML tag.
That's why to change a parameter list, you can use
ci( to change everything inside ().
How to start using Vim?
Once you are comfortable editing and moving around your files with Vim, you can use it inside your IDE. You can start using Vim inside Visual Studio installing VsVim extension and inside Visual Studio Code with VSCodeVim.
You might want to have a cheatsheet next to you. So you can look up command if you get stuck. Don't try to learn all the commands at once. Learn one or two commands at a time and practice them.
Voilà! Now you know how to exit Vim, to switch between modes and move around a file.
There are more things to cover, like searching and replacing, undoing and redoing, and using tabs, among other features. It's true that Vim has a step learning curve. Give it a try! You might find yourself Viming the next time.