Common commands of vi editor

In the shell environment, the vi editor is commonly used

 

#vi file (file name)' to enter the editing environment

#vi 'vi command if no parameters are specified, a new unnamed empty file will be created

Enhanced version of vim editor, collectively referred to as vi editor

:q 'Press the q command in the last line mode to exit the vi editing environment

#vi myfile 'If the parameter file name of vi does not exist, it will open the empty file and specify the parameter as the file name just created

:e filename 'Open a new file when finished editing a file

:!e filename 'If the original file is not saved, also force open a new file without prompting

 

:r filename 'Insert the new file but read and write below the previous line

:w [filename] 'If no parameter is added, the original file will be saved, if the parameter file name is added, it will be saved as a new file

:wq 'Save the original file and exit

:q! 'Force quit without saving

:wq! 'Force quit after saving

 

 

How to move the cursor

h: direction left

j: side down

k: Direction improvement

l: direction to the right

 

^: quickly move the cursor to the first character of the line

$: quickly move the cursor to the end of the line

w: quickly move the cursor to the first character after the current word

b: quickly move the cursor to the first character of the current previous word

e: move the cursor to the current end-of-word character quickly

 

3w: The cursor will move back 3 words to locate the first character of the word

3b: The cursor will move forward 3 words to locate the first character of the word

3e: The cursor will move back 3 words to the end of the word

 

 

Full screen movement:

ctrl+f: page forward

ctrl+b: page back

ctrl+u: turn forward half a page

ctrl+d: turn back half a page

Movement of a general position within a screen:

h 'Move the cursor to the head of this screen

m ' move the cursor to the middle of this screen

l 'Move the cursor to the end of this screen

 

 

:set nu display line number

:set nonu don't show line numbers

 

G:' command the cursor to jump to the end line of the file

1G: 'Command the cursor to jump to the first line of the file

nG: the cursor jumps to the "n" line of the file

 

 

In the command mode of the vi editor, you can use the following command to enter input mode:

i: The command enters the insert state at the current cursor

a: The command enters the insert state after the current cursor

A: The command moves the cursor to the end of the current line and enters the insert state

o: The command inserts a new line below the current line, the cursor moves to the beginning of the new line, and enters the insertion state

O: The command inserts a new line above the current line, the cursor moves to the beginning of the new line, and enters the insertion state

cw: command to delete the character from the current cursor to the end of the word, and enter the insertion state

c$: The command deletes the character from the current cursor to the end of the line and enters the insertion state

c^: The command deletes the character before the current cursor to the beginning of the line, and enters the insertion state

Press ESC to exit input mode and enter command mode

 

Operation of input mode

Use the "up, down, left and right" keys to quickly position the cursor to the beginning and end of the line

Use Page Up and Page Down keys to page up and down text

Use backspace to delete characters to the left of the cursor

Use the Delete key to delete the character at the cursor position

 

 

delete operation

In the command mode of the vi editor, you can use the following command to complete the delete operation

x: command to delete a single character at the cursor

dd: command to delete the line where the cursor is located

dw: The command deletes all characters from the current character to the end of the word (including spaces)

de: command to delete all characters from the current character to the end of the word (excluding spaces)

d$: command to delete all characters from the current character to the end of the line

d^: command to delete all characters from the current character to the beginning of the line

J: The command deletes the newline at the end of the line where the cursor is located, which is equivalent to merging the contents of the current line and the next line

 

Combined application:

3x: delete 3 characters to the right of the current cursor

2dd: delete two lines of text from the current line down

3dw: delete 3 words to the right of the current cursor

2de: delete the 2 words to the right of the current cursor, and keep the space character of one word to the right of the last word deleted

5J: Merge 5 consecutive lines of text from the current line to the same line by removing the newline at the end of the line

 

 

undo operation

In the vi editor command mode, you can use the following command to complete the undo of the operation

u: command to cancel the latest operation and restore the result of the operation. You can use the u command multiple times to restore the awakened multi-step operation

U: command to cancel all operations on the current line

Ctrl+R: Command combination command to restore the operation undone by the u command

 

 

Copy operation

In the vi editor command mode, you can use the following commands to copy text

yy: The command copies the content of the current entire line to the vi buffer

yw: command to copy the content of the current cursor to the end of the word character to the vi buffer

y$: command to copy the content of the current cursor to the end of the line to the vi buffer

y^: command to copy the content of the current cursor to the beginning of the line to the vi buffer

 

Combine application

5yy: Copy the text content of the current line and the following 5 lines to the vi buffer

3yw: Copy the 3 words starting from the current cursor to the vi buffer

:m,ny 'Copy m lines to n lines of the file (in last line mode)

 

 

Temporary post operation:

p: paste copied content

np: Paste the copied content n times

 

Find and replace:

Note Find is in command mode

"/ character" 'The search command of the vi editor needs to start with "/", followed by the string to be searched, and finally press Enter to execute the command

step:

1> Enter "/word", the search command searches the file for the specified string from top to bottom

Enter the n command to find the next matching string (top to bottom)

Enter the N command to find the next matching string in reverse (bottom-up)

2>"?Character" 'It's search direction is opposite to that of "/character", it is bottom-up

step:

Input: "? character" The search command searches the file for the specified string from bottom to top

Enter the n command to find the next matching string (bottom-up)

Enter the N command to find the next matching character in reverse (top-up)

 

 

Normal replace operation: (in last line mode)

The normal replacement operation command starts with s, and the command and the string to be replaced and the string to be replaced are separated by a / sign.

:s/old/new 'replace the current line matching the first string

:s/old/new 'Replace all matching strings on the current line

:n,ms/old/new 'replace the first matching string of each line from n to m

:n,ms/old/new/g 'replace all matching strings from n to m lines

:% s/old/new/g 'Replace all matching strings in the entire text

Use the alternate confirmation function

:s/old/new/c

:s/old/new/gc

:n,m s/old/new/gc

:% s/old/new/gc

After using the confirmed replacement command, the vi editor will display the replacement confirmation interface at the bottom line of the screen

replace with st01 (y/n/a/q/l/^E/^Y) ?

Common choices:

y: replace the currently located matching string

n: Do not replace the currently located matching string

a: Replace all matching strings in the specified range

q: Exit the confirmation interface and cancel all replacement operations of the current command

 

Online help with vi editor

(1) Install other software packages of the vi editor, and put the second installation CD of RHEL4 into the CD-ROM drive of the host computer.

And use the administrator root privilege to execute the following command operation, which can install the additional package of the software of the CD vi program

#mount -t iso9660 /dev/cdrom /media/cdrom 

#cd /media/cdrom/RedHat/RPMS/

#rpm -i vim-common-6.3.035-3.i386.rpm vim-enhanced-6.3.035-3.i396.rpm

#cd

#umount /media/cdrom 

(2) Use the online help of the vi editor: (two ways)

.Select the F1 function key in the vi editor to enter the online help of vi

.Enter the last command of "help" in the vi editor to enter the online help of vi

 

Tip: The online help group of the vi editor actually opens the help text file "help.txt" of vi in ​​the vi editor in read-only mode.

This file is just the home page and index of many help files of the vi editor, and each topic content of the vi help file is saved in a separate file.

 

 

 

Multiple document operations for the editor:

(1)

#vi -o 1.txt 2.txt 'At this time, the files are opened vertically

#vi -O 1.txt 2.txt 'The files are opened horizontally at this time

Note: Press ctrl+w and then ctrl+w to switch between files

(2)

#vi [arguments] [file ...]

E.g

#cd /etc/

#vi passwd fstab inittab

1. Display multiple file information

Before doing multi-file conversion in vi editor,

The args command should be used to view the multi-file status of the vi editor. The command is as follows:

:args

2. The display results are as follows

[passwd] fstab inittab

You can see the order of the three files, of which the [passwd] file is the currently displayed file of the vi editor,

It is indicated by brackets in [ ].

3. Switch between multiple files before and after

Use the "next" and "prev" last line commands in the vi editor to switch backward and forward multiple files opened in vi

The command is as follows:

:next 'Switch backwards

:prev 'Switch forward

Note: When editing multiple files in the vi editor, before switching from one file to another

The current file should be saved and the modified content has been saved. If you need to discard the modification of the current file and

To switch files, use the following command:

:next!

:prev!

4. Locate the first and last files

The "first" and "list" commands of the vi editor can switch the current file directly to the vi editor

To open the first or last file, the command format is as follows:

:first

:list

5. Fast file switching

After the current file has been switched with the magic file switching command in the vi editor,

Use the ctrl+^z key combination command in the vi command mode to quickly switch the position before the editor is switched

The ctrl+^ key combination command is especially suitable for opening two files in the vi editor and referencing each other for text modification

environment.

 

Edit text in the line interface

1. Use vi in ​​a graphic terminal program

2. Use the gedit graphical editor

Open method:

1> Start with the command "gedit" in the graphical terminal program

2> Menu start ([Applications]-[accessories]-[text Editor])

  

 

 

                                                                 

 

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326592740&siteId=291194637