VIM cpoptions+=n in an XML file - vim

In my vimrc I set cpoptions+=n. Usually this option stays enabled, but as soon as I open an XML file it gets disabled. I've looked through the vim folder for things that modify cpo but can't see anything interesting (other than some saving and restoring of the options so that -=C can be used, but commenting those out changes nothing).
Any idea what's causing this or some pointers as to what to look for?

Open a XML file and run :verbose set cpoptions?. This will show you the current value of the setting and what file set that value. This setting is likely being overridden by the syntax file for xml (it is in my case).
Edit: Although this normally helps, it seems in this case there are many files loaded for the XML filetype that simply save cpo off, change it for their script, then restore the original value. It doesn't look like this is going to help you much.
You may have better luck opening up an XML file and running :scriptnames, then grepping through those scripts listed for cpo to see if you can find the culprit.

If you use GVIM, set it in your .gvimrc, because this is the last loaded configuration file. See :help initialization
other way, as #Randy Morris said, a plugin might set it to the default value. In my configuration, Tabularize set it last time in .vim/bundle/after/plugin/TabularMaps.vim.
EDIT: I found it !
In autoclose plugin, in plugin/autoclose.vim the saved cpoption is not writed back at the end. Simply add
" restore cpoptions
let &cpo = s:global_cpo
to the end of the file.
If you dont use the autoclose plugin, you can search for plugin files that reset the state of cpotions by :vimgrep "set cpo&vim" ~/.vim/**
then in the list you can search after the files that don't restore the original state (usually at the end of script a line starting with let &cpo = is missing).

Related

Vim Comments Format Options Repeat Not Working

I set the comments option for a filetype to b:*. If I open such file I can verify this with set comments?. Now further I explicit set the formatoptions to use r for repeating the comment on a new line. After all calling set formatoptions? return in sum j1tcqln.
If open a new line o and write something like * This is a comment hit <CR I expect to get a * on the new line es well, cause the r option, but is doesn't.
The check if the +comments feature is available with :echo has('comments') is positive and returns 1.
Whats the problem here?
Edit:
This is my ftplugin/markdown.vim:
setlocal comments=b:*,b:-,b:+,b:>
setlocal formatoptions+=r
setlocal formatoptions-=c
echom 'Format: ' . &formatoptions
The last line leave a correct message when open a file with this type. But calling :echo &formatoptions manually afterwards, its again the false output.
It seems like no matter what I do in this ftplugin afterwards its not there anymore, even though it has been executed.
Edit:
It looks like my ftplugin gets overwritten by the default one in /usr/share/nvim/runtime/ftplguin/. Does this make sense and how can I mix them up?
Filetype plugins are sourced in this order, each potentially overriding the preceding one:
$HOME/.vim/ftplugin/markdown.vim
$VIMRUNTIME/ftplugin/markdown.vim
$HOME/.vim/after/ftplugin/markdown.vim
--- edit ---
The first location is suitable for two scenarii:
you are creating support scripts for a language not natively supported by Vim,
you want to prevent Vim from sourcing the default support script for a specific filetype (the solution in your answer).
The second location is off-limits. Four main reasons for this:
your changes can and will be overridden during future updates,
your changes may make Vim unstable,
having to sudo your way to your config will get annoying pretty quickly,
put your stuff in your $HOME.
The third location is sourced last so it allows you to selectively override default settings and add your own. This is the safest place for your own filetype-specific settings.
The solution is to add this at the header of the ftplugin file:
if exists('b:did_ftplugin') | finish | endif
let b:did_ftplugin = 1
Then is necessary cause the ftplugin in /usr/share/nvim gets loaded after and does not been executed if this variable is set.

How to set a VimScript to be executed last?

Problem
A setting in my vimrc (set noshowmode) is being overridden by a plugin later in the loading process.
Goal
Have a VimScript file be executed last (or at least after plugins).
What I Know
Plugin VimScripts are executed after the vimrc (Source).
The after-directory is run close to last and holds user overrides
(Source: :h after-directory).
Vim's runtimepath determines the order of what is run.
Failed Attempts
I tried appending a VimScript file (containing set noshowmode) to the
end of $VIMRUNTIME with
set runtimepath=$VIMRUNTIME,~/.vim/nosmd.vim, but this method ended up
messing up other plugins (namely vim-airline, which did not load).
I also tried creating the ~/.vim/after directory and putting my
VimScript in there, but this had no effect.
Your attempts
set runtimepath=$VIMRUNTIME,~/.vim/nosmd.vim
That cannot work. 'runtimepath' contains a list of root configuration directories; you cannot directly place script files in there. Instead, point to a directory that contains plugin/yours.vim.
I also tried creating the ~/.vim/after directory and putting my VimScript in there, but this had no effect.
You can check with :scriptnames to see whether your script was executed (and at the end of the plugin load sequence!)
Solutions
An ounce of prevention is better than any workaround. I would first try to locate the culprit who changes the 'showmode' option; a plugin shouldn't do this (or at least have a configurable option to disable it).
:verbose set showmode?
might already tell you who changed this setting.
As #romainl already commented, ~/.vim/after/plugin/myplugin.vim should work for undoing this. If it doesn't try the following autocmd (in your ~/.vimrc) as a last resort:
autocmd VimEnter * set noshowmode

Vim opens some files in readonly mode by default even when use sudo

I have weird issue with vim - for some files it doesn't color the syntax and opens it readonly mode by default, even if I use sudo. chmod for this file are 664, I am also owner of this file, so normally i shouldn't.
I have no set ro in my vimrc.
I noticed also that it often happens after removing .swp for this file - unfortunately, after this, this particular file is every time opened in readonly mode. One idea is - maybe is this some kind of spf13 cache?
For other files the behavior is correct.
Anyone knows this issue?
I found solution - it was more trivial than I expected.
I read the vim messages more carefully and I saw at the end:
if you did this already, delete the swap file `~/.vimswap/urls.py.swp`
So i did small investigation and i found .vimviews inside my /home directory. I just removed it's content and ...it works!
Probably spf13. There are WAY too many problems caused by spf13 in my opinion. Try doing :verbose set readonly? when you see the problem, to see where it happened from. My guess:
I think I remember spf13 having some kind of automatic session management built in, this would restore 'readonly' on a file if it had ever been set on that file.
It is quite possible (a fairly common solution) that if spf13 detects an existing swap file, it will automatically open the file in readonly mode, triggering (1).
It is also quite possible that some autocmd or another related to (2) sets an empty filetype or syntax, which would likewise be remembered by (1).
If this is the case, you can probably find the session file causing the issue (using that :verbose set readonly? command) and delete it.
Also consider, whether you really need all of spf13, or if you could achieve your desired configuration easier by installing plugins and configuring Vim yourself.

set tags=tags in gvimrc not working, unless set it explicitly

I have a problem to set my tags file correctly. It use to work without problem after I reinstalled the system. error message like
E433: No tags file
E426: tag not found: Pids
accurs when I press ctrl+].
I have this line in my .gvimrc file
set tags=~/projectdirectory/tags
and tags-exuberant installed properly.
It works fine when I type :set tags=~/projectdirectory/tags in gvim
I also tried use set tags=~/projectdirectory/tags;/
All other .gvimrc settings function well. How this could be possible?
UPDATE:
I have solved the problem, it is because I have multiple tags setting in ~/.gvimrc, vim take the last one in current session.
You can check the actual effective value (after starting GVIM) via
:verbose set tags?
The option might have been overwritten by a later :set command, or a plugin.
Even if you only use GVIM, it's recommended to put the general settings into ~/.vimrc (which is also sourced in GVIM), and keep ~/.gvimrc reserved for GUI-specific settings. An important difference between the two is that the latter is only sourced at the very end, so it's unsuitable for configuring plugins.

Manually enable undodir, when needed

I have enabled undodir, to keep track of changes I make even after I close my vim session.
I sometimes want to edit a file, make some changes, save it, then return to the original file of that session. (This might be one of the possible use cases)
If I had undodir disabled, I could simply keep hitting u until it showed me a message.
So I want undodir to be disabled in undo/redo by default, and I should have a command to enable it when needed.
All changes to the file must be tracked in either states, at all times.
Is this possible?
As far as I understand the documentation, only setting undodir does nothing unless undofile is set to true.
So, I assume that you want to activate undofile for certain files.
First thing that comes in mind is a modeline in order to set undofile to true for certain files. But unfortunately, this doesn't work.
The issue, however, is present on the vim developer mailing list and there was a fix provided in January: http://comments.gmane.org/gmane.editors.vim.devel/32896. This fix could be present in current sources; so if you'd like to try, grab the latest vim sources, build it and check if you could use a modeline for setting udf
Until there's an official version containing that fix, you could get around your issue using undofile.vim. Excerpt of it's description:
If you want 'undofile' only for certain files, you will notice that 'undofile' cannot be set in a modeline, or once the buffer is loaded (because an existing undo file will not be loaded then). Bram suggests to use a BufReadPre autocmd which sets 'undofile' before the buffer is loaded. This script does the steps for you.

Resources