In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains the basic concepts and operations of the vi editor under linux. The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn the basic concepts and operations of the vi editor under linux.
The basic concept of vi
Basically, vi can be divided into three operating modes: command mode (Command mode), insert mode (Insert mode) and bottom line command mode (Last line mode).
The functional distinction of each mode is as follows:
1. Comand mode: control the movement of the screen cursor, the deletion of characters or cursors, move and copy a section and enter the Insert mode, or to the Last line mode.
2. Insert mode: text data input can only be done under Insert mode. Press Esc, etc., to return to Comand mode.
3. Last line mode: the file will be saved or left from the editor, or the editing environment can be set, such as finding strings, listing line numbers, and so on.
However, vi can be simplified into two modes, that is, Last line mode is included in Command mode, and vi is divided into Command and Insert mode.
Basic operation of vi
? Enter vi
After you are prompted to enter vi and the file name, you can enter the vi full-screen editing screen:
$vi testfile
One thing to pay special attention to is that after you enter vi, you are under "Command mode". You have to switch to Insert mode before you can enter text.
Users who use vi for the first time will want to use the up and down keys to move the cursor first. As a result, the computer keeps calling, making themselves angry half to death, so after entering vi, don't move.
We'll talk about it after we convert it to Insert. ? Switch to the Insert mode editing file and press the'i','a' or'o' keys under Command mode to enter Insert mode.
At this point, you can start typing text.
I: insert, insert the entered text from where the cursor is currently located.
A: add, the next word where the cursor is located begins to enter text.
O: insert a new line and enter text at the beginning of the line.
Toggle → Command mode for Insert, press Esc key
You are currently in Insert mode, so you can only type all the time. If you find a typo, you want to use the cursor key to move back and delete the word.
Press ESC to switch back to Command mode, and then delete the text.
? Leave vi and save documents
Under Command mode, type Last line mode by pressing the colon ":", for example:
W filename (enter "w filename" to save the article to the specified file name filename)
: wq (enter "wq", because the file name testfile is specified when entering, so it will write to testfile and leave vi)
: q! (enter "Q!" to force you to leave and discard the edited file)
List of Command mode function keys
After introducing the command mode instruction, the function key of the word "commonly used" is added after the instruction to indicate the more commonly used vi instruction.
Readers, you must learn and remember.
(1) I, a, o switch into Insert mode. [Super commonly used]
(2) move the cursor
Vi can directly use the cursor keys on the keyboard to move up and down, but the regular vi uses lowercase English letters
H, j, k, l, control the cursor to move left, down, up, and right, respectively.
Press Ctrl+B: move the screen back one page. [commonly used]
Press Ctrl+F: the screen moves forward one page. [commonly used]
Press Ctrl+U: move the screen back half a page.
Press Ctrl+D: the screen moves forward half a page.
Press 0 (number zero): move the beginning of the article. [commonly used]
Press G: move to the end of the article. [commonly used]
Press w: the cursor jumps to the beginning of the next word. [commonly used]
Press e: the cursor jumps to the suffix of the next word.
Press b: cursor to return to the beginning of the previous word.
Press $: to move to the end of the line where the cursor is located. [commonly used]
Press ^: to move to the first non-blank character of the line.
Press 0: move to the beginning of the line. [commonly used]
Press #: to move to the # th position of the line, for example: 51, 121 [commonly used]
(3) Delete text
X: delete one character after the cursor position each time you press. [Super common]
# x: for example, the 6x table deletes the last six characters of the cursor. [commonly used]
X: the X of a large type, each time you press to delete the first character of the cursor position.
# X: for example, the 20X table deletes the first 20 characters of the cursor.
Dd: delete the line where the cursor is located. [Super common]
# dd: for example, the 6dd table deletes text six lines down from the line where the cursor is located. [commonly used]
(4) copy
Yw: copies the character where the cursor is located to the suffix into the buffer.
(want to be opposite to the functions of # x and # X)
P: paste the characters in the buffer to the cursor position (the instructions' yw' and'p must be used together).
Yy: copy the line where the cursor is located. [Super common]
P: copy the single line to where you want to paste. (instructions' yy' 'and' p 'must be used together)
# yy: for example, 6yy indicates that the text is copied six lines down from the line where the cursor is located. [commonly used]
P: copy multiple lines to where you want to paste. (the directive'# yy' 'must be used in conjunction with' p')
"ayy: put replication rows into buffer a, and vi provides buffer function to store commonly used data in buffer.
"ap: paste the data placed in buffer a.
"b3yy: stores three lines of data in buffer b.
"b3p: paste the data that exists in buffer b
(5) replace
R: replace the character where the cursor is located: [commonly used]
R: replace the character until you press Esc.
(6) restore (undo) the previous instruction
U: if you misoperate an instruction, you can press u immediately to revert to the previous operation. [Super common]
.:. The last instruction can be executed repeatedly.
(7) change
Cw: change the word where the cursor is located to the suffix $.
Cumw: for example, C3W means to change 3 words.
(8) Jump to the specified line
Ctrl+G: lists the line number of the line where the cursor is located.
# G: for example, 15G means to move the cursor to the beginning of line 15 of the article. [commonly used]
Brief introduction of instructions under Last line mode
Before you want to use Last line mode, please remember to press the ESC key to make sure you are under Command mode, and then press the colon ":" or "/" or "?"
One of the three keys enters Last line mode.
1. List line number
Set nu: after typing "set nu", the line number is listed before each line of the article.
two。 Skip to a line of the article
#: the pound sign represents a number. Enter the number before the Last line mode prompt ":", and then press Enter to jump to the line.
For example, 15 [Enter] will skip to line 15 of the article. [commonly used]
3. Looking for a string
/ keyword: first press /, and then enter the word you are looking for. if the keyword you are looking for for the first time is not as much as possible, you can press n to find the keyword you are looking for.
? Keyword: first press?, and then enter the word you are looking for. if the keyword you are looking for for the first time is not what you want, press n to find the keyword you are looking for.
4. Replace string
1MagneAccording string replaceBash g: in last line mode, typing "1meme accountsUniverse string replaceBand g" will replace the full text string string with the replace string.
Among them, "1" means that the search interval means from the beginning to the end of the article, and g means that it is not necessary to confirm the replacement of all.
% s/string/replace/c: will also replace the full-text string string with the replace string. Unlike the above instruction,% s and 1meme's have the same function.
C means that you must reconfirm whether to replace it before you want to replace it.
1Magazine 20s string: replace the string between lines 1 and 20 with a relpace string.
5. Save a file
W: press w before the last line mode prompt symbol ":" to save the file. [Super common]
#, # w filename: if you want to extract a paragraph from the article and save it to another file, you can use this instruction # to represent the line number, for example, 30 ~ 50 w nice
Save lines 30-50 of the article you are editing as a nice file.
6. leave
Q: press Q to leave. Sometimes if you can't leave vi, you can match it with "!: forcibly leave vi, such as" Q! "
Qw: it is generally recommended to use it with w when you leave, so that you can save files when you leave.
Command to enter vi
Vi filename: open or create a new file and place the cursor at the beginning of the first line
Vi + n filename: open the file and place the cursor at the beginning of line n
Vi + filename: open the file and place the cursor at the beginning of the last line
Vi + / pattern filename: open the file and place the cursor at the first string that matches pattern
Vi-r filename: system crash occurred the last time you were editing with vi, restore filename
Vi filename....filename: open multiple files and edit them in turn
Move cursor class command
H: move the cursor one character to the left
L: move the cursor one character to the right
Space: move the cursor one character to the right
Backspace: move the cursor one character to the left
K or Ctrl+p: move the cursor up one line
J or Ctrl+n: move the cursor down one line
Enter: move the cursor down one line
W or W: move the cursor right one word to the prefix
B or B: move the cursor left one word to the prefix
E or E: move the cursor right one word to the suffix
): move the cursor to the end of the sentence
(: move the cursor to the beginning of the sentence
}: move the cursor to the beginning of the paragraph
{: move the cursor to the end of the paragraph
NG: move the cursor to the beginning of line n
Nmarker: move the cursor down n lines
Nmury: move the cursor up n lines
Cursor: move the cursor to the end of the nth line
H: move the cursor to the top line of the screen
M: move the cursor to the middle line of the screen
L: move the cursor to the last line of the screen
0: (note the number zero) move the cursor to the beginning of the current line
$: move the cursor to the end of the current line
Screen tumble command
Ctrl+u: turn half the screen to the first part of the file
Ctrl+d: flip half the screen to the end of the file
Ctrl+f: flip a screen to the end of the file
Ctrl+b; flipped through the first screen of the file.
Nz: scroll the nth line to the top of the screen, or the current line to the top of the screen if n is not specified.
Insert text class command
I: in front of the cursor
I: at the beginning of the current line
A: after the cursor
A: at the end of the current line
O: create a new line under the current line
O: open a new line above the current line
R: replace the current character
R: replace the current character and subsequent characters until you press the ESC key
S: replaces the specified number of characters with the entered text, starting at the current cursor position
S: delete the specified number of lines and replace them with the entered text
Ncw or nCW: modifies a specified number of words
NCC: modifies a specified number of rows
Delete command
Ndw or ndW: delete the word NMY at the beginning and after the cursor
Do: delete to the beginning of the line
Delete: delete to the end of the line
Ndd: delete the current line and the subsequent nmur1 line
X or X: delete a character, x after the cursor, and X before the cursor
Ctrl+u: deletes text entered in input mode
Search and replace command
/ pattern: search for pattern from the beginning of the cursor to the end of the file
Pattern: search for pattern from the beginning of the cursor to the beginning of the file
N: repeat the previous search command in the same direction
N: repeat the last search command in the opposite direction
: s/p1/p2/g: replace all p1 with p2 in the current line
: N1 Magazine, n2sqqp1, p2Unix, g: replace all p1 with p2 in lines N1 to N2.
: g/p1/s//p2/g: replace all p1 in the file with p2
Option settin
All: lists all option settings
Term: setting terminal type
Ignorance: ignore case in search
List: displays the tab stop (Ctrl+I) and the end of line flag ($)
Number: display line number
Report: displays the number of modifications made by line-oriented commands
Terse: displays a short warning message
Warn: displays NO write information if the current file is not saved when transferring to another file
Nomagic: allows special characters not preceded by "\" to be used in search mode
Nowrapscan: forbids vi to start from the other end when the search reaches both ends of the file
Mesg: allows vi to display information written to their terminals by other users using write
Last line command
: N1 co N2 n3: copy the content between lines N1 and N2 under line n3
: N1 meme N2 m n3: move the content between lines N1 and N2 to the next line n3
: N1 and N2 d: delete the content between N1 lines and N2 lines
W: save the current file
: e filename: open the file filename for editing
X: save the current file and exit
: Q: exit vi
: Q quit: do not save the file and exit vi
:! command: execute the shell command command
: N1 recording command: take the contents of lines N1 to N2 in the file as the input of command and execute them. If you do not specify N2, it means that the entire file content is input as the input of command.
: ringing command: puts the output of the command command to the current line
Register operation
Nyy: saves the contents of the current line and its next n lines to a register, where? is a letter and n is a number
"nyw: saves the current line and its next n words to a register, where? is a letter and n is a number
"nyl: saves the current line and its next n characters to a register, where? is a letter and n is a number
"? P: take out the contents of the register and place it at the cursor position. here? it can be a letter or a number.
Ndd: delete the current line and its total n lines of text, and put the deleted contents in the No. 1 delete register.
The use of VI
I. insert text
┌──┬─┐
The │ command │ describes │
├──┼─┤
│ I │ inserts text │ before the current character
├──┼─┤
│ I │ inserts text │ at the beginning of the line
├──┼─┤
│ a │ adds the text │ after the current character
├──┼─┤
│ A │ adds the text │ at the end of the line
├──┼─┤
│ o │ inserts a blank line │ after the current line
├──┼─┤
│ O │ inserts a blank line │ before the current line
├──┼─┤
│ R │ enters the text │ in rewriting
└──┴─┘
2. Move the cursor
┌─┬─┐
The │ command │ describes │
├─┼─┤
│ j or down arrow │ move down one line │
├─┼─┤
│ k or up arrow │ move up one line │
├─┼─┤
│ h or left arrow │ move left one character │
├─┼─┤
│ l or right arrow │ move one character to the right │
├─┼─┤
│ w │ move one word to the right │
├─┼─┤
│ W │ move a space-separated word │ to the right
├─┼─┤
│ b │ move one word to the left │
├─┼─┤
│ B │ moves a space-separated word │ to the left
├─┼─┤
│ 0 │ moves to the beginning of the line │
│ Ctrl-F │ turn the page forward │
├─┼─┤
│ Ctrl-B │ flips back │
├─┼─┤
│ nG │ to nth line │
├─┼─┤
│ G │ to the last line │
└─┴─┘
III. Replace the text
┌─┬─┐
The │ command │ describes │
├─┼─┤
│ $│ to end-of-line │
├─┼─┤
│ (│ to the beginning of the sentence │
├─┼─┤
│) │ to the end of the sentence │
├─┼─┤
│ {│ to the beginning of the paragraph │
├─┼─┤
│} │ to the end of the paragraph │
└─┴─┘
IV. Delete text
┌───┬─┐
The │ command │ describes │
├───┼─┤
│ r │ replaces a character │
├───┼─┤
│ c │ modify the text until Esc key │ is pressed
├───┼─┤
│ cw │ modifies the next word │
├───┼─┤
│ cnw │ modifies the next n words │
└───┴─┘
V. text editing
┌──┬──┐
│ Mingsi │ description │
├──┼──┤
│ yy │ moves a line of text to the default buffer │
├──┼──┤
│ yn │ moves the next word to the default buffer │
├──┼──┤
│ ynw │ moves the next n words to the default buffer │
├──┼──┤
│ p │ if the default buffer contains a line of text, the current │
Inserting a blank line well after the │ │ line will result in the sound │ in the default buffer
The │ │ capacity is pasted into this line; if the default buffer contains │
│ │ multiple words, paste them to the right of the cursor. │
├──┼──┤
│ P │ if the default buffer contains a line of text, the current │
Inserting a blank line well in front of the │ │ line will result in the inner │ in the default buffer
The │ │ capacity is pasted into this line; if the default buffer contains │
│ │ multiple words, paste them to the left of the cursor
│
└──┴──┘
VI. Save and exit
┌─┬─┐
The │ command │ describes │
├─┼─┤
│ zz │ saves and exits │
├─┼─┤
│: W filename │ writes to the file │
├─┼─┤
│: W │ writes to file │
├─┼─┤
│: X │ save (if the current file has been modified) and exit │
├─┼─┤
│: Q! │ does not save the file, just exit │
├─┼─┤
│: Q │ exits vi │
Common skills of VI
The VI command can be said to be the most commonly used command for editing files in the Unix/Linux world, but because of its large command set, many people are not used to using it. In fact, you only need to master the basic command, and then use it flexibly, you will find its advantages, and will gradually like to use this method. The purpose of this article is to introduce some of the most commonly used commands and advanced application skills of VI.
I. introduction of basic commands
-1. Cursor command
K, j, h, lmuri-up, down, left and right cursor movement commands. Although you can use the four cursor keys on the right side of the keyboard in Linux, it is useful to remember these four commands. These four keys are the basic position where the right hand is placed on the keyboard.
NG-- jump command. N is the number of lines, and the command immediately causes the cursor to jump to the specified line.
The number of rows and columns where the Ctrl+G-- cursor is located is reported.
W, BMY-causes the cursor to skip one word forward or backward.
-2. Editing command
I, a, r r=replace-insert character commands (i=insert, a=append, r=replace) before, after, and where the cursor is located.
Cw, dw-- commands to change (replace) / delete the word where the cursor is located (c=change, d=delete).
X, d$, dd-- deletes one character, deletes all characters from the cursor to the end of the line, and deletes the command for the entire line.
-3. Find command
A command that looks back or forward for the corresponding string from where the cursor is located-/ string,? string--.
-4. Copy copy command
-yy, pmure-commands that copy a line to the clipboard or take out the contents of the clipboard.
Second, common problems and application skills
Read the contents of / etc/passwd in a new file and take out the user name section.
-vi file
-: r / etc/passwd reads / etc/passwd at the cursor in the open file file
Remove all the parts of the user name in / etc/passwd from the beginning of the colon to the end of the line.
-you can also read the contents of the file after the specified line number, for example, use the command ": 3r / etc/passwd" to read all the contents of / etc/passwd from line 3 of the new file.
-We can also delete all blank lines and comment lines starting with # in the file using the following method.
-# cat squid.conf.default | grep-v ^ $| grep-v ^ #
After opening a file for editing, it is only known that the logged-in user does not have write permission to the file, cannot save the disk, and needs to save the changes to a temporary file.
-vi file
-you can also save some of these changes to a temporary file, for example, just save the contents between lines 20-59 as a file / tmp/1, we can type the following command.
-vi file
-: 2050 59w / tmp/1
Edit a file with VI, but delete large chunks of content.
-first use the editing command "vi file" to open the file, then move the cursor to the line you need to delete, press Ctrl+G to display the line number, and then press Ctrl+G at the end to display the line number at the end of the file.
-assuming that the line numbers obtained twice are 23 and 1045, then delete all the contents during this period, or mark it with the ma and mb commands in the start and end lines to be deleted, and then delete it using the ": a line bd" command.
-4. Add some strings at the beginning or end of each line or lines throughout the file.
-vi file
Insert "some string" at the beginning of the first line to the last line of the file.
-:% s/$/some string/g adds "some string" at the end of each line in the entire file.
-:% s/string1/string2/g replaces "string1" with "string2" throughout the file.
-replace only the words "string1" from lines 3 to 7 in the file into "string2".
-Note: where s is substitute,% for all lines and g for global.
Edit 2 files at the same time, copy the text from one file and paste it into another file.
-vi file1 file2
-yy copies the line at the cursor of file 1
-: n switch to file 2 (n=next)
-p paste the copied line at the cursor in file 2
--: n switch back to file 1
-6. Replace the path in the file.
-use the command ":% s#/usr/bin#/bin#g" to replace all paths / usr/bin in the file with / bin. You can also use the command ":% s//usr/bin//bin/g", where "" is an escape character, indicating that the subsequent "/" character is a meaningful character, not a delimiter.
Thank you for your reading, these are the contents of "basic concepts and operations of vi editor under linux". After the study of this article, I believe you have a deeper understanding of the basic concepts and operation of vi editor under linux. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 206
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.