Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the vi command in Linux

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly shows you "how to use the vi command in Linux". The content is simple and clear. I hope it can help you solve your doubts. Let the editor lead you to study and learn how to use the vi command in Linux.

The vi editor is the standard editor for all Unix and Linux systems. It is as powerful as any of the latest text editors. Here is just a brief introduction to its usage and a few instructions. Since the vi editor is exactly the same for any version of Unix and Linux systems, you can learn more about it anywhere else that introduces vi. Vi is also the most basic text editor in Linux. After learning it, you will walk freely in the world of Linux.

The basic concept of vi

Basically, vi can be divided into three states: command mode (command mode), insert mode (Insert mode) and bottom line mode (last line mode). The functions of each mode are as follows:

1) Command line mode command mode)

Control the movement of the screen cursor, the deletion of characters, words or lines, move and copy a section and enter the Insert mode, or to the last line mode.

2) insert mode (Insert mode)

Text input can only be done under Insert mode, and press the "ESC" key to return to command line mode.

3) bottom line mode (last line mode)

To save or exit vi, you can also set the editing environment, such as finding strings, listing line numbers, etc. Wait.

However, when we use vi, we usually simplify it into two modes, that is, the last line mode mode is also included in the command line mode command mode).

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.

The above is all the contents of the article "how to use vi commands in Linux". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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: 0

*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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report