Vim usage tips-interesting and efficient tips

1. Tips for using vim

Previous articleSuper detailed detailed common operations and usage tips of Vim editor
https://blog.csdn.net/weixin_46818279/article/details/105443165

1. Import the contents of a file into the current file

  • The command is : r file name
  • For a chestnut: type in the file : r / root / wechat to import the contents of the wechat file into the current file. Screenshot below:
    Insert picture description here

2. The execution result of the import command

  • The command is : r! command
  • Give a chestnut:Enter in the file : r! Date date The execution result of the command is directly imported into the vim file of the current file, and the line where the cursor of the current file is pressed is taken as the import location.
  • how to use:When you are finishing a program, you want to leave your own time to complete. This is the command you can use to insert the time directly in the text. Screenshot below:
    Insert picture description here

3. Execute the corresponding operation command without exiting vim.

  • The command is :! which command
  • Give a chestnut:Enter in the current vim file :! Which ls can view the path where the ls command is located without exiting the vim file.
  • How to use: When you are writing a script, you may want to quote a command, but forget the path where the command is stored, then you can use the : which command to view it. Screenshot below:
    Insert picture description here

4. Define shortcut keys

  • The command is : map shortcut trigger command
  • Give a chestnut:Enter the command : map ^ PI # <ESC> "^ p" is formed by crtl + v + p. Meaning and function of shortcut key: The function of defining shortcut key ctrl + p is to enter the insert command, reach the beginning of the line where it is, and enter the "#" key. Screenshot below:
    Insert picture description here
  • Give another chestnut:Input command : "^ B" in the map ^ B 0x command is formed by crtl + v + b. Meaning and function of shortcut keys: The function of defining shortcut keys ctrl + b is to move to the beginning of the line, and then delete a character. Screenshot below:
    Insert picture description here
  • Another chestnut:
    Input command : "^ H" in the map ^ H iwww.baidu.com <ESC> command is formed by ctrl + v + h. Meaning and function of shortcuts, define shortcut ctrl + h, insert the website www.baidu.com in the text, screenshots are as follows:
    Insert picture description here
    Remarks: 1. The use of shortcut keys can save many operations in the middle, greatly improving work efficiency.
    2. The trigger command is composed of the vim command. Friends who are not familiar with it can check it in the previous article: https://blog.csdn.net/weixin_46818279/article/details/105443165

5. Continuous line comments

  • (1) The command is : n1, n2 / ^ / # / g // Add comments continuously
  • Give a chestnut: : 2,6s / ^ / # / g Meaning and function: "^" means the beginning of the line, add a character at the beginning of the line. Add a "#" at the beginning of each line between the second and sixth lines. The screenshot of the experiment is as follows:
    Insert picture description here
  • (2) The command is : n1, n2 / ^ # // g // Cancel the comment at the beginning of continuous lines
  • Give a chestnut:Our comments are sometimes at the beginning of the line, sometimes in the middle. If we use : n1, n2 / # // g , it will not work . If we comment in the middle, we will also replace the middle with an empty one. Use: 3,6s / ^ # // g to replace # at the beginning of lines 3-6 with empty.
    Insert picture description here
  • (3) The command is : n1, n2s / ^ / \ / \ // g continuously add // comments
  • Let's take a chestnut: If you write not a system shell script or a Linux configuration file but another language, then we may use "//" as a comment. Enter the command : 3,7s / ^ //// g The execution result is shown in the following figure:
    Insert picture description here
    Remarks: 1. Add // at the beginning of the line to enter the command : 3,7s / ^ //// g is obviously wrong, because the "//" in the middle will not be able to distinguish which is the separator and which is the content to be inserted , So you need to add the escape character "" before "/" to make "\" a simple string.
    2. If batch continuous line comments, using this method will greatly improve efficiency.
    3. Speaking ofEscape characterFor example, in the ls command, the result of the command will be colored again, but the actual ls has no color. \ ls is the real ls, there is no color. The screenshot is as follows:
    Insert picture description here

6. Replacement tips

  • The command is : ab Replace the input content **
  • Take a chestnut: enter the command : ab puppy little monster means that you enter small in the vim editor, the system will automatically replace you with a little monster. We usually have this function in the word we often use. The specific experiment screenshots are as follows:
    Insert picture description here
    Remarks: 1. Required when we enter "Puppy" **Press enterorPress space** Only the corresponding replacement can take place.
    2. The benefits of replacement: In the fourth part we talked about defining shortcut keys, this custom shortcut key is also in word. Just to tell everyone that the shortcut keys for defining shortcut keys in word are:CTRL + ALT + "+"Come back quickly, and ran into the word, haha.
    3. That is, there are a lot of shortcut keys defined in vim, and you will find a problem. Some define shortcut keys and you ca n’t remember them. Then you can use the ab command to put some frequently used and longer things like School name, company name, email address, etc. Longer information will greatly simplify our operations and improve our work efficiency.
Published 2 original articles · praised 3 · visits 62

Guess you like

Origin blog.csdn.net/weixin_46818279/article/details/105497871