user11171
user11171

Reputation: 4061

Vim autocommand not triggering on search?

I use Vim 7.3, and have this in my .vimrc (and nothing else):

filetype plugin indent on
autocmd FileType java :setlocal sw=4 ts=4 sts=2 noet
autocmd BufNewFile,BufReadPost ~/testdir/* setlocal sw=2 ts=2 sts=2 et

I have a directory ~/testdir which only contains a subdirectory p and the file ~/testdir/p/A.java which contains this:

class A {
}

If I open A.java by saying :e ~/testdir/p/A.java then :set et? reports that expandtab is currently ON.

On the other hand, if I start a new instance of Vim and go :vim A ~/testdir/** then this search will open A.java, but now :set et? tells me expandtab is OFF.

How do I make my intended indentation settings always apply for files under ~/testdir/?

Upvotes: 0

Views: 712

Answers (3)

FDinoff
FDinoff

Reputation: 31419

The autocmd does work but probably not the way you expect since you did not set softtabstop.

autocmd BufNewFile,BufReadPost ~/git/repo/* setlocal sw=2 ts=2 sts=2 et

If you do not set softtabstop it will keep its old value. If this value is not 0 (which seems to be the case) when you press tab you will get the number of softtabstop spaces inserted into the file. And it will look like its not working.

It is generally a good idea to set shiftwidth, tabstop, and softtabstop to the same value if you ever change it.

Its also probably a good idea to make the set command local to the buffer by using setlocal.

Take a look at :h softtabstop


You can see that the autocmd "works" when softtabstop is set to its default value (of 0) if you run the command like this. And then run your :vim command

vim -u NONE -N -c'autocmd BufNewFile,BufReadPost $HOME/git/repo/* set sw=2 ts=2 et'

Upvotes: 0

glts
glts

Reputation: 22684

Vim explicitly recommends using ~ for the user home.

The help at :h autocmd-patterns says that you can use environment variables in the event patterns ...

... [a]nd ~ can be used for the home directory (if $HOME is defined):

:autocmd BufWritePost ~/.vimrc   so ~/.vimrc
:autocmd BufRead ~archive/*      set readonly

The environment variable is expanded when the autocommand is defined, not when the autocommand is executed. This is different from the command!

So, change $HOME to ~ in your autocommand.

Even if your problem turns out to be unrelated it's good to follow the practice recommended in the docs.

Upvotes: 1

elmart
elmart

Reputation: 2374

The event BufReadPost should be being fired. Check that writing a message. This is:

autocmd BufNewFile,BufReadPost $HOME/git/repo/* echom "Reading buffer " . expand("<afile>")

Look for the messages with:

:messages

If the messages are there, then you know you are setting those options for the buffer. So, if the options have changed when the buffer is displayed, perhaps a plugin is setting them to some other thing. Check who is the culprit by showing who last changed the option with

:verbose set sw?

If that doesn't lead you to a solution, try using BufWinEnter instead of BufReadPost. That would probably work.

Upvotes: 1

Related Questions