Linux notes: using vim

Vim is a simple yet powerful text editor in Linux that can be used to create, edit and view a text. This article is just a brief introduction. More usages have to be practiced by individuals.


Vim is usually divided into three modes:
Command mode: When Vim is opened, the command mode is entered by default. At this time, all keyboard strokes will be recognized as commands rather than character input, and the editor is in a state of waiting for the user to enter commands.
Input mode: Insert mode, usually use this mode to edit text, when pressing the Esc key, it will automatically exit the input mode and enter the command mode.
Bottom line command mode: Enter a colon in the command mode: you can enter the bottom line command mode. After entering the command, press Enter to execute the corresponding command and exit the bottom line command mode. When you press the Esc key, you will also automatically exit the bottom line command mode. And enter the command mode.

 

The commonly used commands in the command mode are:

  • a / A: a means to insert after the character where the cursor is, A means to insert at the end of the line where the cursor is, then enter the input mode.
  • i / I: i means to insert before the character where the cursor is, I means to insert at the beginning of the line where the cursor is, then enter the input mode.
  • o / O: o means to insert a new line under the line where the cursor is located, O means to insert a new line above the line where the cursor is located, then enter the input mode.
  • gg: Go to the first line.
  • G: Go to the last line.
  • [n] G: means to locate to the nth line, if you press the number 88 first (it will not show your key on the screen), then press G will jump to the 88th line, the effect is the same as the bottom line command mode The command ": 88".
  • $: The cursor moves to the end of the line.
  • 0: The cursor moves to the beginning of the line.
  • x: Delete the character under the cursor.
  • [n] x: Delete n characters after the cursor.
  • dd: Delete (cut) the line where the cursor is.
  • [n] dd: Delete (cut) n lines after and under the cursor line.
  • dG: Delete everything from the line where the cursor is to the end of the file.
  • D: Delete the cursor to the end of the file.
  • yy: copy the current line.
  • [n] yy: Copy the current line and the following n lines.
  • p / P: Paste below or above the line where the cursor is.
  • r: Replace the character at the cursor.
  • R: Enter the replacement state, start replacing characters from the cursor position, and press Esc to end.
  • u: Cancel the previous operation.
  • / [string]: Search for the specified string, then press Enter, press n to view the next search result. But the default is case-sensitive. If you want to be case-insensitive, you need to execute a command ": set ic" in the underline command mode. On the contrary, if you want to distinguish case-sensitive, execute ": set noic".
  • ZZ: Shortcut key, save changes and exit.

 

The commonly used commands in the underline command mode are (the colon is omitted :):

  • set nu: Set the line number.
  • set nonu: cancel the line number.
  • [n]: locate to the nth line, such as ": 50" means to locate to the 50th line.
  • [n1], [n2] d: Delete all the contents of lines n1 to n2.
  • set [ic / noic]: Case-insensitive and case-sensitive.
  • % s / [old] / [new] / [g / c]: replace the old character string with the specified new character string in the full text, g means no query during execution, and c means query during execution.
  • [n1], [n2] s / [old] / [new] / [g / c]: replace the old string with the specified new string between the specified range n1 and n2 lines, g means do not ask during execution , C means query during execution.
  • w: Save changes.
  • w newfilename: Save as the specified file.
  • wq: Save changes and exit.
  • q !: Exit without saving changes.
  • wq !: Save the changes and exit (available for the file owner and root). When the file with read-only permissions is modified, only: wq cannot be used. At this time, you can use: wq!

 

Vim more tips

The first colon indicates the underline command mode:

  • : r [filename]: Import the contents of other files into this file from where the cursor is.
  • :! [Command]: Execute the command without exiting Vim.
  • : r! [Command]: Import the execution result of a command into this file from the cursor.
  • : [n1], [n2] s / ^ / # / g: continuous multi-line comments (that is, replace the line capital of all lines with #, ^ indicates the beginning of the line, and other language comments can be replaced by the same reason).
  • : [n1], [n2] s / ^ # // g: cancel the comment at the beginning of multiple lines (that is, replace the # at the beginning of all lines with a null character, ^ means the beginning of the line, the comments in other languages ​​are similarly replaced can).

 

Guess you like

Origin www.cnblogs.com/guyuyun/p/12695295.html