Read and write details of special characters in Vim

  • 2020-05-13 04:21:14
  • OfStack

1. Look for special characters

The visible special characters in Vim are displayed directly, and the invisible special characters are displayed as the character's input on the command line, for example \r is displayed as ^M. All special characters that can be entered in Vim can be seen through :help digraph-table. Page 1 of this document is as follows:


char digraph hex  dec  official name
^@   NU   0x00  0  NULL  (NUL)
^A   SH   0x01  1  START OF HEADING (SOH)
^B   SX   0x02  2  START OF TEXT (STX)
^C   EX   0x03  3  END OF TEXT (ETX)
^D   ET   0x04  4  END OF TRANSMISSION (EOT)
^E   EQ   0x05  5  ENQUIRY (ENQ)
^F   AK   0x06  6  ACKNOWLEDGE (ACK)
^G   BL   0x07  7  BELL (BEL)
^H   BS   0x08  8  BACKSPACE (BS)
^I   HT   0x09  9  CHARACTER TABULATION (HT)
^@   LF   0x0a  10  LINE FEED (LF)
^K   VT   0x0b  11  LINE TABULATION (VT)
^L   FF   0x0c  12  FORM FEED (FF)
^M   CR   0x0d  13  CARRIAGE RETURN (CR)

The first column is the special character, the second column is the digraph (see below), the third column is the base 106, the fourth column is the base 10, and the fifth column is the official name of the character.

Display the binary encoding of the current file can be input: %!xxd , the command will replace the file content! Recovery: : %!xdd -r .

2. Enter through digraph

There is obviously no special character selection tool in Vim, but there are two ways to enter special characters:

1. Enter a special character (digraph) with two characters.

2. Enter the encoded value (ASCII or Unicode) directly.

Among them, digraph is a kind of double-spelling method, in which two characters are continuously input to represent a special character. You need to press the leading button first < Ctrl-K > For example, enter in edit mode:


<Ctrl-K>Rg

Will appear. Character where "Rg" is the digraph (double spelling) of the character. All digraph can be accessed through: help digraph-table The query.

3. Input by character encoding

In addition to digraph, you can enter it directly through the character encoding, which is not required in digraph-table of Vim. This is also done in insert mode by pressing the leading key <Ctrl-V> (Windows < Ctrl-Q > ).

There are five ways:

The base 10 value is ASCII: ^Vnnn (000) < = nnn < = 255)

Base 8 value: ^VOnnn or ^Vonnn (000) < = nnn < = 377)

Base 106 value: ^VXnn or ^Vxnn (00) < = nn < = FF)

Base 106 BMP Unicode: ^Vunnnn (0000) < = nnnn < = FFFF)

Base 106 is any Unicode: ^VUnnnnnnnn (00000000) < = nnnnnnnn < = 7FFFFFFF)

The above operations are performed under the Unicode character encoding setting. Such as:


<Ctrl-V>065

It will output the A character, 65 is its ASCII encoding, and Unicode is compatible with ASCII.

4. Line feed search/replace/input

The behavior of newline in Vim is very special and not enough to be 1, so we need to discuss 1 separately.

First distinguish 1 \r and \n:

The former is carriage return (Carriage Return), which is passable in Vim < c-k > CR input, shown as ^M.

The latter is line feed (New Line), which is passed in Vim < CR > (enter) key input, display as enter and line feed;

So for Windows style newline (\r\n) in Vim it will show ^M at the end of each line.

replace

Note: when the s command is replaced with a newline (New Line), \r (equivalent to the enter key) should be used instead of \n. For example, replace all commas with newlines:


:%s/,/\r/g

If you use \n, the target is replaced with the null character NULL (shown as ^@).

To convert the DOS style newline (\r\n) file to Unix style newline (\n) is very simple and does not require manual search and replacement:


:set fileformat=unix 
:w

search

The \n character should still be used when searching for newlines in search mode (/), because the newline of Vim (Unix style) is really \n and not \r\n. Such as:


/foo\nbar

Can be matched to all:


foo
bar

5. View invisible characters

In addition to special characters, ASCII has a large number of invisible characters, such as space carriage returns, tabs, and so on. These characters can be displayed or not through the list variable:


"  Show hidden characters 
:set list
"  Do not show hidden characters 
:set nolist
"  Sets which hidden characters to display 
:set listchars=eol:$,tab:>-,trail:~,extends:>,precedes:<

conclusion

The above is the whole content of this article, I hope the content of this article can help you in your study or work, if you have any questions, you can leave a message to communicate.


Related articles: