introduce

Vim is a well-known powerful and highly customizable text editor similar to Vi, with many features improved and added to Vi. VIM is free software. Vim is widely touted as the best Vi editor of all, but the real competition comes from variations of Emacs. In 1999 Emacs was selected as the winner of the Linuxworld text editing category, with Vim coming in second. But in February 2000, Vim won Slashdot Beanie’s Award for best Open Source Text Editor, pushing Emacs into second place. Overall, both Vim and Emacs are excellent text editors.

use

First we need to install Vim

Brew install vim // Brew install Vim // MAC install vim apt-get install vim // Ubuntu install VimCopy the code

For Windows users, go to the search engine and search for Vim

This article uses vimtutor to study vim. After installing vim, enter vimtutor on the terminal

Vim model

Command mode: used for entering commands such as save, run, switch labels, cut screen, etc. Insert mode: also known as edit mode, used for editing text. Visible mode: equivalent to the normal mode after highlighting selected text. Normal mode: Used for viewing text, or copying, pasting, canceling, redoing, etc

Move the cursor

In normal mode, h, J, K and L can be used to replace ←, ↓, ↑ and → keys respectively to move the cursor. For beginners, this way of moving the cursor is very unaccustomed, but in the process of programming, you can move the cursor in this way without moving the right hand frequently

Note: when a behavior is empty, H and L cannot move left and right

If you are not sure about the letter you have pressed, press the key to return to Normal mode. Then type the command you want from the keyboard again.

Vim entry and exit

Yeah, I’m used to cursor manipulation, it’s so easy, but how do I quit Vim

1. Press the

key to make sure you are in normal mode.

2. Then type q and press Enter, but this will not save your changes.

3. Enter :wq to exit and save your changes.

4. Yes, you guessed right. If you want to save but not exit, type w

Delete text editing

So what do I do if I want to delete something in normal mode, it’s very simple, you just use the move cursor method we learned to move the cursor to the position of the text you want to delete, press the X key

Tip: When I’m not satisfied with an action and want to withdraw, just type U

Insert text editing

Wow, I find the text is not what I want, I need to modify it, but I can’t seem to edit it in normal mode, what should I do?

Press I to enter insert mode. In this mode, you can modify the text at will, but you can no longer use h, J, k, l to move the cursor. When you are done, press

to return to normal mode

Add text editing

Ah? What is this insert mode? I can only add content in front of the cursor. What if I want to add new content after the original content?

In normal mode, you can press a, and then enter the content you want to add, again, after typing, press

to return to normal mode.

Delete class command

The previous deletion method was so troublesome that I had to move the cursor one by one and press x again and again to delete a long list of contents. Of course not

Many commands that change text consist of an operator and an action.

The format of the delete command using the delete operator d is as follows:

​ d motion

A short list of actions:

W – From the current cursor position up to the beginning of the next word, excluding its first character. E – From the current cursor position to the end of the word, including the last character. $- From the current cursor position until the end of the current line.

Basically, if I want to delete a word I just type in dw, and if I type in d$, then I delete the whole line

Use counts to specify actions

Suppose vim opens a file with a line that says something like this

This is just a line with words you can move around in.

My cursor is at the beginning of T

  • Type in ae, my cursor position will appear inThisAt the end of the wordsType it twice and it will appear at the end of the next word.
  • typew, will appear at the beginning of the next word, multiple type witheHas the same effect.
  • type0To make my cursor appear at the beginning of the line.
  • type$Which brings my cursor to the end of the line.

Use count has been deleted more

The combination of the delete operator and the action was mentioned above. You can insert a number before the action in the combination to delete more

D number motion

For example, by typing d2w, we can delete two words from where the cursor is

Operation of the entire line

  • After deleting the entire line earlier with the d$command, we noticed that the line became an empty line, so it might be better called clear the entire line

  • If we want to delete the current line, type the dd command

  • You can also type 2dd to delete two lines from the current line

Undo class command

  • As mentioned before, if you make a mistake, you can type inuTo undo the last command executed
  • If it’s too much trouble for you to undo again and again, you can type inUTo restore the current row to its original state
  • You can also type inCtrl-r (hold CTRL, then type R)To redo the undo command

Place class commands

The previous dd command deleted an entire line, but it didn’t. It didn’t disappear. It was saved in a vim register.

  • When usingddAfter the command deletes a line, move the cursor to a line and typep, the deleted line will be pasted onto the next line

Substitution commands

When we find something is wrong, we can fix the error instead of deleting and then inserting, which is too cumbersome, we can use the replace command.

The following two lines, the first line has an error, the second line is correct, we want to fix the first line of words wrong what can we do

Whan this lime was tyoed in, someone presswd some wrojg keys!

When this line was typed in, someone pressed some wrong keys!

  • Move the cursor to the character you want to modify and typep, and then enter the character to be replaced

Change class command

In the same way we can change the entire word instead of a single character

  • typecw“, and then enter the correct content, we can change the whole word.

Use C to change more

Changing the class operator works in the same way as removing the class. The operation format is C [number] motion

The same goes for motion, such as w for word, $for end of line, and so on

  • In addition to what we said in the last videocwI can also type inc$To change everything from the current character to the end of the line

Location and file status

Enter Ctrl-G to display the current cursor position and file status information in the current edited file. To jump directly to a specified line in the file, enter capital G

Tip: be sure to read through this section before performing the following steps!!

  1. Hold down the CTRL key and press the G key. We call this key combination Ctrl-G. You will see a status line at the bottom of the page showing the name of the file being edited and the position of the cursor in the file. Remember the line number, which will be used in Step 3.

  2. Enter a capital G to jump the current cursor to the last line of the file. Gg is used to jump the cursor to the first line of the file.

  3. Enter the line number you stayed on, and then enter a capital G. This returns you to the line where you first hit Ctrl-G.

Search commands

Enter/followed by a string to find the string in the current file.

1. Enter a/character in normal mode. You’ll notice that both the character and the cursor appear at the bottom of the screen, the same as the: command.

2, then enter what you are looking for, and then press Enter, you will find the string you entered

3, to find the same string as last time, just press the n key. To find the same string in the opposite direction, type capital N.

4, if you want to reverse search strings, please use? Take the place of.

5. To return to your previous position, press Ctrl-O (hold down the CTRL key while pressing the letter O). Press repeatedly to take more steps back. Ctrl-i will jump to a new location.

Tip: If the search has reached the end of the file, the search will automatically continue from the head of the file unless the ‘wrapscan’ option is reset.

Search for matching parentheses

Enter % to find a matching string

* When your cursor is in a parenthesis, type % and your cursor will jump to the matching parenthesis. If you type again, the cursor will go back to the matching first parenthesis

This feature is useful for finding mismatched parentheses during program debugging.

Replace the command

Let’s say I have a string like this

thee best time to see thee flowers is in thee spring.

  • When I typeS /thee/the < carriage >, then the first in the linetheeI replaced it withthe
  • If you want to put all thetheeTo replace, you can type in:s/thee/the/g
  • To replace each matching string that appears between two lines, type :#,#s/old/new/g where #,# represents the first and last lines of the replacement operation. Input :%s/old/new/g replaces every matching string in the entire file. Enter :%s/old/new/gc will find every match in the entire file, and for each match it will prompt whether to replace.

Methods of executing external commands within Vim

What if I want to type an external command while programming in Vim? Do I need to create a new terminal window? Of course not

1. Press the familiar command to move the cursor to the bottom of the screen. This allows you to enter a line of commands.

2. Then enter the exclamation mark! This character allows you to execute external shell commands.

3. Take the ls command as an example. Input! Ls < Enter >. The command lists the contents of your current directory, just as you would if you typed ls at the command line prompt. If! Ls does not work, you can try :! Dir.

More information on saving files

To save the changes to the file to a file, enter :w FILENAME.

1. Enter :! Dir or:! Ls Obtains the contents of the current directory. You should know that you have to hit enter at the end.

2. Select an unused file name, such as TEST.

3. Next type :w TEST (here TEST is the file name you selected.)

4. This command saves the entire file as TEST (Vim tutorial). To verify this, type again :! Dir or:! Ls Displays the contents of your directory list.

An optional save command

To save part of the file, type V motion :w FILENAM

1. Move the cursor to a line

2. Type V and move the cursor to the next few lines. You will see that the previous text is highlighted

3. Then type: and you will see :'<,’> appear at the bottom of the screen.

4. Now enter w TEST, where TEST is an unused file name. Make sure you see :'<,’>w TEST and then press the < Enter > key.

5. Vim will then write the selected line to a file named TEST. Use:! Dir or:! Ls Verifies that the file is saved correctly. Don’t delete it this time! We’ll use that in the next lecture

Press the V key to put Vim into visual mode for selection. You can move the cursor around to make the selection area bigger or smaller. You can then use an operator to manipulate the selected text. For example, pressing the D key removes selected text.

Extract and merge files

To insert the contents of another file into the current file, enter :r FILENAME

1. Move the cursor to a new line

Tip: After step 2, you will see the text in section 3 of Module 5. Please scroll down to see the lecture again

2. Next, use the command :r TEST to extract the previously created file named TEST. The files you extract will be placed from where the cursor is

3. In order to confirm that the file has been successfully extracted, move the cursor back to the original position to notice that there are two copies of the contents of the last lecture, one is the original content, and the other is a copy from the file.

You can also read the output of external commands. For example, :r! Ls can read the output of the ls command and place it below the cursor.

Open class command

Typing o will open a new line below the cursor and enter insert mode.

  • When the cursor is in any line, the key isoTo open a new line and enter insert mode, you can write new content, when the writing is done, press<ESC>Return to normal mode.

Additional class command

Typing a will insert text after the cursor

Another version of the replacement class command

Enter an uppercase R to replace multiple characters consecutively.

Suppose you have the following two strings

Adding 123 to xxx gives you xxx. Adding 123 to 456 gives you 579.

  • You can move the cursor to the first sentencexxxAnd then typeRIf the content is similar to the second sentence, pressESCEnter normal mode and move the cursor to nextxxx, repeat the preceding operation so that the above and below strings are equal.

Copy and paste the text

Use the operator y to copy text, and p to paste text

1, first move the cursor to the beginning of the content to copy.

2. Type V to enter visual mode.

3. Move the cursor to highlight what you want to copy

4. Type Y to extract text

5. Move the cursor to the position you want to copy, type P and paste

Set options for class commands

1. To find the word ignore, type /ignore < Enter > in normal mode. To find the word repeatedly, press the n key repeatedly.

2. To set the IC option (Ignore Case), enter: :set IC

3. Now you can look for the word ignore again by typing n. Notice that Ignore and Ignore are now also found.

4, then set hlSearch and incSearch options, please enter: :set HLS is

5. Now you can type the find command again and see what happens: /ignore < Enter >

6. To disable case ignoring, enter: :set NOic

Tip: To remove the highlighting of a match, type: : nohlSearch Tip: If you want to ignore case for only one search, you can use \c: /ignore\c < Enter >

Obtaining Help Information

Vim has a detailed and comprehensive online help system. To start the help system, choose one of the following three methods:

  • Press the<HELP>Keys (if they are on the keyboard)
  • Press the<F1>Keys (if they are on the keyboard)
  • The input:help < Enter >

Read the text in the Help window to see how help works.

  • Enter Ctrl-W to jump between Windows.

  • Enter :q < Enter > to close the help window.

  • Provide a correct argument to the “:help” command and you can find help on that topic. Please test the following parameters (don’t forget to press enter) :

:help w
:help c_CTRL-D
:help insert-index
:help user-manual
Copy the code

Creating a startup script

Vim has many more features than Vi, but most of them are not enabled by default. To use more features, you need to create a vimrc file.

1. Start editing the vimrc file, depending on your operating system:

:edit ~/.vimrc // This is the command used on Unix systems :edit$VIM/_vimrc // This is the command used by mS-WindowsCopy the code

2. Then read the contents of the vimrc sample file:

:r $VIMRUNTIME/vimrc_example.vim
Copy the code

3. Run the: :write command to save the file

The next time you start Vim, the editor will have syntax highlighting. You can add as many Settings as you like to this vimrc file. For more information please enter :help vimrc-intro

Completion function

1. Enter help or press

or < help > to open the help window.

2. Enter :help CMD to find help for the CMD command.

3. Enter Ctrl-w to jump between Windows.

4, enter :q to close the help window

5. You can create a vIMrc startup script file to save your preferred Settings.

6. When you enter the: command, press Ctrl-d to view possible completion results. Press

to use a completion.

summary

I believe that learning Vim, your programming efficiency can be more on the next level, the dream is still to have, while still young.