Vim is awesome! I can’t see living without it. So, I blogged about some its features to invite you to know more about this powerful text editor.

Trying to make a simple way to explain it and not so basic at the same time, I defined two main topics: Understanding the User Model Interface and The power of Operations and Registers.

This post is not a step by step how to start with Vim, it’s just to show you some features which make you think about this powerful text editor.

If you would like to have a good reason to use Vim, it’s time!

Understanding the User Interface Model

Vim has 6 modes (NORMAL, INSERT, VISUAL, SELECT, COMMAND-LINE and EX-MODE), It’s common to see people trying to edit some files with Vim without understanding how it works. But, believe me! After understanding how Vim modes work, you will see that is really difficult to live without it.

I’ll talk about the most useful to me: NORMAL, INSERT, VISUAL and COMMAND-LINE, If you want learn about other modes, I recommend to read the Vim Help via :h

Normal Mode

It’s default mode, we normally use it to nagivate. Navigation with Vim is very easy using the keys:

left down up right
H J K L

It could look weird to beginners, but when you are adapted to use these navigation keys, you realize the amount of productivity is “ready to type position”.

Look that navigate in the document with fingers close to keys permit you be in ‘type zone’, it’s great and faster! A simple combination like Go, you’ll go to last line of buffer and change to INSERT MODE ready to write.

Insert Mode i

That’s moment with you write the content. Should I really talk about it? the common situation found in the most traditional editors. To change to this mode, just press i. To back to NORMAL MODE, ESC.

Visual Mode v

We could select and change text with this mode! To active it, just press the key: v. Vim will select content based on your navigation. Example: To select everything until the final line, press: $.

A common situation is make operations like delete d, change c and copy y to selected text.

If you’re asking yourself, why is c key to copy? The c key already is used to change action and Vim needs to found another key, yank was the term choosed to copy operation.

Via VISUAL MODE, you can select text, apply all navigation keys used in the NORMAL MODE.

Visual Block <C-v>

A subtype of VISUAL MODE I use a lot is VISUAL BLOCK, we could select text in blocks defining lines and columns. When could we use it? Imagine we have a sequence of operations with we would change the machine word to other word:

machine.add(milk)
machine.make(coffee)
machine.clean

With the cursor positioned to begin the first line, we could press <C+v> to change to VISUAL BLOCK mode and execute 2jec, after it, type other and ESC to back to NORMAL MODE. we will have result:

other.add(milk)
other.make(coffee)
other.clean

Command-line Mode :

Vim is a clone of vi and its ancentry is ed. Based on it, we have Ex operations brought of ed in the COMMAND-LINE MODE. Again, the numbers of operations that we could make in this mode is huge, i’ll talk about some those I normally use.

To change to COMMAND-LINE MODE, we use the : key. Typing a command name and <CR>(Enter), we execute it. To explain better it, Imagine that you want to change verto to Everton word, so, we could make something like it:

:%s/verto/Everton/g

Where, % is the range of content to execute the command, In this case, % is a reference to entire document. s/<de>/<para>/ is a sintax to substitute ( we could use the shortcut s) The g is a flag to substitute all occurrences in the line. We could use c flag too (example: gc) to request confirmation to each occurrence.

Another command I use sometimes is !, it allow me to execute shell command. Example: :!ls.

Another way to change to COMMAND MODE is via /, where we could find texts. Example: /verto<CR>. Vim will mark all occurrences as highlight in the document. You can navigate between occurrences using n to next and N to previous.

Vim Modes are awesomes, it’s weired to me when I don’t have these options editing and reading documents. The learning path could be a bit large to feel productivity, but when you’re more comfort with it, you won’t accept use simple editors like we have in the market.

The power of Operations and Registers

Using Vim we have many ways to make something. I’ll show some tips to make big changes using short and fast commands.

To be more productivity with Vim, you need to understand how to use less keys to make more things. That’s the moment to know about Operations and Motions.

Operations and Motions operation{motion}

Everyday we always needs to change or delete text that we’re working. Using Operations and Motions, we can execute this tasks. We can remove an entire line via dd or just a word with daw.

Some useful operations

operation description example
c change ciw change the current word
d delete diw delete the current word
y yank yiw copy teh current word
gu or gU make lowercase or uppercase giU make the current word as uppercase
> or < shift right or left >> shift text to right
= autoindent == indent line

To read more about operations and motions, please read Vim Help

Look that via combinations of operations and motions, we can make changes using short steps. Imagine that we have a code like chat.sendMessage("Vim is awesome") and we want to remove "Vim is awesome" value to a named variable called message.

Using 2 short steps, we can make:

fV + ci( and type message.

the final result will be: chat.sendMessage(message)

Explaining a bit more…

  1. fV find for the word V
  2. ci( change everything inner of ( and change to INSERT MODE
  3. type message variable

Repeat operations? . q{word}

Always when we need to change something that occurs many times, Vim show us its power to help to make this boring task.

Dot command .

the dot . is used to execute the last change. For Example, the Scala language, () to call method is optional, the example bellow, we want to remove it. We’ll use the . to repeat the last change.

server.initialize() // initializing the server
server.runScripts() // run all scripts
server.shutdown() // shutdown server to save money

Moving the cursor to ( using the keys: f(, we execute the next step: da( (delete entire content inner of () including itself) Now, In the others lines, we make the same change but, instead of use da( we just use . to repeat the last change.

Final result

server.initialize // initializing the server
server.runScripts // run all scripts
server.shutdown // shutdown server to save money

Macros q{word} @{word}

The last example we have a boring task to always move to next () for execute the dot command. A good way to solve it, is recording a sequence of operations and execute using a single command. To make it, we could use Vim Macros. Via q{word} keys, Vim will start a record of sequence commands that could be execute via @{world}. Let’s make an example: We will record a sequence of commands to find (, remove the () via da( and back to begin the next line.

  1. Start record of a macro: qa
  2. Move to caracter (: f(
  3. Remove () and back to NORMAL MODE: da( + ESC
  4. Move to begin the next line: 0j
  5. Finish the record: q

Now, we could via @a command, execute changes to others lines. (via Command-line, we could change all using a single command: :%normal @a)

It was a simple example to show how Vim Macros could be useful to repeat tasks. Save it with you and will see it saves these boring moments. :D

Registers "{register}

If you already use Vim, even when you just edit a file in the remote machine, you already had been a “weird” experience of copy and paste text. Something like copy a selected text using y, go to some line, delete it via dd and press p to paste. Ups! the pasted content is the deleted line. It occurs because vim store way called registers.

Using registers, we could store texts and command to be accessed when we need it. Vim has many register types, i’ll talk about Unnamed "", Numbered "0 to "9 and Named "a to "z or "A to "Z. If you want to learn about others, I recommend to read the Vim Help.

The default register is called"" (Unnamed register), In the other words, when we copy or delete text, Vim store the data in the unnamed register. There is a way to address to a specific registry defining it before the operation.

Example, copying a selected text and store to named register "n: "ny

We could verify the registry list via Command-line using the command: :reg. We’ll see that in "n registry, we have the recent copied text. If you rememeber of the @a macro, you’ll realize that it’s stored in the named registry: "a.

Always that we’re using yank or delete command, Vim store its data too in the specific registrys, the Numbered Registers.

The "0 has last copied text and the "1 last deleted text. So, when we make a copy, we have the scenario where Vim store the copied text in 2 registers: Unnamed "" and Numbered "0.

The scenario used in the begin this topic, when we make the y operation, Vim store its data in the registers: "" and "0, so, when we delete the line via dd, the deleted content is stored in "" and "1. When we press p to paste, we’re getting the Unnamed register content.

Now that you understand how registers works, it’s not difficult to find solution to problem above! We just need to specific the registry has the copied text. In this case, the register of last yank: "0p.

Conclusion

When you understand how to use operations and motions, you could reduce drastically the number of steps to make things using Vim. I hope you have learned how useful Vim could be to make your tasks easier. There are others resources that I didn’t talk here because my idea wasn’t make a complete tutorial about Vim. I recommend to read the references posted here and try to find the best resources to help you.

Vim is a complete and powerful text editor ready to make your life better.

References