WebDevChallenges Logo
WebDevChallenges

A brief introduction to Vim (Part 6)

Updated June 5, 21
Special commands, insert mode, undo & redo and macros in Vim.

About

Here is the fifth (previous) part of this series: A brief introduction to Vim (Part 5)
Here is the seventh (next) part of this series: A brief introduction to Vim (Part 7)

Special manipulations

There are more manipulations in normal mode we haven’t talked about yet in this series but can come in handy.

  • J to join the line, the cursor is currently on with the following into one line (separated with a space character)
  • r followed by any character to replace the character, the cursor is sitting on
  • R to switch into replace/overwrite mode which enables you to replace multiple successive characters at and after your cursor (press ESC to get back to normal mode)

More possibilities to join insert mode

We learned some ways to switch into insert mode so far (e.g. i or c) but there are more ways which can be helpful in certain scenarios.

  • a switch into insert mode and place the cursor after the current character (Append)
  • A switch into insert mode and place the cursor at the end of the line
  • o create an empty line after the one, the cursor currently sits on, switch into insert mode and place the cursor at the beginning of the new line
  • O create an empty line before the one, the cursor currently sits on, switch into insert mode and place the cursor at the beginning of the new line
  • I switch into insert mode and place the cursor at the first character of the line

Undo and redo

It is possible to undo the performed changes in Vim by pressing u as often as you desire. To redo the changes, simply press CTRL+r.

Macros

Now lets talk about a very important topic called macros. It is possible to record a few actions you perform into a macro and then repeat that macro an arbitrary amount of times you desire.

A macro has to be stored in a register of your choice. Vim offers a lot of registers (from the letter a to the letter z).

Recording a macro

The syntax of recording a macro is the following: q[register][actions_to_record]q

Lets assume we have the following json file and want to convert it into a leaner javascript object by removing the double quotation marks of the keys (left of the colons):

{
  "varname1": "string",
  "varname2": "string",
  "varname3": "string",
  "varname4": "string",
}

First of all, we place the cursor at the first double quotation mark infront of varname1.

Now we want to record a macro for this line which does the following: * Delete the character at the cursor * Jump to the end of the word (where only space counts as word separator) * Go one character back * Delete the character at the cursor * Go to the next line * Go to the first character of the line

And then we want to repeat that action for the rest of the lines (by using relative line numbers as described in A brief introduction to Vim (Part 3) it is really easy to count the numbers if you want to use that macro on multiple ones).

The following sequence would record that macro into register a: qaxEhxj^q

Using / Playing a macro

You can use a macro by typing @[register]. In the previous case that would be @a.

You can prefix that command with a number 3@a to perform that macro on the remaining three lines of code.

Ask me questions

I will be happy to answer your questions in the comments section below. Also let me know if you have any tips or ideas for me to improve my post!