Disabling autocommands for help buffers - vim

I have the following lines in my .vimrc:
" UNIX fileformat
au BufRead,BufNewFile * set fileformats=unix,dos
au BufRead,BufNewFile * set fileformat=unix
These are to make any opened file using the unix file format.
It works well in almost all cases except for the help messages. If I type, let's say:
:h help
Vim first complains that:
"helphelp.txt" [readonly] 350L, 13662C Error detected while processing
BufRead Auto commands for "*": E21: Cannot make changes, 'modifiable'
is off: fileformat=unix
Obviously, I am trying to set the fileformat option on a non-modifiable buffer so this error is to be expected. However, what is the cleanest way to get rid of it without removing the feature for other files ?
Is there a way to conditionally apply/not apply autocommands for the help buffers ?
Thank you.

The 'fileformats' is a global setting, it should suffice to set it only once in your .vimrc:
set fileformats=unix,dos
The error will occur for any other unmodifiable file, not just help files. Therefore, it's best to make the setting conditional on the 'modifiable' buffer setting:
au BufRead,BufNewFile * if &l:modifiable | setlocal fileformat=unix | endif
(Alternatively, you could also just :silent! the error, but I regard the conditional as cleaner.)

Related

Vim colorscheme not loading when using autocommand BufNewFile

When trying to automatically open the corresponding .cpp or .h file using autocommand I encounter no colorscheme on the corresponding file that is opened.
I'm not too familiar with vimscript but I believe Vim is opening the file thinking it is of file type ".txt" and therefore using a default colorscheme.
Two autocommand lines in ~/.vimrc:
au BufRead,BufNewFile *.cpp exe "bel vsplit" fnameescape(expand("%:r").".h")
au BufRead,BufNewFile *.h exe "vsplit" fnameescape(expand("%:r").".cpp")
Any help would be appreciated.
Your answer is a workaround (though you should use :setlocal instead of :set to avoid that the syntax leaks out to new buffers that are opened from that one), but it doesn't attack the root cause, which you'll find explained at :help autocmd-nested:
By default, autocommands do not nest. If you use ":e" or ":w" in an autocommand, Vim does not execute the BufRead and BufWrite autocommands for those commands. If you do want this, use the "nested" flag for those commands in which you want nesting.
Syntax highlighting (you say colorscheme in your title, but that's actually just the color and font attributes that are then used by syntax highlighting) is based on :autocmd events (same goes for filetype plugins, so any C++-related settings you also wouldn't find in the split file, assuming you have :filetype plugin on in your ~/.vimrc). Without the nested attribute, the split file will be opened, but none of the usual actions will be run on them. Though nesting in general can be problematic, this is one of those cases where it is needed.
au BufRead,BufNewFile *.cpp nested exe "bel vsplit" fnameescape(expand("%:r").".h")
au BufRead,BufNewFile *.h nested exe "vsplit" fnameescape(expand("%:r").".cpp")
Unfortunately, this introduces another problem: The one autocmd will trigger the other one, and vice versa (up to a limit). You need to guard the actions so that a split is only done if the file isn't open yet. (This also improves on the usability in a general way, when you open a file with the other already open.) :help bufwinnr() checks whether the target buffer is already visible in a window:
au BufRead,BufNewFile *.cpp nested if bufwinnr("^" . expand("%:r").".h$") == -1 | exe "bel vsplit" fnameescape(expand("%:r").".h") | endif
au BufRead,BufNewFile *.h nested if bufwinnr("^" . expand("%:r").".cpp$") == -1 | exe "vsplit" fnameescape(expand("%:r").".cpp") | endif
If anyone cares to look at this in the future the solution was that Vim was loading the second file as syntax=none. So adding | set syntax=cpp at the end of each auto command fixed it.

Disable syntax highlighting for certain filenames

I have syntax highlighting enabled in .vimrc, but that makes loading certain files too long. So I need to disable (or, to be precise, not enable... enabling it and then disabling is not a solution) syntax highlighting for these files. I tried
au BufNewFile,BufRead !*.inc syntax enable
but that made just no syntax highlighting applied ever. The solution presented here does not work for me, since I can't make a distinction by filetype. I tried adapting to no avail, which might or might not be connected to the events needed for "syntax enable".
Thanks for any pointers!
The mentioned solution points to the right direction: Define an autocmd for all buffers, and then (instead of 'filetype'), match with the filename via expand('<afile>'):
au BufNewFile,BufRead * if expand('<afile>:e') !=? 'inc' | syntax enable | endif
Here, I've used your example of *.inc extensions in the condition. If you find the matching cumbersome and would rather use the autocmd syntax, you can do that with an intermediate buffer flag, too, using the fact that autocmds are executed in order of definition:
au BufNewFile,BufRead *.inc let b:isOmitSyntax = 1
au BufNewFile,BufRead * if ! exists('b:isOmitSyntax') | syntax enable | endif
If you want to show syntax only for .c files. Put
syntax off
autocmd! bufreadpost *.c syntax on
in your vimrc.
Also you can map a key for enabling syntax (Ctrl+s in this case)
nnoremap <C-S> :syntax on<CR>
In you question you want to disable syntax only for .inc file. Do it like this:
syntax on
autocmd! bufreadpost *.inc set syntax=off
To disable syntax highlighting for files with .inc extension, you can basically use:
syntax on
au BufNewFile,BufRead *.inc setlocal syntax=OFF
To disable it for multiple extensions, e.g. also for py:
au BufNewFile,BufRead *.{inc,py} setlocal syntax=OFF

Vim: How to disable syntax altogether for certain filetype

I'm trying to disable syntax for Markdown files using autocmd in Vim. I have the following in my .vimrc:
function! SyntaxOff()
echom "one"
syntax off
echom "two"
endfunction
autocmd filetype markdown exe "call SyntaxOff()"
When I open a Markdown file and check :messages, I can see the one and two, and yet the syntax is still on. If I open a dummy file and subsequently open a Markdown file, then I can see that syntax is disabled. If I then open a file of a different filetype, then syntax is still disabled. My guess is that something else in my .vimrc is re-enabling the syntax when I open Vim up for the first time, but then after the function runs syntax is permanently shut off.
So my questions are (1) How can I disable syntax for Markdown files when I open them directly (i.e. not using the maneuver described above)? (2) How can I get Vim to re-enable syntax when I then open another file of a different filetype?
You should wrap your auto commands in a check and also in a group or else you will set multiple auto commands (resource leak) and Vim will slow down after a long time of use. It's also better to use FileTypes instead of regular expressions/matchers for auto commands. However, in some cases the regular expressions make sense. In this case I think you want to use a regex to match *.md as well as markdown files because Vim doesn't detect *.md as markdown.
if has("autocmd")
augroup standard
autocmd!
autocmd BufNewFile,BufRead *.markdown,*.md setlocal setfiletype disabled
augroup END
endif
#kev's answer shows you how to disable the Markdown filetype completely by modifying the filetype detection.
If you want to keep the filetype and indent plugins, and just disable the syntax, you can either:
turn off after-the-fact
Create ~/.vim/after/syntax/markdown.vim with:
setlocal syntax=
avoid turning on
Create ~/.vim/syntax/markdown.vim with:
if exists("b:current_syntax")
finish
endif
let b:current_syntax = "markdown"

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/?
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.
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.
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'

Disabling autocommenting for all filetypes

I turned on filetype plugin for some rails vim plugins I added, but a side effect of this seems to be that now autocommenting has been enabled in all filetypes (for instance, if I start a line with #, the next line, either by Enter in insert mode or O, etc. to enter insert mode, will also get a #).
I found a guide to disabling the auto-commenting formatoptions, and added the following to my .vimrc:
au FileType * setlocal formatoptions-=cro
However, I am still running into problems -- unless I explicitly :source .vimrc, (or enter the setlocal ... directly), it is not taking effect. I determined that this is the case because vim's ftplugins are overriding my options with their own.
I then found a second guide which talks about using an after ftplugin script to make changes after the ftplugin scripts have run, however their solution is to create symlinks for every single filetype in ~/.vim/after/ftplugin to a central file, and this seems to be kludgy to me.
Is there any way to create a generic after-ftplugin script or am I approaching this problem incorrectly? Any help would be appreciated.
How about an "after" plugin? Create a file in ~/.vim/after/plugin/ called noAutoComments.vim (or whatever) and place your autocmd in that?
Edit:
The reason this works? I'm only guessing here, but I have a feeling that the autocmd in the ~/.vimrc file is getting removed by some other file (but before the "after" files are getting sourced).
I ended up removing my ~/.vim directory and replaced my ~/.vimrc with the following 3 lines:
filetype plugin on
syntax on
au FileType * setlocal formatoptions-=cro
With only these lines in my ~/.vimrc and no ~/.vim/ directory, the autocmd seems to work as expected (Vim 7.1).
For any file that I edit:
:verbose set formatoptions?
formatoptions=ql
Last set from ~/.vimrc
I have yet to determine what file (plugin) is causing this issue however.
I've done some more investigation and it seems that the location of my autocmd within my .vimrc file determines if formatoptions will be overridden by vim's ftplugins or not. Using vim --noplugin to disable all external plugins, I found the following results:
If my vimrc looks like:
au FileType * setl fo-=cro
filetype plugin indent on
The result of :verbose set fo? is:
formatoptions=croql
Last set from /usr/share/vim/vim72/ftplugin/ruby.vim
However, if the lines in my vimrc are reversed:
filetype plugin indent on
au FileType * setl fo-=cro
The result of :verbose set fo? is:
formatoptions=ql
Last set from ~/.vimrc
... which is the desired result. So it seems that the autocmd needs to be specified after filetype plugins are enabled.
Another reason this might not be taking effect...
From :he :set-=:
When the option is a list of flags, {value} must be
exactly as they appear in the option. Remove flags
one by one to avoid problems.
I have
" Turn off auto-commenting
au FileType * setlocal formatoptions-=c
au FileType * setlocal formatoptions-=r
au FileType * setlocal formatoptions-=o
because I've run into this.
Using one of the various autocmd events to set the configuration option should work if you find the right one, but I'd start by running:
:verbose set formatoptions?
This will tell you where the option was set, which may make it easier to determine which autocmd to use. Alternatively, if you don't mind a bit of minor hacking, the way I'd probably do it is just to find out where it's set in the plugin and comment out that line (and make a note of it in case you ever upgrade the plugin). You could also contact the plugin's author and ask them to make it a configurable option.
For the available autocmd events, read this:
:help {event}
I have tried solutions proposed by many, but none of them worked for me, but I found one very simple workaround, namely, in your ~/.bash_aliases:
# vim without auto comment
alias vi="vi +'set fo-=cro'"
I was struggling with this issue and I finally works with the following lines:
syntax on
filetype on
filetype plugin on
au FileType * setlocal formatoptions-=cro
I think the key here is that the autocmd is place after the filetype plugin on.

Resources