Linux basic commands-text editing vim

Source: Internet
Author: User

Linux basic commands-text editing vim
GuideThis article describes the general usage of vim (version 7.4,Vim is a powerful text editor and enhanced version of vi.The most common commands for editing a file using vim are:

Vim file is a new file or an existing file. After such a command is executed, the editor is opened to display the file content. :

If a new file is created, the "file" [new file] text is displayed on the left at the bottom. The "0-1" text on the right indicates the number of rows and characters of the current cursor. If an old file is opened, "file" 3L and 66C are displayed on the left at the bottom, indicating the file name, the row number of the current cursor, and the total number of bytes of the file. If there are no characters in the file, it will take characters ~ .

Currently, you cannot edit the file. vim can edit files in several modes. The current mode is normal mode. You can move the cursor and execute commands such as copy and paste. There are also two common modes: insert mode and baseline mode.

Generally, the entry is in normal mode. Press the following key to enter the insert mode:
1. a starts inputting the next character at the cursor position. 2. A starts at the end of the row where the cursor is located. 2. I start to input 4 at the cursor position. i. Enter 5 at the beginning of the line where the cursor is located. o add a row under the row where the cursor is located, and input 6 at the beginning of the new row. O add a row to the row where the cursor is located, and start inputting at the beginning of the new row.

After entering the insert mode, the words "-- insert --" will appear at the bottom. Then, you can enter the words at the cursor position. Return from insert mode to normal mode and Press ESC.

In normal mode, press the following key to enter the baseline mode:
1.: Execute Command 2./forward search 3 .? Reverse search

Press the ESC key twice to return from the baseline mode to the normal mode.

Run the following command in baseline mode: q (quit) indicates to exit the editor. If the file content has been changed, run wq (write quit) to save and exit; if you do not save and exit, run: q! Force exit; force save and exit: wq !.

The following lists some commands that can be executed in normal mode and their functions:

Modify text:

1. dd cut the entire line of the cursor position (saved in the temporary buffer) 2. nddn is a number indicating to cut n rows from top to bottom starting from the current row. p: place the content in the buffer below the current row. 4. A value of is a number, which is equivalent to executing the p command n times. p: place the content in the buffer zone above the current row. 6. y starts copying from the cursor. 6. yy copy the current row (saved in the temporary buffer) 7. nyyn is a number that indicates copying n rows 8 from top to bottom from the current row. r replaces the cursor position with a character 9. R starts from the cursor position and enters the text input mode (ESC exits) 10. u undo the last operation 11. save ZZ and Exit 12. move the cursor: 13. h or the left arrow keys move a grid to the left. 14. l or right arrow keys move the cursor to the Right to 15. j or the down arrow key move the cursor down to a grid of 16. k or the up arrow points move the cursor up to a grid 17. 0 move cursor to the beginning of the current line 18. ^ move the cursor to the beginning of 19 non-blank characters (such as spaces and tab keys) in the current row. $ move the cursor to the end of the current row 20.g _ move cursor to current line non-Blank The end of the character 21. w move the cursor to the beginning of the next word 22. the eindicator is moved to the end of the next word 23. * match the word with the cursor and move it to the next same word 24. # match the word with the cursor and move it to the previous same word 25. f {move the cursor to the next character {in the current row, and {It can be changed to another character 26. f} move the cursor to the last character} of the current Row 27. t, move the cursor to a character before the next comma in the current row 28. t. move the cursor to 29 characters after the last comma in the current row. % move to the other half of the pair brackets, including () {} []. move the cursor to the brackets first. 30. Move the gg cursor to the first line of the file 31.G move the cursor to the first line of the file 32. Press the Enter key to move the cursor to the first line of the next line

Only a part is listed here. Beginners may feel too much to remember. In fact, you only need to remember a few of them, and you can use them. Other functions make your operations easier and faster.

Most of the commands in vim can be used in combination:

For example, to delete the current position of the cursor until the end of the row, run d $;

For example, if you want to insert 50 'word' at the current position, you only need to execute 50 iword ESC and press the ESC key before the 50 words are inserted;

For example, if you want to paste the copied content but want to paste it five times, execute 5 p;

For example, if you want to execute the last command five times, execute 5 .;

Proficiency in these commands will greatly increase the speed of file writing.

In insert mode (normal mode by a, I, o, etc.), enter the beginning of a word, and then press CTRL-P or CTRL-N will automatically complete.

The baseline mode contains three starting characters (:, //, And (:,/,?), Where/and? Used for matching mode search:

For example, search for the string centos in the file:

/centos

After entering this string, press Enter. vim will highlight all matching strings. Press the n key and the cursor will jump to the next matching string and press the N key, the cursor jumps to the previous match. (Think about man queries)

Use? It works in the same way as/, but in the opposite direction.

: You can execute many commands, such as the Save and exit command described earlier: wq.

The following describes some basic-line commands:
1: setnu display row number 2: setnonu hidden row number 3: rfile reads the file content and writes it to the currently edited file. The content is inserted starting from the next row at the current position of the cursor. 4: wfile writes the edited content to a new file. 5: s/pattern/string/Replace the string matching pattern with string6: x, which is the same as: wq. Save and exit. 7 :! Command temporarily leaves vim and executes the shell command. 8: help view help 9:. = show the current row number 10: = show the total number of rows 11: n move the cursor to the beginning of the n row
These commands can also be combined to execute shell commands and write the results to the next row of the current row: r! Ls-lCommands in other modes can also be combined, for example, replacing all strings in the matching mode of the current line: s/pattern/string/g # g to indicate globalReplace all matching strings in this file: % s/pattern/string/g # % indicates all rowsSuch as replacing the matching string of the specified row: n, MS/pattern/string/g

Here n and m are both numbers, representing the row number. The dot number can be used to represent the current row.

For example, delete the content from the current row to the fifth line:., 5d

When the dashboard is under the fifth line, a reverse deletion prompt is displayed.

When/pattern and: s/pattern/string are used, pattern is a regular expression used to match a string pattern.

The regular expression is similar to the wildcard (Basic command Introduction 2) described earlier, but note that the two are different.

Wildcard is mainly used to match the file name. A regular expression can be used not only to match the file name, but also to match any string. It is more common than wildcards, and most programming languages and some tools (such as vim, grep, awk, sed) have direct support for regular expressions.

The following describes the concepts and usage of the regular expression to be used:
Matching position: ^ indicates the beginning of the line $ indicates the end of the line <indicates the start of the word> indicates the end of the word matching character:. Indicates matching any single character (equivalent to the? In the wildcard ?) [...] Match any single character in brackets [^...] match any non-listed character # reference wildcard description/a matches English characters, equivalent to [a-zA-Z] or [[: alpha:]. /A matches non-English characters, which is equivalent to [^ a-zA-Z]. /D matches the number, which is equivalent to [0-9] or [[: digit:]. /D matches non-numbers, which are equivalent to [^ 0-9]. /X matches a hexadecimal number, which is equivalent to [0-9A-Fa-f] or [[: xdigit:]. /X matches non-hexadecimal numbers, which is equivalent to [^ 0-9A-Fa-f]. /W matches the word, which is equivalent to [0-9A-Za-z _]. /W matches non-words, which is equivalent to [^ 0-9A-Za-z _]. /T matches the TAB character. /S matches blank characters, which is equivalent to [/t] or [[: blank:]. /S matches non-blank characters, which is equivalent to [^/t]. /U matches uppercase letters, equivalent to [A-Z] or [[: upper:]. /U matches non-UPPERCASE letters. /N match newline/r match carriage return (...) Match and capture. Use/1/2/3... to reference the captured string. | Number of logical or matched characters: * indicates that the first character of a match is zero to any number of times, equivalent to {0 ,}. + Indicates matching the first character one to multiple times, equivalent to {1 ,}.? It indicates that the first character is matched at zero to one time, which is equivalent to {0, 1 }. # Note and wildcards? The difference between {n, m} indicates matching the previous CHARACTER n to m times.

When using a regular expression, you sometimes need to add an escape character "/" before a special character so that a special character represents its literal meaning rather than its special meaning, this is also required when using regular expressions in specific tools to avoid special characters being interpreted by the tool itself.

Vim needs to escape special characters when using the following regular expressions:/<.../>,/{n, m },/(.../),/? ,/+,/| The following uses regular expressions to illustrate mode matching and some command usage in vim.

Match the world string and place the cursor at the beginning of the third line after the matching row:/world/+ 3Add the annotator to the beginning of the third row to the eighth row //: 3, 8 s/^ //////Note the usage of the first line character ^ and the escape character '/': % g/^/sxyz/normaldd

This command is used to globally match rows starting with a blank string followed by xyz and execute the command dd in normal mode.

If more than 6 lowercase letters are matched: // a/{6 ,}For example, exchange a colon: string on both sides: s //(. */):/(. */) // 2:/1/# note how to reference the previously matched groupFor example, change all tags, tog, and tug to hat, hot, and hut % s/t/([aou]/) g/h/1 t/gFor example, match the hello or world words: // Here we only list some of the regular expressions used by vim. For more information about regular expressions, we will provide descriptions and examples in future articles.

The vim editor is very powerful. It only describes some basic usage methods. Vim can also use view mode, edit multiple texts, set keyboard ing, multi-clipboard, recording macro, and plug-ins. Vim can be used as IDE. But relatively speaking, vim's learning curve is steep, and the number of commands and methods that need to be remembered is quite different in different application scenarios. We recommend that you familiarize yourself with only a few commands each time. After a while, you can edit the text smoothly and quickly.

From: http:// OS .51cto.com/art/201611/521465.htm

Address: http://www.linuxprobe.com/linux-vim-commend.html


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.