Feb 212015

Sometimes, while editing a text file in vim, you might need to insert, rearch or replace characters that are not in your keyboard, such as  æ, å, ě, … or non-printing characters such as the control characters^A, ^B, … or characters with hexadecimal codes in the range 0x7F to 0xFF.

This post explains some of the possible ways that vim offers to handle those characters.

Obtaining the hex code of a character:

The hexadecimal code of a given character present in the file that is being edited can be obtained by placing the cursor on top of it, and pressing “ga”. A line will be displayed at the bottom of the window showing the hexadecimal and octal coding of the character. For instance:

This example shows that character Ѳ is coded as 0472 (utf8 encoding)

Inserting a character by entering its hex or unicode code

To insert a character with a given hex code, press Ctrl-V, followed by “x”, and then enter the hex code of the character. For instance:

To insert a character with a given unicode code, press Ctrl-V followed by “u”, and then the unicode code. For instance, to insert Ѳ:

Searching a character with a given hex or unicode code:

In a search or replace expression, a character can be specified as “\%xhh”, where hh is the hex code of the character. For instance, to search a character with hex code 9d:

In the same way, a character can be specified as “\%uhhhh”, where hhhh is the unicode code of the character. For instance, to search the next occurrence of character Ѳ:


The most common characters in western languages can be inserted pressing Ctrl-K followed by a two-letter combination (known as digraph) specific for the desired symbol. For instance, the sterling pound symbol £ can be inserted pressing ^KPd, and the ä character can be inserted pressing ^Ka:.

Besides, digraph mode can be enabled with the command “: set digraph”. In digraph mode, accented characters can also be inserted using the backspace key. For instance, character ä can be inserted as a<BS>: ( “a” + backspace + “:” ); character ê can be inserted with the sequence “e” + backspace + “>”

Finally, all digraphs known to vim can be listed with the command “:digraphs”:

In this list:

  • the two first characters are the sequence to be entered after Ctrl-K.
  • next appears the resulting symbol
  • and then, the decimal unicode the symbol

As you can see, there are hundreds of available digraphs, including digraphs for the control characters with hex codes 0x00 to 0x1F, non-printing characters with hex codes in the range 0x7F to 0xFF, currency symbols (sterling pound, yen,…) and most non-ASCII symbols used in western language, as well as other symbols commonly used (Copyright symbol, etc.)

Reloading an open file, reading it as utf8

Vim detects the encoding used in a file and opens it accordingly. But it might happen that a text file encoded in utf8 contains a control character. In this case, vim interprets the encoding of the file as “Non-ISO extended-ASCII text”, and as a result the utf8 characters are not correctly displayed. If this is the case, you can force vim to reload the file as utf8, with the command:


Related posts

 Posted by at 11:13 am

 Leave a Reply