Vim is not common but very practical command skills

  
        Command
Save the file and exit
:x

is equivalent to the following command:

:wq

Saves the current file and exits.

Difference: These two commands are not actually equivalent, and the two commands are the same when the file is modified.

But if it is not modified, using :x will not change the modification time of the file, and using :wq will change the modification time of the file.
Basic Calculator

In insert mode, you can use Ctrl+r and then enter =, then enter a simple formula. Press Enter and the result will be inserted into the file. For example, try typing:

Ctrl+r '=3+6' ENTER

Then the result of the calculation “9” will be inserted into the file.
Finding Duplicate Continuous Words

When you type quickly, it is very likely that you will enter the same word twice in succession, just like this this. This kind of mistake may fool anyone, even if you re-read it yourself. Fortunately, there is a simple regular expression that can be used to prevent this error. Use the search command (by default /) and enter:

\\(\\<\\w\\+\\>\\)\\_s*\\1

This will display all duplicate words. To get the best results, don't forget to put the following command:

set hlsearch

in your .vimrc file to highlight all matches.
Abbreviations

The syntax is as follows:

:ab [abbreviation] [text to be replaced]

A general example is:

:ab asap as Soon as possible

will replace the “asap” you entered with “as soon as possible”.


Save files when you forget to open files in root

Whenever you open a file (such as a system configuration file) that you don't have write access to, and make some changes Vim cannot be saved by the normal “:w” command.

You don't need to re-open the file as root and modify it. Just run:

:w !sudo tee %

This will be saved directly as root.

To ensure that the user is in
AutoComplete

Not to mention the autocompletion provided by the plugin, just say that Vim has autocomplete by default. It's true that this feature is very basic and can be enhanced with plugins, but it is also very helpful.

Vim tries to predict the end of a word from words that have already been entered. For example, when you enter “xiaogongjiang” for the second time in the same file, just enter “x” or more, then keep in insert mode, press Ctrl+n to see Vim fill in the words for you. . It's simple, but it's also very useful.
Comparing the differences between two files

The vimdiff command, which can open Vim in split mode and compare the differences between the two files. The syntax is as follows:

$ vimdiff [file1][file2]

But the same result can be obtained with the following Vim command:

:diffthis

First at Vim Open the original file. Then use the split mode to bring the second file:

:vsp [file2]

Finally type in the first buffer:

:diffthis

by Ctrl+ w to switch the buffer and type again:

:diffthis

The different parts of the two files will be highlighted.

You can use the command directly in a buffer: windo diffthis instead of typing: diffthis twice)
Backing up files by time

Vim will record changes to the file, you are very It's easy to fall back to a certain time ago. This command is quite intuitive. For example:

:earlier 1m

will roll the file back to the state 1 minute ago.

Note that you can use the following command to do the opposite conversion:

:later delete the text inside the tag

When I started using Vim, I always What you want to do very easily is how to easily remove the contents of square brackets or parentheses.

Go to the start tag in normal mode,

Then use the following syntax:

di[tag]

For example, put the cursor at the beginning of the parenthesis On, use the following command to remove the text inside the parentheses:

di(

If it is a square bracket or a quotation mark, use:

di{

and :< Br>

di"


Deleting the content before the specified tag

There are some similarities inside the delete tag, but the purpose is different. The command is as follows:

dt[tag]

will delete all the content between the cursor and the mark (keep the mark does not move), if there is this mark on the same line. For example,

dt.

will be deleted to the end of the sentence, but keep &lsquo ;.’ does not move.
Convert Vim to hex editor

You can connect Vim and xxd functions to convert files to hex mode. The command is as follows: Br>

:%!xxd

Similarly, you can restore the original state by the following command:

:%!xxd -r Skip to the previous/next position

Things you often do when editing a large file Modify it somewhere, then jump to another place. If you want to jump back to where you modified it, use the command:

Ctrl+o

to go back to where you previously modified

Similar:

Ctrl+i

will roll back the above.
Convert the current file to a web page

This will generate an HTML file to display the text and Separate window display source code:

:%TOhtml
Copyright © Windows knowledge All Rights Reserved