Some obscure but very useful Vim commands

Source: Internet
Author: User

Save the file and exit

It's a shame to say it, and I've learned this order recently.

??

And the following command is equivalent:

: Wq

is to save the current file and exit.

(Translator Note: These two commands are actually not exactly equivalent when the file is modified when the two commands are the same.) But if not modified, use?? Does not change the file's modification time, and using: Wq changes the file's modification time. )

Basic Calculator

In insert mode, you can use the Ctrl+r key and enter =, and then enter a simple calculation. Press Enter and the result is inserted into the file. For example, try entering:

' =2+2 ' ENTER

The result "4" is then inserted into the file.

Find repeated consecutive words

When you type quickly, it is likely that you will enter the same word two times in a row, just like this. This kind of mistake may deceive anyone, even if you read it yourself again is inevitable. Fortunately, there is a simple regular expression that can be used to prevent this error. Use the search command (default is/) and type:

This will show all the duplicated words. To achieve the best results, don't forget to put the following command:

Set Hlsearch

Put in your. vimrc file to highlight all matches.

Abbreviation

One of the most impressive tips is that you can define abbreviations in Vim, which can replace your input in real time with something else. The syntax format is as follows:

[Abbreviation  [text to replace ] 

A common example is:

: AB ASAP as soon as possible

Replace the "ASAP" you entered with "as soon as possible".

Save files when you forget to open files in root mode

This may be a command that has been popular in the forum. Whenever you open a file (such as a system configuration file) that you do not have permission to write to and make some changes, Vim cannot be saved by the normal ": w" command.

You do not need to re-open the file and modify it again, only to run:

Tee%

This will be stored directly in root mode.

Encrypt text in real time

If you don't want people to understand what's on your screen, you can use a built-in option to encode text using ROT13 with the following command:

Ggvgg?

GG moves the cursor to the first line of the Vim buffer, V enters the visual mode, and G moves the cursor to the last line of the buffer. Therefore, GGVG causes the visual mode to overwrite this current buffer. Last g? Use ROT13 to encode the entire region.

Note that it can be mapped to one of the most commonly used keys. It can also work well with alphabet symbols. The best way to undo it is to use the Undo command: U.

Auto-complete

This is another feature that makes me feel ashamed, but I find that many people around me don't know. Vim defaults to Auto-complete function. It is true that this feature is very basic and can be enhanced by plugins, but it is also helpful. The method is simple. Vim attempts to predict the end of a word by a word that has already been entered. For example, when you enter "compiler" in the same file for the second time, just enter "com" and then remain in insert mode, press CTRL + N to see that Vim complements the word for you. It's simple, but it's also useful.

Compare the differences between two files

Most of you probably know the Vimdiff command, which can open Vim using detach mode and compare the differences of two files. The syntax is as follows:

[File 1[File 2] 

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

:d Iffthis

First open the original file in Vim. Then use the detach mode to bring up the second file:

[File 2]

Finally, in the first buffer, enter:

:d Iffthis

Use CTRL+W to toggle the buffer and enter again:

:d Iffthis

This will highlight the different parts of the two files.

(Translator Note: You can use the command directly in a buffer: Windo diffthis without input:d iffthis two times)

To stop the comparison, use:

:d Iffoff
Fallback files by time

Vim records changes to the file, and you can easily fall back to a previous time. The command is fairly straightforward. Like what:

: Earlier 1m

The file will be rolled back to a state that was 1 minutes ago.

Note that you can use the following command to convert the opposite:

: Later
Delete text inside a tag

When I started using Vim, one thing I always wanted to do was to easily remove the square brackets or parentheses. Go to the start tag, and then use the following syntax:

Di[tag ] 

For example, place the cursor on the opening parenthesis, and use the following command to delete the text inside the parentheses:

Di(

If it is square brackets or quotation marks, use:

Di{

And:

Bid
Delete the content before the specified tag

Similar to the inside of the delete tag, but with different purposes. The command is as follows:

DT[Mark ] 

All the content between the cursor and the tag is deleted (keep the tag intact) if the tag is in the same line. For example

Dt.

will be deleted to the end of the sentence, but keep '. ' Fixed.

Turn Vim into a hex editor

It's not my favorite trick, but it can be fun sometimes. You can link the Vim and XXD functions to convert the file to hexadecimal mode. The command is as follows:

:%!xxd

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

:%!xxd-r
Place the text under the cursor in the center of the screen

What we have to do is as shown in the title. If you want to force scrolling the screen to place the text under the cursor in the center of the screen, use the command in Visual mode (translator Note: in normal mode):

Zz

Jump to previous/next position

When you edit a large file, it is often necessary to make changes somewhere and then jump to another place. If you want to jump back to the place you previously modified, use the command:

Ctrl+o

To go back to the place where you changed.

Similar to:

Ctrl+i

Will fall back above the beat.

Convert the current file to a Web page

This generates an HTML file to display the text and display the source code in a separate window:

:%tohtml

(Translator Note: The original is:%tohtml, but on my computer is:%tohtml)

Very basic but very good.

Overall, this series of commands was collected after I read a lot of forum topics and Vim Tips wiki (if you want to learn more about the editor, I highly recommend this article).

If you also know which commands are useful but you think most people don't know, feel free to share them in the comments. As the introduction says, a "little-known but useful" command may be just your own, but it's always good to share.

Some obscure but very useful Vim commands

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.