VIM's Modeline and Autocmd

Source: Internet
Author: User

Modeline

Vim's modeline allows you to set file-level settings for each file, which overrides the settings in the current user's VIMRC. When Vim opens a file containing a vim modeline comment line, it automatically reads the parameter configuration for this line and adjusts its settings to this configuration. Vim is turned off by default modeline, you need to add a line in the. vimrc file under your home:

Set Modeline

In the first line of the file/last line (must), write a line of the current file in the language supported by the comments, such as writing the shell is #, write Python is #, write PHP on//or/**/, and then add modeline recognition in the fixed format, for example:

# vim:set Expandtab ts=4 sts=4 sw=4:

In the comments above, ' # ' After, "vim:" The preceding spaces are required, the end of ': ' is also necessary, these are recognized by Modeline. The middle is the familiar VIMRC in the set up. In different languages, the syntax for annotations is not the same, and all may be written differently in other languages.

In the text of the home, you can use the document standard annotated form to the vim under instruction. For example, a restructuredtext format text file, you can add the following modeline, let vim read it automatically interpret the file as RST (to obtain the correct syntax highlighting):

.. Vim:set Ft=rst:

Modeline can be placed in an unlimited order, so I. py files have this line at the end of the file Modeline

# vim:set AI et nu sw=4 ts=4 tw=79:
Autocmd

There's nothing wrong with a file that you can read, but if someone else is going to see it, the other person uses vim, and the modeline we set up may be a bit disturbing. However, can vim such a powerful software not be set to determine the unified setting according to the file type? Of course, VIM can execute instructions for the specified condition, when a specified event occurs in the software. This function is called Autocmd. Let's say I want to execute the above modeline on each of the. py files, and you can set the following Autocmd (written in your. VIMRC or Windows vimrc)

Au bufread *.py set ai et nu sw=4 ts=4 tw=79

If we write the program to show others, want to leave someone else's vim a little set of space, you can use Autocmd instead of Modeline

VIM's Modeline and Autocmd

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.