Can you query what a variable/setting in vim is set to? - vim

For example, in vim, if I want to know if autoread is set or not, is there a command I can run to tell me? Or to know what my tabstop is set to?

In your case, :set autoread? will give you the current value of autoread. Generally, set foo? will give you the value of option foo.
:set will display all options that are different from default.

:verbose set autoread? will tell you what set autoread and its value.

You can also do, for example,
echo &ft
The & refers to the contents of the variable. I find this useful in scripts.

You can view settings in vim with this command
:set all

Related

Display the current status of `expandtab` in statusline in macvim

I am currently using vim-airline in my macvim and I want to display the status of expandtab whether it is set or not in the statusline.
I can find out the status of expandtab by running the following command :set expandtab?. From the vim-airline documentation I found that I can use something like this
let g:airline_section_b = '%{getcwd()}'
I modified it to
let g:airline_section_b = '%{expandtab?}'
but I am getting the error undefined variable: expandtab.
Can someone kindly tell how I can retrieve the status of expandtab and then show it in the status line. Thanks.
:set does not access variables, so you cannot use the question mark to query variables.
You are trying to access the variable expandtab variable, which doesn't exists. You actually want to access an option setting and those are accessed using the & prefix.
Sou you should add:
let g:airline_section_b = '%{&expandtab}'
Note, the quesion mark is not necessary and has no special meaning for VimL.
See :h expr-option for the details.
Update
This will only display 1 (expandtab set) or 0 (expandtab not set). What should work however is something like this:
let g:airline_section_b = '%{&expandtab?"et":"noet"}'
Which will display 'et' when expandtab is set or 'noet' when expandtab is not set. This uses the <cond>?<true>:<false> expression to display a certain string depening on the value of the <cond> condition. This is explained in the help below :h expr1
Vim options can be accessed live variables if prefixed with &. Example:
let g:airline_section_b = '%{&expandtab}'
See :h :let-& for more

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

Why :help ignore :set number in .vimrc?

I :set number in ~/.vimrc to show line numbers. When I type :help to view documentation, the line numbers don't show in the new window.
If I type :setl number?, it prints nonumber. If type :setg number?, it prints number.
I want to know why ~/.vimrc doesn't work. Which script resets the local number option?
I've checked the $VIMRUNTIME/ftplugin/help.vim, but it doesn't reset the number.
Currently, I'm using:
if has('autocmd')
auto FileType help set number
endif
Thanks.
I'm not sure what is the problem you're having. Numbers don't show up in help - yes, if I recall correctly that is a design decision and a feature - a good one, in my opinion, since why would one want line numbers in help files? (Okey, you could say "to quote a particular line from help file" but tags seems sufficient for that).
As far as the other thing go, when I set
setl nonumber
setg number
and open a new buffer in a split, numbers show.
If I start a new vim session, setlocal and setglobal number being nonumber and set number they're both changed.
Is this the behaviour you're having too?
After opening vim, simply type:
:auto FileType
Do you see set number listed under help? If not, your .vimrc is not being read.

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