Configuration of vim

  • /etc/vimrcThe configuration item takes effect globally and applies to all users
  • .vimrcIn the home directory, this parameter takes effect for only one user
> vim /etc/vimrc set number set autoindent set nowarpCopy the code

exitvimThe editor

Save the exit

Using the Vim editor to save and exit the editing state is a snap. Just remember to press ESC to switch to normal mode, then type colon (:), and then wq to save and exit.

> vim rumenz.txt
123
:wq
Copy the code

Directly out of

If you don’t want to save, press ESC to switch to normal mode, then type a colon (:), followed by q! Can.

> vim rumenz.txt
123
:q
Copy the code

Delete one or more lines

Deleting a line of code by Backspace is obviously too much work. This can be done by switching to normal mode (pressing ESC in edit mode) :

  • dd: Enter twiceddDeletes the current row.
  • 3dd: Deletes 3 rows starting from the current row.
  • dGDelete all rows from the current row to the last row.

Copy and paste a piece of code

You may often need to copy a line or block of code, and it’s easy to do this using Vim shortcuts:

  • Press Esc to switch to normal mode;
  • Move the cursor to the start of the line you want to copy.
  • Press V to select a whole line, and move the cursor to select multiple lines.
  • Press D to cut or y to copy the selected code;
  • Move the cursor to where you want to paste, press P to paste the code to the position behind the cursor, or press P to paste the code to the position in front of the cursor.

Undo and redo

When using Vim or other editors, you may often need to undo or redo some changes. In Vim, you can switch to normal mode, press U to undo the action and Ctrl+ R to redo it.

Code comments

Code comments

  • Press Ctrl+ V to switch to Visual mode;
  • Move the cursor (j or K) to select the beginning of the line to comment;
  • Press capital I, then enter a comment such as #;
  • Finally, press Esc.

uncomment

  • Press Ctrl+ V to switch to Visual mode;
  • Press J or k to select an annotation to remove;
  • Press D or X to remove the annotation

search

Search is a very important feature in many cases. To search for a particular word in a file, switch to normal mode, then type a slash /, followed by the word to be searched, and press Enter.

> vim rumenz.txt

/rumenz
Copy the code

Press n to display the next search result, press n to display the previous search result.

Read external files into VIm

When I started using Vim, I often opened a file, copied the content, closed the file, opened another file, and then pasted in to copy the content. In fact, Vim is very convenient to read the contents of another file. Switch to normal mode, then press :read

. With this shortcut you do not need to manually open the file to copy the contents.

> vim rumenz.txt

:read readme.md
Copy the code

Read the result of the command into vim

Switch to normal mode and type :read! Command inputs the results of command into vim.

> vim rumenz.txt :read ! pwdCopy the code

Switch to the last modified position

Want to know where you made the last change in the file? To switch to normal mode, enter g; To switch to the last modified position.

Move to the top or bottom of the file

To switch to normal mode, type GG to return to the top of the file and G to return to the bottom of the file.

Switches to the beginning or end of the current line

In normal mode, type $to jump to the end of the current line. Type 0 to jump to the beginning of the current line.

View files in hexadecimal format in VIM

In normal mode, enter %! XXD converts the current text to hexadecimal

> vim rumenz.txt 123 :%! xxdCopy the code

Restore to normal mode

> vim rumenz.txt 123 :%! xxd -rCopy the code

Case sensitive search

Add \c to the lookup mode for case insensitive lookup, \c for case sensitive lookup

> vim rumenz.txt
123
/rumenz \c
Copy the code

Will look for Rumenz, rumenz, rumenz and so on

Find and replace

grammar

:{scope} S /{target}/{replace}/{replace flag}Copy the code

For example :%s/foo/bar/g looks for foo in the global scope (%) and replaces it with bar, and all occurrences are replaced with (g).

Replace the current line

Replace all rumens in the current line with rumenz
:s/rumen/rumenz/g
Copy the code
Replace only the first occurrence of rumen on the current line with rumenz
:s/rumen/rumenz/
Copy the code

Global replacement

Replace all rumen with rumenz
:%s/rumen/rumenz/g
Copy the code
Replace only the first occurrence of rumen in each line with ruemnz
:%s/rumen/rumenz/
Copy the code

5 to 10 lines of substitution

Lines 5 to 10 replace all rumen with rumenz
: 5, 10 s/rumen/rumenz/gCopy the code
Lines 5 to 10 where the first rumen occurs are replaced with rumenz
: 5, 10 s/rumen/rumenz /Copy the code

Current line and the next three linesrumenAll replacement

:.,+3s/rumen/rumenz/g
Copy the code

Append to each line123

:%s/$/123/
Copy the code

Before each line#, add a comment

:%s/^/#/
Copy the code

Delete the beginning of each line#, delete comments

:%s/^#//
Copy the code

Original link :rumenz.com/rumenbiji/l… Wechat official account: entry station