The need to learn how to use text editors in Linux is indisputable, as every system administrator deals with configuration (plain text) files daily, and most times this is done purely using one or more tools from a command-line interface (such as nano, vim, or emacs).

While nano is perhaps more suitable for new users, vim or emacs are the tool of choice for more experienced users due to their advanced capabilities.

But there is yet another reason why learning how to use one of these text editors should be a top priority for you: you may either bump into a CLI-only server or run into an issue with the desktop manager in your GUI-based Linux server or desktop and the only resource to examine it and edit configuration files is the command line.

Between this article and the next of this 2-article series, we will review 15 tips and tricks for enhancing your vim skills. It is assumed that you are already familiar with this text editor.

If not, do yourself a favor and become acquainted with vim before proceeding further: you may want to refer to how to use vim text editor for a very detailed guide on starting with vim.

1. Accessing Online Help in Vim

After you launch vim, press F1 or use :h in ex mode to enter the online help. You can jump to a specific section or topic by placing the cursor on it and then pressing Ctrl+] (Ctrl, then the closing square bracket).

After you’re done, press Ctrl+t to return to the previous screen. Alternatively, you can look up a specific subject or command with :h <topic or command>.

For example,

:h x 

will display the help for the x (delete) command:

Vi Editor Online Help
Vi Editor Online Help

and

:h substitute

will bring up the help about the substitute command (our final tip in this article).

2. Add Bookmarks Inside Vim Editor

If you find yourself editing a file that is larger than one screen, you will appreciate the functionality provided by marks. You can think of a mark in vim as a bookmark – once you place it somewhere, you can go back to it quickly and easily.

Suppose you are editing a 300-word configuration file and for some reason need to repeatedly switch between lines 30 and 150 for example.

First, go to line #30 by entering :30 in ex mode, then return to command mode and hit ma (m, then a) to create a mark named “a” in line 30.

Then go to line 250 (with :250 in ex mode) and hit `a (backtick, then a) to return to mark a in line 30.

You can use lowercase and uppercase letters to identify marks in vim (now repeat the process to create a mark named A in line #250).

You can view your marks with

:marks aA
Marks Usage in Vim Editor
Marks Usage in Vim Editor

As you can see, each mark is referenced by a specific line/column position on the file, not just by line.

3. Effortless Code Cleanup in Vim

Suppose you’re editing a shell script and realize the previous developer was rather lousy when it comes to indentation. Let’s see how you can fix it with a couple of vim commands.

First, select a visual block by placing the cursor at the start of the block, then pressing Ctrl+v (Ctrl, then v).

  • To indentate to the left: press <j
  • To indentate to the right: press <j

Then press the . (dot) command to repeat either indentation. The selected block will either move to the right or to the left with only one keystroke.

Another classic example of using the dot command is when you need to delete a series of words: place the cursor on the first word you want to delete, then press dw.

To continue deleting the next words, just press . (shorter and easier than repeating dw several times).

4. Inserting Unicode Characters in Vim

If your keyboard layout does not allow you to easily insert special Unicode characters in a file, or if you find yourself in front of a server with different language settings than the one you are used to, this trick will come in handy.

To do this, press Ctrl+v in insert mode followed by the letter u and the hexadecimal numeric code for the character you want to insert.

You can check the Unicode charts for a list of special characters and their corresponding numeric codes.

For example,

Ctrl+v followed by returns
u0040 @
u00B5 μ
u20AC

5. Incorporating External Command Output in Vim

There will be times when you will need to insert the output of external commands directly into a file being edited with vim.

For example, I often create a variable named DIR in my scripts to store the absolute path to the directory where the script resides in order to use it later in the script.

To do that, I use:

:r! pwd 

in ex mode. Thus, the current working directory is inserted.

Another example: if you’re required to use the default gateway somewhere in a script, you can easily insert it in the current file without exiting vim as follows:

:!r ip route show | grep default | cut -f 3 -d " "

6. Appending External File Contents in Vim

If you need to append the contents of a separate file to the one you are currently editing, the syntax is similar to the previous tip. Just omit the exclamation sign and you’re good to go.

For example, to copy the contents of /etc/passwd:

:r /etc/passwd

You may find this tip useful when you need to modify configuration files but want to keep the original ones to roll back to “factory settings” so to speak.

7. Search and Replace a Word in Vim

Once during an exam, I was asked to open a large text file containing random data. The assigned task consisted of replacing each occurrence of the word Globe with Earth (yes, I still remember the exact words).

For those familiar with sed, this will ring a bell – in ex mode, type:

:%s/old/new/g

where old is the pattern to search for and new is the string that will replace it.

In the case described above, I used:

:%s/Globe/Earth/g

to get the job done.

So what about you want to be prompted before making substitutions? Easy. Just add a c at the end of the above command, as follows:

:%s/old/new/gc

The occurrences of the pattern will be highlighted and you will be asked whether you want to replace it with the new string:

:%s/gacanepa/me/gc
Search and Replace String in Vim
Search and Replace String in Vim

where

  • y: yes
  • n: no
  • a: substitute all
  • q: quit
  • l: substitute this occurrence and quit
  • ^E (Ctrl+E): Scroll up one screen
  • ^Y (Ctrl+Y): Scroll down one screen
Summary

In this article, we have started reviewing some vim tips and tricks to add to your text editing skills. You will probably think of several others, so please share them in the comment form below and I will consider covering them in the next and final article of this vim series.

Similar Posts