You are not logged in.

#1 2013-08-23 15:44:26

alphaniner
Member
From: Ancapistan
Registered: 2010-07-12
Posts: 2,810

[SOLVED]vim file specific settings (ie. modelines)

I'm referring to a line in a file s/a

# vim: ft=sh ts=4 et sw=4:

I have that line at the bottom of a .bashrc file. When I open it with vim as a regular user, the settings are applied. When I open it with vim as root (logged in as root; no sudo involved) on the same machine, the settings are ignored. Any ideas as to why this is, or how to fix it?

Both reguser and root have identical .vimrc and .vim folders. If I remove both users' .vimrc, reguser still applies the settings and root still ignores them. I also tried starting vim with full path to rule out any user env. weirdness, no change.

Last edited by alphaniner (2013-08-23 18:10:45)


But whether the Constitution really be one thing, or another, this much is certain - that it has either authorized such a government as we have had, or has been powerless to prevent it. In either case, it is unfit to exist.
-Lysander Spooner

Offline

#2 2013-08-23 15:56:30

Vain
Member
Registered: 2008-10-19
Posts: 183

Re: [SOLVED]vim file specific settings (ie. modelines)

Hi,

Vim disables modelines for root per default. You need to have a

set modeline

in your vimrc to enable them for root as well.

I do recommend a different approach, though. Vim is actually pretty good at recognizing file types. You can use something like this in your vimrc:

autocmd FileType python setlocal et sta ts=4 sw=4

No need to go through the trouble of modelines.

Offline

#3 2013-08-23 17:05:50

x33a
Forum Fellow
Registered: 2009-08-15
Posts: 4,587

Re: [SOLVED]vim file specific settings (ie. modelines)

Vain wrote:

I do recommend a different approach, though. Vim is actually pretty good at recognizing file types. You can use something like this in your vimrc:

autocmd FileType python setlocal et sta ts=4 sw=4

No need to go through the trouble of modelines.

Vim is reasonably good at recognizing file types, but not always. Especially if we are not using a proper extension, or custom extensions. So modelines are useful in such cases.

For example, try renaming .bashrc to .bashrc.py and see the difference wink

Offline

#4 2013-08-23 17:14:55

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,547
Website

Re: [SOLVED]vim file specific settings (ie. modelines)

X33a, I think that's a bit over stated.  .bashrc.py fails because vim has essentially be "lied" to with the py extension.  If you rename bashrc to "nosuchfile" it works fine as vim reads the shebang.

File specific modelines are quite handy at times, but there certainly shouldn't be a need to specify "ft=sh" in a modeline of bashrc, it is already recognized as such.


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#5 2013-08-23 17:20:02

x33a
Forum Fellow
Registered: 2009-08-15
Posts: 4,587

Re: [SOLVED]vim file specific settings (ie. modelines)

Trilby wrote:

X33a, I think that's a bit over stated.  .bashrc.py fails because vim has essentially be "lied" to with the py extension.  If you rename bashrc to "nosuchfile" it works fine as vim reads the shebang.

That was a quick example tongue

But my point about custom extensions remains. Custom extensions I use tend to be categorised as "conf".

Offline

#6 2013-08-23 17:42:56

alphaniner
Member
From: Ancapistan
Registered: 2010-07-12
Posts: 2,810

Re: [SOLVED]vim file specific settings (ie. modelines)

Thanks for all the input and for pointing out it's called a modeline. Not knowing what it's called made it difficult to research.

In the case I cited, I added the modeline just as an experiment. I copied it verbatim from /usr/lib/network/network because I didn't understand it enough to be sure I could just remove ft=sh .

x33a wrote:

Vim is reasonably good at recognizing file types, but not always. ...

I remove all comments from a lot of config files. Thus I've also found that vim considers the following (my linux.preset) to have no ft:

ALL_config="/etc/mkinitcpio.conf"
ALL_kver="/boot/vmlinuz-linux"

PRESETS=('default')

default_image="/boot/caddywhompus.img"

But if I add a new line to the top with just a '#', vim suddenly recognizes it as ft=conf.

Trilby wrote:

File specific modelines are quite handy at times, but there certainly shouldn't be a need to specify "ft=sh" in a modeline of bashrc, it is already recognized as such.

Completely unrelated to my experiment with the modeline, but when I back up that .bashrc as root-bashrc it's recognized as ft=conf .

Last edited by alphaniner (2013-08-23 18:19:23)


But whether the Constitution really be one thing, or another, this much is certain - that it has either authorized such a government as we have had, or has been powerless to prevent it. In either case, it is unfit to exist.
-Lysander Spooner

Offline

Board footer

Powered by FluxBB