How can I find out why vim keeps changing my expandtab setting - vim

I use vim. Specifically I am using Janus. I have expandtab set. However, during the course of using vim, for some reason, my expandtab setting gets set to noexpandtab, and my files start to gain hard tabs. I have tried typing :verbose set expandtab? but this does not show me anything (specifically, it shows me that noexpandtab is set, but it doesn't show a file that is responsible for setting it).
So I would like to find out:
Why my expandtab setting might be changing
How I can track down the culprit and prevent it from happening
Thanks

Try this
:verb set expandtab?
:verb set et?
:verb set invexpandtab?
expandtab can really be set in a number of ways :/

I should add that in addition to the above if you tried the following:
:verb set expandtab?
:verb set et?
:verb set invexpandtab?
And you get back with no line number or file:
noexpandtab
It's more than likely that you have the following and need to change the order:
set expandtab
set binary
set noeol
Change to (note the order)
set binary
set noeol
set expandtab
The reason for this is due to set binary command has a few defaults it executes after it runs. Including the following:
'textwidth' will be set to 0
'wrapmargin' will be set to 0
'modeline' will be off
'expandtab' will be off
I noted DavidWinterbottom called this out in his comment, but that seemed to be the only comment that was hidden by default and this caused me to do a lot more reading that was necessary, so hopefully this post helps another poor soul from wasting countless hours tracking this down.

VIM settings can either be set in a configuration file, or in a modeline inside the file you're editing. Note that expandtab can be shortened in VIM to et, so be sure to look for that as well.
Possible configuration files I'd look for:
/etc/vim/vimrc and other files in that directory (sometime vimrc.local and such)
$HOME/.vimrc
As for modelines, they are simply configuration options for VIM, located as comments in the file itself. If this only happens with some files but not others, look for comments that look something like:
/* vim: set noet ai tw=75: */
And try to remove them and see if it helps.

Related

Vim doesnt respect vimrc indentation settings

I am new to using Vim, and have been using "The Ultimate Vim configuration" on GitHub. It uses 4 spaces wide tabs as default, so to change that, I changed my .vimrc file to the following:
set runtimepath+=~/.vim_runtime
source ~/.vim_runtime/vimrcs/basic.vim
source ~/.vim_runtime/vimrcs/filetypes.vim
source ~/.vim_runtime/vimrcs/plugins_config.vim
source ~/.vim_runtime/vimrcs/extended.vim
" Own settings
set number
set tabstop=2
set softtabstop=2
set shiftwidth=2
set expandtab
set smarttab
try
source ~/.vim_runtime/my_configs.vim
catch
endtry
The my_configs.vim file is deleted, so I know that the indentation settings are not being overwritten there.
The problem is, if i source the vimrc file from vim with :source ~/.vimrc The indentation works perfectly, but if I close Vim and open again, then the indentation is back to 4 spaces wide instead of 2 as specified in the vimrc... All other settings from the Ultimate Vim Configuration get loaded from vimrc but not the indentation settings, which is really frustrating, as I need to source the vimrc file every time i want to use vim.
Any help is greatly appreciated, I really would like to exclusively use Vim but if this cannot be solved then I need to keep using Visual Studio Code :(
Thank you in advance.
EDIT: Here is the output of running command :verbose set tabstop? softtabstop? shiftwidth?:
tabstop=2, Last set from ~/.vimrc line 10
softtabstop=2, Last set from ~/.vimrc line 11
shiftwidth=2, Last set from ~/.vimrc line 12
When you're editing a Python file in Vim, it will auto-detect the file type and apply Python specific settings. Amongst those, it will set the soft tab stop recommended by PEP 8.
You can find these settings in ftplugin/python.vim in your Vim runtime:
if !exists("g:python_recommended_style") || g:python_recommended_style != 0
" As suggested by PEP8.
setlocal expandtab shiftwidth=4 softtabstop=4 tabstop=8
endif
You can override those settings with a global variable, which you can set in your vimrc. To disable the Python tab stop automatically set by Vim, include the following somewhere in your vimrc:
let g:python_recommended_style = 0
Note that those settings are there for a reason, they're pretty standard for Python and it's highly unusual to see Python code indented by a different number of spaces... But you have the knob to override it if you really want to.

vim: check where a setting value was modified

I should probably written this down somewhere because it's not easy to remember and not often used. But anyways,
I have a setting like 'expandtab'. I'd like to change to 'noexpandtab'. But I can't figure out in which rc file it was set that takes the last effect. There is a trick to know where a setting was last set. What's the trick?
Use :verbose along with :set {option}?:
:verbose set expandtab?
You should see something akin to:
expandtab
Last set from ~/.vimrc

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

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#

Vim ignores shiftwidth specified in .vimrc

I am using Vim 7.3.154 cli on Linux Mint 12 "Lisa"(x64).
I prefer using a tabstop and shiftwidth of 2 columns.
My .vimrc has
:set ts=2
:set sw=2
to accomplish the same.
Whenever I open a new instance of Vim, tabstop value is retained, so existing tabs are rendered according to .vimrc. But somehow the shiftwidth value gets changed to 3. This results in 3 column indention while auto-indenting.
Running :set sw=2 on the new instance of Vim fixes this issue.
Can anyone tell me why Vim is ignoring/changing shiftwidth value from .vimrc?
I tried disabling all plugins to no avail.
Vim compiled options | .vimrc
This answer just summarizes what we discussed in the comments. :)
This is most likely caused by the fact that you have file specific settings enabled. Check :h modeline for more info on that. Make sure that those files you have the problems with don't have this line in them.
Instead of only setting tabstop and shiftwidth you should also setup a couple more settings regarding whitespace and tabs.
set noexpandtab " Make sure that every file uses real tabs, not spaces
set shiftround " Round indent to multiple of 'shiftwidth'
set smartindent " Do smart indenting when starting a new line
set autoindent " Copy indent from current line, over to the new line
" Set the tab width
let s:tabwidth=4
exec 'set tabstop=' .s:tabwidth
exec 'set shiftwidth=' .s:tabwidth
exec 'set softtabstop='.s:tabwidth
Check this video for some extra info: http://vimcasts.org/episodes/tabs-and-spaces/
So this is the answer to the actual problem that you were able to solve. That php.php file was in the /plugin directory. That directory gets loaded once, each and every time Vim starts up. Check this: http://learnvimscriptthehardway.stevelosh.com/chapters/42.html#vimplugin
If what you want is that file to load only on PHP files then you should put it in the /ftplugin folder: http://learnvimscriptthehardway.stevelosh.com/chapters/42.html#vimftplugin
Read the documentation there, it should be a .vim file, in other words in this case it would be called php.vim.
What Pathogen or Vundle do is modify the runtimepath (:h runtimepath), nothing more, nothing less.
So you can now accept this answer, by clicking the little green arrow to the left of this answer. :)
You can run:
verbose set shiftwidth ?
to check what is setting the value of shiftwidth. It might coming from a plugin. You can also choose to modify the value there.

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...

Resources