Summary of VI query commands

VI is the most commonly used text editing tool under unix/linux system, and it has many commands. This article is an article I collected from the Internet before. I don’t remember the specific source. It is posted here for your own review and use, and at the same time express to the original author. grateful.

Open file with 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 the nth line
  • 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 matching pattern
  • vi -r filename: The last time the system crashed while editing with vi, restore filename
  • vi filename….filename: Open multiple files and edit them one by one

move cursor commands

  • 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或Ctrl+p: move the cursor up one line
  • j或Ctrl+n: move the cursor down one line
  • Enter: move the cursor down one line
  • w或W: Move the cursor one word to the right to the beginning of the word
  • b或B: Move the cursor one word to the left to the beginning of the word
  • e或E: move the cursor one word to the right to the end of the word
  • ): 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 the nth line
  • n+: move the cursor down n lines
  • n-: move the cursor up n lines
  • n$: 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 scrolling commands

  • Ctrl+u: Scroll to the first half screen of the file
  • Ctrl+d: Scroll half a screen to the end of the file
  • Ctrl+f: Scroll one screen to the end of the file
  • Ctrl+b: Scroll to the first page of the file
  • nz: Scroll the nth line to the top of the screen, if n is not specified, scroll the current line to the top of the screen.

Insert text command

  • i: before the cursor
  • I: at the beginning of the current line
  • a: after the cursor
  • A: at the end of the current line
  • o: open a new line below the current line
  • O: open a new line above the current line
  • r: replace the current character
  • R: Replace the current character and the characters after it until the ESC key is pressed
  • s: Starting at the current cursor position, replace the specified number of characters with the entered text
  • S: delete the specified number of lines and replace them with the entered text
  • ncw或nCW: Modify the specified number of words
  • nCC: modify the specified number of lines

delete command

  • ndw或ndW: delete n-1 words starting at and after the cursor
  • do: delete to the beginning of the line
  • d$: delete to end of line
  • ndd: delete the current line and its next n-1 lines
  • x或X: delete a character, x deletes the character after the cursor, and X deletes the character before the cursor
  • Ctrl+u: delete the text entered in the input mode

Search and replace command

  • /pattern: search pattern from the cursor start 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 last search command in the same direction
  • N: Repeat the last search command in the opposite direction
  • : s/p1/p2/g: replace all p1 in the current line with p2
  • : n1,n2s/p1/p2/g: Replace all p1 in lines n1 to n2 with p2
  • : g/p1/s//p2/g: replace all p1 in the file with p2

option settings

  • all: List all option settings
  • term: Set the terminal type
  • ignorance: ignore case in search
  • list: Show tab stops (Ctrl+I) and end-of-line signs ($)
  • number: Display line number
  • report: Displays the number of modifications made by line-oriented commands
  • terse: Display a brief warning message
  • warn: Display NO write message if the current file is not saved when moving to another file
  • nomagic: Allows special characters not preceded by "\" in search patterns
  • nowrapscan: Prevent vi from starting from the other end when the search reaches both ends of the file
  • mesg: Allow vi to display information written by other users to their terminal with write

last line command

  • : n1,n2 co n3: Copy the content between lines n1 to n2 to the next line n3
  • : n1,n2 m n3: Move the content between lines n1 to n2 to the next line n3
  • : n1,n2 d: delete the content between lines n1 to n2
  • : 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 vi without saving the file
  • : !command: execute shell command command
  • : n1,n2 w!command: Use the contents of lines n1 to n2 in the file as the input of the command and execute it. If n1 and n2 are not specified, it means that the entire file content is used as the input of the command
  • : r!command: Put the output of the command command on the current line

register manipulation

  • “?nyy: save the contents of the current line and its next n lines to a register? , where ? is a letter and n is a number
  • “?nyw: save the current line and its next n words to a register? , where ? is a letter and n is a number
  • “?nyl: save the current line and its next n characters to a register? , where ? is a letter and n is a number
  • “?p: remove register? and place it at the cursor position. here? Can be a letter or a number
  • ndd: Delete the current line and the next n lines of text, and put the deleted content in the delete register No. 1.

Guess you like

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