What is the syntax for telling VI to read / write the source file with soft-tabs and the specified indent?

At some point, I saw a piece of code that says vi uses soft tabs and sets the size of the tab. If you put this snippet at the bottom of the source file, then vi will magically use these settings for this file.

What is the syntax and rules for including this fragment in the source file? Can emacs be used to use these settings?

+4
source share
5 answers

You can put this in a comment in the source file:

ex: set softtabstop=4 shiftwidth=4 tabstop=4 expandtab: 

The syntax of the comment depends on the type of source file.

For C / C ++ / Java, this will be:

 // ex: set softtabstop=4 shiftwidth=4 tabstop=4 expandtab: 

For JSP it will be:

 <%-- ex: set softtabstop=4 shiftwidth=4 tabstop=4 expandtab: --%> 

This works if it is placed at the beginning of the source file, but I'm not sure if it will be placed at the end of it.

This will not work for emacs. For emacs, there may be another way to achieve the same.

+2
source

Check out :h modeline .

Example:

 /* vim: ai set sw=4 ts=4 */ 

See :h modelines , how many lines in a Vim file will check for modeling information. By default, the first 5 lines are checked.

+3
source

As far as I know, vi did not have this capability. You are probably thinking of the Vim model function . Similar functionality exists in emacs where you can put local variables in a file.

Note that, at least in Vim, modellers had a history of vulnerabilities. This is primarily due to the fact that problematic options are specifically blacklisted, and not only allow you to set a specific subset of variables in modelines. I would suggest using a plugin like securemodelines .

+1
source

Put this in your C ++ source file:

 // vim: set ft=cpp 

The modeline function searches for the string " vim: " and then does the following. Note. This can open up potential exploits if you don't trust the files you open, so think twice before turning on this feature.

+1
source

Well, first of all, in real vi you do this in the .exrc file.

Secondly use

 set autoindent tabstop=8 shiftwidth=4 

because otherwise vi will insert tabs, which, in his opinion, are only 4 characters. The resulting text file will not look like it makes sense in any other editor.

0
source

All Articles