How to find out, in which plugin a Vim setting gets changed? - vim

Today I had some obscure behavior. I have
set autoindent
in my vimrc, but for some reason, this gets turned off, whenever i open a PHP file.
I've fixed it now by adding a line like
autocmd FileType php set autoindent
But I'm still trying to figure out, where this setting is disabled. So is there some way to find out, where in the vim config a setting gets changed?
For reference here's my full vimrc.local that I'm using on Ubuntu:
https://gist.github.com/mikehaertl/1612035/5fa149468006577d193858bbc8cefcd3a413e017
EDIT:
The problem was caused by a filetype indent on that I added some time ago to my config. No idea, why that affects autoindent, though.

The :verbose command will tell you where an option was last changed:
:verbose set autoindent?
If that alone doesn't help, you can inspect all executed commands, preferably with the output redirected into a logfile:
:set verbosefile=vim.log
:20verbose edit foo.php
Also note that there are several options controlling indentation, e.g. 'cindent', 'smartindent', 'indentexpr', etc.
PS: To avoid that the changed option value spills into other buffers, it's recommended to use :setlocal instead.

Isnt that line the problem?
autocmd FileType php set cindent|set cinkeys-=0#

Related

how to prevent textwidth from reseting on txt files

Whenever I open txt files (and some others) I get my textwidth set to 80. I think this is coming from syntax or ftplugin. I'd like to fix this in my _vimrc so I don't have to call "set tw=0" every time I open a file that has this setting.
My guess is that you are getting the defaults, not a setting from an ftplugin. Check
:verbose set tw? ft?
to confirm. See the examples under
:help autocmd-patterns
for one way to set your own defaults for *.txt files.
On second thought, the default for 'tw' is zero, so you are not getting defaults. Perhaps some ftplugin used :set instead of :setlocal and you are getting the global value of the option. I think the rest of what I wrote is still on target.
The problem was in vimrc_example.vim the line
autocmd FileType text setlocal textwidth=78
sets the textwidth on txt files. Also my formatoptions get reset and no longer have l or lv (verbose doesn't give any detail who did it)
/etc/vimrc can also contain a set tw command - it certainly did in my case, thereby forcing all my files to a text width of 78 until I changed it manually.

Vim filetype detection does not trigger syntax highlighting

I'm trying to detect Go files in vim. I've set this up normally on other computers but this one is stuck.
:set filetype?
filetype=go
So it knows that it's a go file, but isn't triggering the syntax highlighting.
However,
:set filetype=go
triggers it correctly.
:filetype detect
doesn't work, and reopening the file
:e!
turns syntax highlighting off, even though filetype remains set to "go".
I have
set rtp+=/usr/local/go/misc/vim
filetype plugin indent on
syntax on
in my .vimrc (as the instructions say).
What's going on? I suspect there's some other configuration that's undoing the syntax highlighting, but lack the knowledge of where to find it.
I'm not sure exactly what the problem is… However, you can try :scriptnames to see what files were loaded by Vim.
One step further, you can set verbose=9 in your .vimrc and restart Vim. Careful, this is really verbose, try to tweak that number down.

Quickest Way to Revert Spaces to TABs in VIM

I have always been one to replace TABs in VIM with x amount of spaces (usually 4).
Four lines I almost always use in my .vimrc config file are:
set tabstop=4
set shiftwidth=4
set expandtab
syntax on
Basically, there are moments when I NEED to use a single TAB (such as Makefiles), and I don't know how else to work around that other than leaving the file, editing my .vimrc, and then reloading the file of interest.
That said, what is the quickest way (from within VIM) to revert it back to using TABS, and then reverting back to my original settings? I'm looking for the least-hassle, least-keystrokes solution.
VIM will automatically enable the TAB for a makefile, assuming you name it "makefile," as opposed to "Makefile." Not sure why VIM still doesn't detect the type with a lower-uppercase difference, but such is life. (#Sedrik)
That aside, other alternative solutions are:
Filetype Binding (#ThorstenS #tungd):
autocmd FileType make setlocal noexpandtab
RealTime Switch (#ThorstenS):
Assuming the .vimrc configuration mentioned in the question, do:
:set noet (to switch from spaces to TAB)
and :set et (to switch back)
Just use the magical escape key available in insert mode.
On the *NIX's it is ^V by default when you are in insert mode. On Windows, you need to find out what the magical escape character is - ^V is taken for something else; I think it may be ^Q or ^S?
So! In your makefile:
this: this.c
<C-V><Tab>cc this.c
where the usual meanings apply:
means hit ctrl-V (you should see a ^ hiding away under the cursor)
- hit the tab key. Bingo.
Works for me.
Note: if you use vim settings or startup code which smashes tabs as you read a file, this obviously is a short-term fix. I prefer to learn how to use the retab command to ensure a file is tab-clean, because I don't like a file to be touched unless I consciously choose to do so.
Just type set noexpandtab . Perhaps you bind this to a function key.
Only this configuration helped me solve this problem.
filetype plugin indent on
filetype detect
autocmd FileType make set noexpandtab
Vim defaults to tabstop=8 and noexpandtab, so the defaults are well suited to working with Makefiles. If your .vimrc uses custom options for tabstop, expandtab, etc., one simple solution is to bypass your .vimrc while working with Makefiles.
From the manpage (emphasis mine):
-u {vimrc} Use the commands in the file {vimrc} for initializations. All the other initializations are skipped. Use this to edit a special kind of files. It can also be used to skip all initializations by giving the name "NONE". See ":help initialization" within vim for more details.
Since I can never remember the necessary flag/value/formatting, I've created a Bash alias which remembers for me: alias vimnone='vim -u NONE'
You can create a custom configuration per file type.
mkdir -p ~/.vim/after/indent
echo 'set noexpandtab' >> ~/.vim/after/indent/make.vim
Basically, here we created a new indent configuration for makefiles that will be loaded after other scripts.
source

How to impose hard wrap against my soft wrap wishes?

Well, I'm a newb at Vim, so I'm guessing there's a 99% chance it's a user error!
Vim was soft wrapping long lines very nicely thank you, then a couple of days ago it started to insert hard wraps but only when I had saved the file.
I have been through wrap, nolinebreak, textwidth, nolist, and all combinations thereof to try to get softwrap back but to no avail. Heck, I even read the help pages. All of 'em.
Here's the relevant bits from my .vimrc (as you can tell, I'm getting desperate):
" Editing
set aw ai
set et ts=8 sts=2 sw=2 nu
set fo+=tcrqw fo-=o
set showmatch matchtime=5
set whichwrap=<,>,h,l,[,]
set cursorline
set nofoldenable
set wrap
set linebreak
let mapleader = ","
I picked up this .vimrc from using Vundle.
I found the culprit, Tim Pope's Vim Markdown plugin. Lovely plugin but personally prefer soft wraps, will have to find how to change it!
but only when I had saved the file.
This should hint to you that some plugin is touching the buffer Pre-Write.
Find out which it is by doing
:au BufWrite,BufWritePre,BufWriteCmd
:au FileWriteCmd,FileWritePre
To see where the trigger was installed from:
:verbose au BufWrite,BufWritePre,BufWriteCmd
:verbose au FileWriteCmd,FileWritePre
I have a suspicion this is probably caused by your fo line. Having "t" in the formatoptions option means that if a textwidth is set for the current buffer then vim will break lines at this width. You may notice that this only happens for certain filetypes because different ftplugins may be setting textwidth without you knowing.
The next time you see this happening, I'd suggest running :verbose set textwidth? (with the question mark) and seeing if the value is set. This command will also point you to where it was last set.
Another test would be to just remove "t" from your fo line and see if the problem goes away.
Janus is another Vim plugin that fiddles with linewrap/linebreak and textwidth.
:verbose set tw?
told me:
textwidth=72
Last set from ~/.vim/janus/vim/core/before/plugin/filetypes.vim
Now I just need to figure out the right incantation to disable that... for now, I just added set textwidth=99 to my ~/.vimrc.after file, but there may be a better way...

Disable all auto indentation in vim

In TeX vim usually screws up my indentation. Mainly when I'm in a displayed equation which I think should look like this:
\[
x=\frac{y}{z}
\]
where the whitespace infront of the x is one tab.
When I start type the equation I type the \[ and \] marks first and then go back between them, typing the tab and then the rest of the equation.
Vim doesn't do anything wrong until I have to use something that incorporates curly braces (\frac{} for example). When I type the closing } vim automatically shifts the indentation for the whole line to the left, which undoes my typed tab.
This is very anoying, how do I disable it?
my .vimrc contains:
"indentation
set smartindent
set autoindent
set tabstop=5
set shiftwidth=5
filetype indent on
I just spent a few hours working through indentation pains with javascript, and the conclusion I came to is don't remove filetype indent on from your vimrc!
This setting provides the best smart indentation for multiple file types. If you're getting bad results with this, there's likely a configuration issue at hand.
File Specific Indent Settings
So if you're like me, you probably had filetype indent on in your vimrc and had no idea what it was doing.
All this setting does is tell vim to look for files with filetype-specific indent rules. There are a few places it looks, but there are probably only two that you'd be interested in.
$VIMRUNTIME/indent/
~/.vimrc/after/indent/
The first place holds the default indent rules that come with vim. If you were to set filetype indent on on a fresh vim installation, this is where all the smart indenting would come from. For example, when you open a file called index.html in would get the rules from $VIMRUNTIME/indent/html.vim.
In my experience, these default rules are pretty darn good, but they can get messed up by other settings.
The second place (the after directory) allows you to add settings that will supercede those in the first place. This is nice because you don't have to edit the default files in order to customize them.
Flavors of Indentation
There are a few different indentation options as you've seen, and they don't all play nice together. From the Vim wiki:
autoindent
'autoindent' does nothing more than copy the indentation from the previous line, when starting a new line. It can be useful for structured text files, or when you want to control most of the indentation manually, without Vim interfering. 'autoindent' does not interfere with other indentation settings, and some file type based indentation scripts even enable it automatically.
I use filetype indent on and set autoindent in my vimrc, since they work well together. I don't have the others set.
smartindent & cindent
'smartindent' automatically inserts one extra level of indentation in some cases, and works for C-like files. 'cindent' is more customizable, but also more strict when it comes to syntax.
'smartindent' and 'cindent' might interfere with file type based indentation, and should never be used in conjunction with it.
When it comes to C and C++, file type based indentations automatically sets 'cindent', and for that reason, there is no need to set 'cindent' manually for such files. In these cases, the 'cinwords', 'cinkeys' and 'cinoptions' options still apply.
Generally, 'smartindent' or 'cindent' should only be set manually if you're not satisfied with how file type based indentation works.
indentexpr
Runs filetype indent scripts found in (vimfolder)\indent\\(indentscripts). It is mentioned in the vim documentation for filetype, alongside the others just mentioned (also, it was the cause of the problem I was having):
Reset 'autoindent', 'cindent', 'smartindent' and/or 'indentexpr' to disable indenting in an opened file.
Troubleshooting
There's a chance that some rogue plugin is changing your indent settings and that's why you're getting poor results. Luckily verbose will tell you which file was the last to change the option in question.
:verbose set autoindent?
:verbose set cindent?
:verbose set smartindent?
:verbose set indentexpr?
You may get a result such as
indentexpr=SomeMessedUpValue
Last set from ~/.vim/bundle/some_plugin/indent/plaintex.vim
If that happens, you can move that file, close and open vim, and see if it fixes your problem.
Turning Off Indent Settings for TeX
Maybe the defaults just aren't doing it for you, and you want to disable the indent settings for TeX, but leave all other file types alone. You can easily do so by setting these values to their defaults in a file in the after directory.
I don't know much about Tex or LaTex, but when I created a file with the .tex extension and ran :filetype it had the filetype as plaintex. Assuming that this is correct, you'd want to create a file, ~/.vim/after/indent/plaintex.vim. In that file:
set autoindent&
set cindent&
set smartindent&
set indentexpr&
This will set all these values to their defaults whenever you open a .tex file.
There seem to be a little mix of terms in your question. In vim the term autoindent points to a special kind of indentation that simply follows the indent level of the previous line (which is quite handy sometimes). To remove it set noautoindent by hand, or write it in your _vimrc.
There are two other automatic kinds of indentation, cindent and smartindent. Similarly, if you wish to disable them go with set nocindent and set nosmartindent
If you look in help (help autoindent, ...) they are all quite nicely explained. Which one you prefer (or don't) is mostly determined by your programming style and habits. So, try them out and see which you like most.
Unfortunatelly, I don't use LaTeX that much anymore, so I'm not familiar with its internal filetype indentation rules.
For anyone else having a similar problem, a solution that worked for me was:
Use :verbose set indentexpr? to find what file was causing the de-indentation
Find where indentexpr is changed (for me it was setlocal indentexpr=GetTeXIndent())
Change that line to setlocal indentexpr& to turn indentexpr off
This removed all de-indenting from brackets, parentheses, and braces.
Remove the lines set autoindent and set smartindent to remove all vim autoindentation.
The following command finally stopped VIM from pretending it knows how to indent files for me and only do what I explicitly tell it:
:setl noai nocin nosi inde=
Courtesy https://vim.fandom.com/wiki/How_to_stop_auto_indenting
If you are using the vim-latex plugin, set this option:
let g:tex_indent_brace=0
For other plugins, if you don't want to turn off indentexpr as in the above answers, you can find where indentkeys is set and comment out those lines. This should stop triggering re-indent when you type a closing brace.
:verbose set indentkeys?

Resources