Why does Vim ignore my modeline values? - vim

I'm using a Python file with the following modeline at the end:
# vim: sts=4:ts=4:sw=4
If I start Vim with this file the modeline is ignored. How can I fix this? And BTW, I have nocompatible set in my .vimrc.

Here is what you check...
Step 1
Make sure your settings are right. Do...
:verbose set modeline? modelines?
If you see either of these values returned, it's not going to work.
nomodeline
modelines=0
(By adding verbose before the set command, you will be told what file produced that setting. Thank you, drew010)
What you need to see is this (where that 4 is anything greater than 0)
modeline
Last set from /usr/share/vim/vimrc
modelines=4
Last set from ~/.vim/vimrc
Note from the comments: If you have either nomodeline or modelines=0, you are going to need to add set and the corresponding setting from the previous code block. (Thank you #pilat)
Step 2
Do you have a line with a vim: that is not touching any else within the last <modelines> lines of your document?
Seriously, that's all it takes.
These modelines all work...
# vim: cursorline
// vim: cursorline
; vim: cursorline
vim: cursorline
# anything here vim: cursorline
even without a valid comment vim: cursorline
literally anything as long a space separates> vim: cursorline
# vim: set cursorline: <-that colon is required if you use the word "set" and this comment after the colon does not affect it.
Notice that the only way to have a comment after the modeline is to use the word set. But if you use set, you must put a colon at the end of your settings even if you have no comment. Thank you, Amadan
Step 3
And this is an important one! Are you using the word set? (as described as the "second form" in the options doc) Take out the set or add a colon to the end.
None of these work...
# vim: set cursorline
^^^ issue: use of the word set without a trailing colon (:)
# vim:cursorline
^ issue: no space after the colon
#vim: cursorline
^ issue: no space before the word vim
Step 4
Have you been awake for more than 18 hours? Before you lose your mind, get some sleep. It'll still be broken tomorrow. You'll be more likely to notice the issue when you go through this list again then.
Update notes
This answer used to include this claim:
Are you using the word set? (as described as the "second form" in the options doc) Well, that doesn't work in version 8, and I don't know when it stopped working. Use the "first form".
I was wrong. It was pointed out by #Amadan and I have corrected Step 3.

I had a similar issue with my modeline not working. The answers in this thread helped me find my answer (which was adding set modeline to my ~/.vimrc)
https://superuser.com/questions/323712/modeline-not-work-in-vim
Also one thing that helped me debug this was to type :set in vim. This will tell you the different values that are currently set.

If you are saving and loading views, for example with something like the following, your modeline will not be reevaluated when you reopen a file.
autocmd BufWinLeave *.* mkview
autocmd BufWinEnter *.* silent loadview

I tracked the problem to a local plugin, called local_vimrc.vim. The fact that modeline does not work is a side-effect of the plugin.

The first step is always look in
/usr/share/vim/vim74/debian.vim
and comment out the nomodeline line.
Kubuntu distributions consider it a security
threat.
(EG: libcal allows you to run an arbitrary program)
This is only an issue if someone off system can run vim via a webpage. (don't trust user input.)
It is also possible to do a ddos attack using spell checker. (don't trust user input.)

I have had such problem with my modeline and "noexpandtab":
# vim: noexpandtab ft=python
but if I write:
# vim: ft=python noexpandtab
it works again. (Thank you guys for knowledge about :verbose set ... !)

Related

Vim: How to permanently allow auto indent only on manual linebreak

Vim has a tendency to automagically mess with the indentations in the line I'm currently working on, as well as placing linebreaks. I want it to do an automatic indentation when I manually make a linebreak (i.e. physically hitting the Enter key), but not in any other case, I don't want it to make any linebreaks itself either, and I want these settings to be permanent.
The textwidth option controls the number of characters after which a line break will be inserted, see :h textwidth.
To turn off the insertion of line breaks while editing, the following lines should be added to .vimrc:
set textwidth=0
set wrapmargin=0
set formatexpr=
Information on the options can be obtained by the usual :h command.
Note that these options are set globally but may be overridden e.g. by a filetype plugin. If the problem persists, see :h ftplugin.
Another question is where :set textwidth=132 originates from. This may be due to an earlier misconfiguration or set from any plugin.

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 does remove indent for all lines starting with # character

I have already read this:
Vim automatically removes indentation on Python comments
I have tried everything that is mentioned there without success:
I have smartindent off
I use filetype indent on
I tried the trick with :inoremap # X^H#
None of the above helps: Whenever i start a indented line with a # the indentation is removed and the cursor is moved to column 0.
Here's the output of :set: https://gist.github.com/mikehaertl/5387743
And here's the vimrc.local that i use on Ubuntu 12.10: https://gist.github.com/mikehaertl/1612035
So i'm clueless what else i could try. I don't want my cursor to be moved to column 0 whenever i type an indented #. Any suggestions?
UPDATE
So i found out this is caused by cindent. Still this is very obscure to me: Why does vim do that and how can i prevent that from happening if i still want to use cindent?
If you are using cindent, it probably contains the 0# part which comes by
default. You just have to remove it, for example by using an auto command to
be triggered when the file type changes to the type(s) you want with this
indentation disabled.
Is it PHP? If so, adding this line to your .vimrc may help:
autocmd FileType php set cinkeys-=0#
The 'formatoptions' option governs that behavior. What is the output of :set fo?
croql is a good value, see :h fo-table.

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

Resources