1. Introduction

Adding a comma at the end of a single line is easy, but this can become tedious when working with multiple lines. That’s why we value automation.

Vim has the corresponding toolset to make inserting text at the end of a group of lines easy. In this tutorial, we’ll look into one such method.

2. Manipulating Text With the normal Command

We’ll use the Visual mode to select text and the normal command to make the necessary changes to selected lines.

Let’s look at the steps:

  1. Press the v key to enter Visual mode
  2. Move the cursor around to select lines of text
  3. Press the : (colon) key to enter Command mode
  4. Type norm A followed by the text that we want to insert
  5. Press the Enter key

Let’s see what each of these steps looks like. In the first two steps, we enter Visual mode and select the lines of text we want to modify:

Vim selected text in visual mode

Visual mode allows performing commands on a visually selected part of the text. This can be keyboard commands directly executed in this mode or commands run from the Vim command line. To exit this mode, perhaps we want to restart the selection process, we use the Esc key.

Next, we enter Command mode:

Vim switched to command mode

The ‘<,’> text is automatically inserted representing the visually selected line range.

Notably, the last line where we have left the cursor is also considered selected. Although we can’t see it visually in the command mode, it’ll be included when executing the command.

Our next step is editing the selected text. The normal command takes a sequence of key commands as an argument and executes them as if they were typed in Normal mode. If, for any reason, we need to force the execution, we add !:

:normal {commands}
:normal! {commands}

For our case, we need the A command. A is an insert mode command that appends any text following it at the end of the line:

:normal A...
Vim typing 'normal' command

Finally, we execute the command:

Vim result of editing multiple lines

Voilà, the text we wanted to insert is now appended to the end of each selected line.

3. Text Selection Options

When selecting text, we used the v key to enter Visual mode character selection. There’s also line selection activated with the V key and block selection activated with the Ctrl + v key combination.

There are options to select lines non-visually as well. For example, prepending the normal command with a line number range executes the command on the lines from that range:

:3,9norm A{text to insert}

Also, in case we want to apply the change to the whole file, we can use the % symbol:

:%norm A{text to insert}

The option we choose is up to our own convenience, as long as the lines are properly selected.

4. Conclusion

In this quick article, we looked into inserting text at the end of a group of lines using the normal command. Of course, this isn’t the only way to accomplish the task at hand – Vim has other automation features worth exploring.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.