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

What are the most commonly used commands in VI

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

Share

Shulou(Shulou.com)05/31 Report--

This article will explain in detail what are the most commonly used commands about VI. Xiaobian thinks it is quite practical, so share it with you for reference. I hope you can gain something after reading this article.

1: Basic command introduction:

1)cursor command

k,j,h,l---Up, down, left, right cursor movement commands, although you can use the four cursor keys on the right side of the keyboard in Linux,

But remember, there's something else very useful about these four commands, which is the right hand position on the keyboard.

nG ---n is the number of rows, this command immediately causes the cursor to jump to the specified row.

Ctrl+G----Reports the number of rows and columns at the cursor position

w,b-----causes the cursor to skip a word forward or backward

2)editing commands

i,a,r ------insert character commands before, after and above cursor (i=insert,a=append,r=replace)

cw,dw-----Command to change (replace)/delete the word where the cursor is located (c=change,d=delete)

x,d$,dd ----Delete a character, all characters from the cursor to the end of the line, and the entire line command

3)lookup command

/string, ? string----Command to find the corresponding string backwards/forwards from the cursor

4)copy copy command

yy,p -----Copy a line to clipboard/extract the contents of clipboard command

Go to vi (enter the following command at the system prompt):

vi Enter vi without reading any files

2. FAQs and application skills

1)Read the contents of/etc/passwd in a new file and remove the username section

vi file

:r /etc/passwd Read/etc/passwd where the cursor is in the open file file

:%s/:.*// g Remove all parts of/etc/passwd after the user name from the colon to the end of the line

:3r /etc/passwd This reads the file contents after the specified line number

Another method is to delete all blank lines and comment lines starting with #in the file

#cat squid.conf.default | grep -v '^$' | grep -v '^#'

2)After opening a file for editing, it is known that the logged user has no right to write the file and cannot save it.

vi file

:w /tmp/1 Since you can't save it, you don't want to give up all the changes you've made, so save it temporarily to/tmp/1.

:20,59w /tmp/1 or just save lines 20 through 59 to the file/tmp/1

3)Edit a file with VI, but delete large sections of content

vi file

Ctrl+G Move the cursor to the line you want to delete. Press Ctrl+G to display the line number, and then press Ctrl+G at the end.

:23,1045d Assuming that the two line numbers are 23 and 1045, delete all the contents in these rooms.

It can also be marked with ma,mb at the beginning and end of the line and deleted with:'a,'bd.

4)Add strings at the beginning or end of a line to an entire file or lines

vi file

:3,$s/^/some string /insert some string before the first line to the last line of the file

:%s/$/ some string/g Add some string at the end of each line throughout the file

:%s/string1/string2/g Replace string1 with string2 throughout the file

:3,7s/string1/string2/Replace string1 to string2 only in lines 3 to 7 of the file

Note: s for substituent,% for all rows,g for global

5)Edit two files at the same time, copy clipped text in both files

vi file1 file2

yy Open two files at the same time, copy the line where the cursor is located in file 1

: nSwitch to File 2 (n=next)

pPaste the copied line at the cursor location in file 2

:N Switch back to file 1

6)Replace path in file

:%s#/usr/bin#/bin#g Replace all paths/usr/bin in the file with/bin

or use

:%s//usr/bin//bin/g single character '/' preceded by a symbol indicating that '/' is true

vi filename Enter vi and read the file with the specified name (new or old file).

vi +n filename Go to vi and start with the first few lines of the file.

vi +filename Go to vi and start with the last line of the file.

vi + /word filename Enter vi and start with the word of the file.

vi filename(s) Enter vi and list the specified files, reading the first file first.

vedit Enter vi and "INSERT MODE" will be displayed in the status line when entering the mode.

Edit several files (after entering vi using vi filename(s))

:args Show file names in the edit list

:n Read the next file in the edit list

:rew Read the first file in the edit list

:e# Read the previous file in the edit list

:e file Read another file into vi(this file may not be in the edit list). If the original file has not been archived after modification, it should be archived with: w first.

:e! file Forcing another file into vi without archiving the original file.

Save and exit vi :w filename Stored in specified file without exiting vi (or the file name of the current job if no file name is specified).

:wq or zz Save file and exit vi.

:q Leave vi without modification.

:q! Abandon any modifications and exit vi.

:! command Temporarily exit vi and execute shell commands, then return to vi.

:sh Temporarily exit vi to the system, press Ctrl + d at the end to return to vi.

add data instruction

i Start inserting characters at the mark off position and press ESC at the end.

I Start adding words at the beginning of the cursor line and press ESC at the end.

a Start adding words after the cursor position and press ESC at the end.

A Start adding words at the end of the line where the cursor is located and press ESC at the end.

o Add a blank line under the cursor and start adding words. Press ESC when finished.

O Add a blank line to the cursor and start adding words. Press ESC when finished.

! command Execute the shell command and append the result to the line next to the cursor.

Delete command nx Delete n characters starting from the cursor position (including cursor position, press an x to delete the character on which the cursor is located)

nX Delete n characters starting from the cursor position (excluding the cursor position).

ndw Delete the n character groups at the cursor position.

d0 Delete all characters from the beginning of the line to the cursor position.

d$or D Delete all characters from the beginning of the cursor position to the end of the line.

ndd Delete the n rows (entire rows) starting at the cursor position (dd means delete the row where the cursor is located).

:start,endd Delete lines start to end of the file.

mark off movement

0 Move to the beginning of a line

$ Move to the end of a line

[ Move to start of file

] Move to end of file

nh Move left n position

nl or spacebar Move n to the right

nk Move up n lines

n+ Move up n rows with cursor at the beginning of row

ni Move down n rows

n- Move down n lines with cursor at start of line

H Move to the upper left corner of the screen

M Move to the beginning of the middle line of the screen

L Move to the last line of the screen

G Move to the last line of the file

nG or:n Move to line n of the file

nw Move right n word groups, punctuation belongs to word groups

nW Move right n word groups, punctuation marks do not belong to word groups

nb Left shift n word groups, punctuation marks belong to word groups

nB Left shift n word groups, punctuation marks do not belong to word groups

Ctrl + u Half a menu on the screen

Ctrl + d Scroll down half a menu

Ctrl + b A menu rolls up on the screen

Ctrl + F Scroll down to a menu

modification instruction

r Modify cursor file characters

R Start at the cursor position and press ESC when finished

new Change n groups of characters, press ESC when finished

ncc Change n lines starting at the cursor position and press ESC when finished

rearrangement line length

i and press Enter to break the line from where the cursor is and enter insert mode

J Connect the next row of data to the next row

Find instructions/text Find the string text from the cursor position down

? text Find the string from the cursor position up text

n Continue to find the next string (used after entering the find command above)

Find and replace commands

:getxt1/s/ /text2/options Replace text1 with text2 on each line

option=g indicates that all text1 in the file are replaced. If no option is entered, only the first text1 in each line is replaced.

option=go Display rows replaced on screen

option=gc requires confirmation before each string substitution

Start,endgtext1/s/ / text2/options Same as above, looking for and replacing only lines start~end.

or:Start,ends/text1/text2/options

Copy and move files

:first,last co dest Copy data from first to last rows below destination row (dest)

:Start,end m dest Move the data from the start to end rows under the dest row.

:r filename Reads the contents of the specified file under the cursor line.

nY temporarily storing n rows of data starting from the cursor position

p Copy temporary data in the next row of cursor

P Copy temporary data one line above cursor

Other orders. Repeat previous command

u cancel previous command

Ctrl + l Refresh screen display

:set number Displays file line numbers, but does not save files

:set nonumber Undisplay Line Number

:set ai Sets the starting position of each line (starting from the current position of the cursor)

:set noai Cancel row start position setting

:f or + g Tells the user about the data of the currently edited file.

About "VI most commonly used command what" this article is shared here, I hope the above content can have some help to everyone, so that you can learn more knowledge, if you think the article is good, please share it to let more people see.

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

Database

Wechat

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

12
Report