VI editor tips

Time:2022-6-6

‘vi is a UNIX like operationsystemOne of the most widely used text editors in. This tool is provided by default for UNIX, Solaris, or Linux. It can be said that as long as you are familiar with VI, in any kind of UNIXoperationsystemText editing is freely available on the platform. There are many materials about the basic usage of VI. The purpose of this article is not to repeat these basic usage, but to provide some practical skills for those who have a preliminary grasp of the basic usage of VI, so that they can use VI to edit files more quickly and efficiently.

VI has command mode and insert mode. VI is in command mode after startup. In command mode, you can freely move the cursor, copy and paste text, etcoperation。 Commands such as “a”, “I”, “C”, “C”, “O” and “O” can be used to switch from command mode to insert mode. In insert mode, you can enter text and press ESC to switch back to command mode. In order to distinguish commands in different modes, this article will indicate which mode they are in during the introductionoperation

In addition, alloperationBoth are implemented in VIM provided by red hat 9.0. Most of the techniques described in this article are also feasible in various VI versions.

Encryption and decryption

Encrypting and decrypting files

Enter the “: X” command in the command mode, then enter the password, confirm the password, and finally enter the “: WQ” command to save and exit. The file is encrypted.

Pass abovemethodThe encrypted file becomes a ciphertext file. To read the original plaintext, the user will be prompted to enter the password after opening the file. If the password is entered correctly, you can see the original plaintext. In addition, you can see the prompt that the file has been encrypted on the last status line.

Unencrypt

UnencryptedmethodIt’s simple. When you enter the “: X” command in the command mode, you will be prompted to enter the password. At this time, you can directly press enter. Next, when you are prompted to enter the password repeatedly, you can also directly press enter, and finally enter “: WQ” to save and exit. In this way, the encryption of the file is cancelled.

Note that the above encryptionmethodIs in VIM running on Red Hat Linux 9.0. There may be slight differences on other platforms. Please refer to the help of the actual platform or version.

replaceoperation

Some simple replacement commands frequently used are:

◆ s replaces the current character with one or more characters. For example, 5 s means replacing the five characters starting from the current character with one or more characters;

◆ s replaces the current line with one or more characters;

◆ r single character replacement.

Text format conversion

To convert DOS formatted text to UNIX formatted text, the command is as follows:

:1,$s/^M//g

Where ^m is pressed at the same time ctrl+v+m, indicating enter.

Add a string at the beginning and end of a line

Use the following command to add the string “newString” at the beginning of each line of text. Please note the existence of spaces.

:g/^/s//NewString/g :%s/^/NewString/g

Add the string “newString” at the end of each line with the following command:

:g/$/s//NewString/g :%s/$/NewString/g

Insert the string “newString” from the third line to the beginning of the last line of the text:

:3,$s/^/NewString/

Replace in entire file

To replace a specific string in the entire file, use the following command:

:%s/old_string/new_string/g

If you only replace “old_string” with “new\u string” in lines 3 through 7 of the file, use the following command:

:3,7s/old_string/new_string/

Note that “s” refers to the replacement action, “%” refers to all lines, “g” refers to the full text, “^” refers to the beginning of the line and “$” refers to the end of the line.

Power programming

Syntax highlighting

Write a program in VI. if you want to enable the syntax highlighting function for convenience, just enter “: syntax on” in the command mode. Similarly, if you want to turn off the syntax highlighting function, enter “: syntax off” in the command mode.

Quick view function help

When reading and writing programs (mainly C or c++ programs) with VI, if you need to understand the specific functions of a function, you can move the cursor over the function, and then enter “K” in the command mode to call the man help for this function in the “Linux programmer’s manual”.

Mark positioning

When reading and writing large program files, it is very helpful to use the mark (bookmark) function to locate. I suggest you use it more. The following is a simple example.

First, open a source code file and move the cursor to the position you want to mark. If a tag named “debug1” is made, the user can enter the marked command “mdebug1” in the command mode, and then press the Enter key, and a tag named “debug1” is completed.

Next, the user can move the cursor to other positions at will. After entering “`debug1” in the command mode, the user can quickly return to the beginning of the line where the “debug1” tag is located.

“Ctags” command

When reading and writing C or c++ programs, you can use the “ctags” command to scan the C source programs in the current directory. The command is as follows:

$ ctags *.c

The above command will be in the current directory C file generates the tags file, and then use the “Ta function name” command to quickly open the file where the corresponding function is located, and position the cursor at the beginning of the corresponding function.

Auto align

Use the “: set AI” command in command mode to automatically align the rows in the program.

Show match

In programming, {}, [], () often appear in pairs, and often have multiple levels of nesting, which is easy to miss and make mistakes. So the following technique is very useful. Using the “: set SM” command in the command mode will display the corresponding matching {, [, (when the user clicks),],}.

Abbreviated text

In programming, it is often necessary to input some repeated and long information, such as long nouns and company information. At this time, the abbreviation function of VI can be used. It can replace a long word with a short abbreviation. For example, for a long URL, you can give it an abbreviation and use “: ab myurl” in the command mode http://www.mykms.org/kms/myproject/index.php?id=0023 ”Then, after entering “myurl” in the insert mode, “myurl” will automatically become the long URL just defined. In addition, entering “: una myurl” in the insert mode cancels the abbreviation “myurl”.

Set option

Use “: set” toset upVI environment variables. There are many environment variables for VI. Use “: set all” to display all environment variables. Use “: set variable name and parameters” toset upCorresponding environment variables.

The following command displays the line number:

:set number

The following command makes the file read-only:

:set readonly

The following command makes VI in “: n” and “:!” Automatically save the file before the command:

:set autowrite

The following command shows which mode the user is in:

:set showmode

Similarly, “: set noshowmode” turns off this display.

There are many more VI environment variables. Understanding them will bring more convenience for users to use VI. Users can find out the detailed functions of the corresponding environment variables through the online help of vi.

Using macros

VI macro provides more powerful functions. Users can customize their own macros according to their own needs, and then complete a set of pre-defined macros through macrosoperation。 Define a macro with “map” and delete it with “unmap”. After you are familiar with the basic commands of VI, you can complete complex work by combining these commands. If the combination of these commands is defined as a macro, it will be convenient for users to call them frequently. This is a very powerful function. I suggest you use it more.

summary

VI is UNIX like operationsystemThe default editor commonly used on. Knowing VI, users can use it to edit files efficiently. But the process of learning VI is very long, because it has many functions. But it is a tool worth the time to learn. The so-called “if you want to be good at something, you must first sharpen your tools”. I hope everyone can learn VI well and make good use of this sharp tool of text editing.
 

Recommended Today

The implementation in python outputs the bases in the fasta file with the specified number per line

  001. Test data [[email protected] test2]# ls a.fa test.py [[email protected] test2]# cat a.fa ## Test data >OR4F5_ENSG00000186092_ENST00000641515_61_1038_2618 CCCAGATCTCTTCAGTTTTTATGCCTCATTCTGTGAAAATTGCTGTAGTCTCTTCCAGTTATGAAGAAGGTAACTGCAGAGGCTATTTCCTGGAATGAATCAACGAGTGAAACGAATAACTCTATGGTGACTGAATTCATTTTTCTGGGTCTCTCTGATTCTCAGGAACTCCAGACCTTCCTATTTATGTTGTTTTTT >OR4F29_ENSG00000284733_ENST00000426406_20_955_995 AGCCCAGTTGGCTGGACCAATGGATGGAGAGAATCACTCAGTGGTATCTGAGTTTTTGTTTCTGGGACTCACTCATTCATGGGAGATCCAGCTCCTCCTCCTAGTGTTTTCCTCTGTGCTCTATGTGGCAAGCATTACTGGAAACATCCTCATTGTGTTTTCTGTGACCACTGACCCTCACTTAC [[email protected] test2]# cat test.py ## Conversion script #!/usr/bin/python dict1 = {} in_file = open(“a.fa”, “r”) out_file = open(“result.fa”, “w”) for i in in_file: i = i.strip() if i.startswith(“>”): key = i dict1[key] = “” else: […]