15 humble but very powerful Vim commands

Source: Internet
Author: User
If my latest post on this topic didn't remind you, then I make it clear that I am a fan of vim. So before some of you throw me stones, I'll show you a series of "little known Vim commands." I mean, some commands that you may not have encountered before, but might be useful to you. As a second disclaimer, I don't know which commands you may know and which ones are useful to you. So these commands are actually relatively rare, but probably very useful Vim commands.

Save the file and exit
Speaking of shame, I have only recently learned this command

: x

Is equivalent to the following command:

: wq

Both save the current file and exit.

(Translator's Note: These two commands are not exactly equivalent. When the file is modified, the two commands are the same. But if not modified, using: x will not change the modification time of the file. Will change the modification time of the file.)

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

Ctrl + r ‘= 2 + 2’ ENTER
The calculation result "4" is then inserted into the file.

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

This will show all duplicate words. For best results, don't forget to put the following command:

set hlsearch
Put it in your .vimrc file to highlight all matches.

abbreviation
One of the most impressive tricks is probably that you can define abbreviations in Vim, which can replace what you type in with something else in real time. The syntax is as follows:

: ab [abbreviation] [text to replace]
A common example is:

: ab asap as soon as possible
Will replace the "asap" you entered with "as soon as possible".

File saving when you forget to open the file as root
This may be an order that has been popular in the forums. Whenever you open a file (such as a system configuration file) for which you do not have write permissions and make some changes, Vim cannot save it with the normal ": w" command.

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

: w! sudo tee%
This is saved directly as root.

Encrypted text in real time
If you don't want others to understand 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 visual mode, and G moves the cursor to the last line of the buffer. Therefore, ggVG makes visible mode overwrite this current buffer. Finally g? Uses ROT13 to encode the entire region.

Note that it can be mapped to one of the most commonly used keys. It also works well for alphabetic symbols. The best way to undo it is to use the undo command: u.

Auto-completion
This is another feature that I feel ashamed of, but I find that many people around me don't know. Vim has autocompletion by default. It's true that this feature is very basic and can be enhanced by plugins, but it is also very helpful. The method is simple. Vim attempts to predict the end of a word based on the word that has been entered. For example, when you type "compiler" for the second time in the same file, just type "com" and keep it in insert mode. Press Ctrl + n to see that Vim completes the words 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 in detached mode and compare the differences between two files. The syntax is as follows:

$ vimdiff [File 1] [File 2]
But the same result can also be obtained with the following Vim command:

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

: vsp [file 2]
Finally enter in the first buffer:

: diffthis
Ctrl + w to switch buffers and enter again:

: diffthis
This will highlight the different parts of the two files.

(Translator's Note: You can use the command: windo diffthis directly in a buffer instead of typing: diffthis twice)

To stop the comparison, use:

: diffoff
Rollback files by time
Vim records changes to the file, and you can easily roll back to a previous time. The command is quite intuitive. such as:

: earlier 1m
The file will be rolled back to the state it was 1 minute ago.

Note that you can do the reverse conversion with the following command:

: later
Delete text inside marker
When I started using Vim, one thing I always wanted to do was how to easily remove the contents of square brackets or parentheses. Go to the opening tag and use the following syntax:

di [mark]
For example, place the cursor on the opening parenthesis and use the following command to delete the text in the parentheses:

di (
If it is square brackets or quotes, use:

di {
with:

di "
Delete content before specified tag
It's similar to the delete tag inside, but with a different purpose. The command is as follows:

dt [mark]
It will delete all the content between the cursor and the mark (keep the mark still), if it is on the same line E.g

dt.
Deletes to the end of the sentence, but keeps ‘.’ Intact.

Turn Vim into a hex editor
This is not my favorite trick, but it can be fun sometimes. You can link Vim and xxd functions to convert files to hex mode. The command is as follows:

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

:%! xxd -r
Center the text under the cursor
What we have to do is shown in the title. If you want to force the screen to scroll to place the text under the cursor in the center of the screen, use commands in visual mode.

zz

Go to previous / next position
When you edit a very large file, it is often necessary to make changes somewhere and then jump to another. If you want to jump back to where you changed it before, use the command:

Ctrl + o
Come back to where you changed it before

akin:

Ctrl + i
Will back off the beat above.

Turn the current file into a web page
This generates an HTML file to display the text and the source code in a separate window:

:% TOhtml
(Translator's Note: Original text:% Tohtml, but on my computer:% TOhtml)

Very basic but nice.

In general, this series of commands was collected after I read many forum topics and the Vim Tips wiki (if you want to learn more about editors, I highly recommend this article).

If you know other commands that are useful but you don't think most people know, feel free to share them in the comments. As stated in the introduction, a "little known but useful" command may be just your own opinion, but it is always good to share it.

15 humble but very powerful 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.