How to load a vimscript file after loading plugin utl.vim? - vim

I need to source some configuration lines after loading a plugin called [utl.vim][1].
The documentation says, I need to put these configurations into after-directory:
[23] NOte that you cannot include this line in your .vimrc because it is
:source'd before utl_rc.vim (which defines g:utl_cfg_hdl_mt_generic).
So either include/change it at <url:config:#r=utl_cfg_hdl_mt_text_directory>
or include it in the after-directory, see <url:vimhelp:after-directory#5.>
I created a file called utl.vim inside ~/.vim/after/ directory.
But this file is not sourced. I verified this using :scriptnames.
How to assure that Vim sources this file after loading utl.vim plugin?

The list of files/directories loaded by Vim is described at :help startup. Despite it lists the plugin directories, it doesn't mentions the after directory, so what you are seeing is the expected behavior.
Usually the after directory is source for filetype plugins, as explained in Vim FAQ 26.3 - "How do I extend an existing filetype plugin?". It is possible that the plugin will run something like :runtime! after/**/*.vim, but if the documentation is unclear will should ask to the plugin author, because it would be probably easier for users to be allowed to set this variable, and them the plugin could append/prepend default values to it

Related

Vim not recognizing some commands from vim-latex

I have vim-latex installed via Vundle and I'm trying to disable some annoying mapping that it sets up by default. From the docs I know that, for example, I can use the following command to unmap FEM:
call IUNMAP('FEM','tex')
But when I type that I get the error E117: Unknown function: IUNMAP.
I have installed vim-latex with Vundle by including Plugin 'LaTeX-Suite-aka-Vim-LaTeX' in my vimrc and I have just used the PluginUpdate command to update everything, which runs with no error, so I should have the latest version of the package.
Am I missing something here?
Actually, the problem you're having is related to where you're getting your vim-latex from.
With:
Plug 'LaTeX-Suite-aka-Vim-LaTeX'
You're getting it from here, which you'll notice hasn't been updated since 2010. Looking at the plugin/imaps.vim file in that repository, you'll see there's a definition for function IMAP(), but not for IUNMAP(), which was probably introduced after the last date that repository was synced...
Use this source instead:
Plug 'vim-latex/vim-latex'
Which will get it from here which is an official maintained location for this plug-in.
If you look at plugin/imaps.vim in that source tree, you'll notice function! IUNMAP is defined there.
Updating to the correct plug-in location should fix this problem for you (together with probably quite a few fixes from the last 10 years!)
The functions IMAP() and IUNMAP() are loaded by the vim-latex plug-in only after your vimrc is processed. So you need to execute them from a context where they're available.
Furthermore, in order to have your unmapping succeed, you need to actually execute it after the mapping was created, and mappings are typically created when the filetype is set.
The documentation mentions that these overriding rules should be done in specific files:
An important thing to note is that if you wish to over-ride macros created by Latex-Suite rather than merely create new macros, you should place the IMAP() (or IUNMAP()) calls in a script which gets sourced after the files in Latex-Suite.
A good place typically is as a file-type plugin file in the ~/.vim/after/ftplugin/ directory. [...]
For example, to delete a mapping, you can use
call IUNMAP('FEM', 'tex')
in ~/.vim/after/ftplugin/tex_macros.vim.
The documentation mentions that you should use a file in ftplugin after the filetype you use. Check :set ft? to confirm yours is indeed tex, in which case you can use tex.vim, tex_something.vim (like the suggested tex_macros.vim) or tex/something.vim (a subdirectory.)

How to organize content .vimrc in multiple files

Is there any methodology to organize the content of .vimrc in multiple files?
Currently my .vimrc has everything: Plugs statement, remaps, sets,functions,mapleader, custom plugins configuration, autogroup, etc.
I would like to organize in multiple files, perhaps by language.
Thank you!
You could split the contents of .vimrc into different files. Then source those files from within vimrc. Example:
" File: vimrc
" Author: Jonh Doe
" Options, commands, and auto commands
runtime settings.vim
" Maps
runtime maps.vim
" Functions
runtime functions.vim
" Plugin settings
runtime plug-settings.vim
Filetype-specific settings should be stored under ~/.vim/ftplugin, or less commonly, under ~/.vim/after/ftplugin. For example, html settings should be in ~/.vim/ftplugin/html.vim.
I suggest to take a look at vimrcs of experienced vim users. This repository should be a good starting point.
After your ~/.vimrc, Vim starts sourcing the plugin/ scripts. For your own mappings and custom commands, you can just place them anywhere in there, e.g. ~/.vim/plugin/MySuperCommand.vim and ~/.vim/plugin/MyLifeSaverMappings.vim.
For customization of other plugins, or Vim options that influence the behavior of plugins, it is crucial that these execute before the plugin(s). By using the ~/.vimrc (the general recommendation), this is ensured automatically. You can explicitly :runtime the scripts from your ~/.vimrc, as per #Sergio's answer, or define them as plugin scripts, too, but ensure that they run first:
If you install plugins the traditional way (in ~/.vim/plugin/), you can use the script naming (e.g. 00MyStuff.vim) to influence loading order.
If you use pack plugins, or a package manager, just ensure that your own plugin directory comes first in the 'runtimepath'.
The :scriptnames command is a valuable troubleshooting tool to verify that the script loading order is correct.

Vim: load syntax file from local directory

I would like to be able to create a vim syntax file for a specific project, to highlight identifiers unique to that project. I could, of course, install a syntax file in $HOME/.vim/after/syntax/*.vim but I want to set this up as a per-folder configuration. The project syntax file should add on to the existing syntax file, not replace it. I also want a general solution that automatically loads a local file it if exists, and doens't need to be configured for specific directories in .vimrc or in a plugin.
Edit: This is similar to the question Vim: apply settings on files in directory but that deals with settings in .vimrc, which is loaded before syntax files. Syntax files conventionally wipe out predefined syntax information, so it is not useful to set this infomration in .vimrc, and the techniques to load alternate .vimrc files are therefore not helpful. I don't know of a way to trigger additional "after" files to be loaded. I have seen this question asked before but no answers except those pertaining to .vimrc.
I have configured local .vimrc files using :set exrc and have verified that these files get loaded, but syntax definitions in .vimrc get overwritten by syntax files that are loaded later on. (And I want project syntax to add to standard syntax, not replace it.)
Is a similar functionality, whether a vim feature or a plugin, that can be used for local "after" syntax files?
I adapted a solution based on this answer: https://stackoverflow.com/a/13192721/2150289. While I'd still like to explore other options if they exist, this works. Leaving this as a separate question and answer, since .vimrc answers don't apply, and it is not obvious that this is possible for a syntax file.
In my project directory I have a local .vimrc that sets tabs and so forth. This is loaded by set exrc or any other method that loads a local .vimrc.
To load "after" files, you can daisy-chain them to an "after" file that gets loaded in your .vim/after directory. For example, I have a .vim_syntax_cpp.vim with C++ syntax specific to the project. I place this is the project directory:
PROJECT/
.vimrc # gets loaded by set exrc
.vim_syntax_cpp # daisy chained from .vim/after/syntax/cpp.vim
Makefile
foo.cpp
bar.cpp
In ~/.vim/after/syntax/cpp.vim, I place this code:
" load syntax file if it exists in the
" current directory or parent directory
if filereadable(".vim_syntax_cpp.vim")
source .vim_syntax_cpp.vim
elseif filereadable("../.vim_syntax_cpp.vim")
source ../.vim_syntax_cpp.vim
endif
I have verified that this loads the local syntax file and adds them to existing syntax definitions.
This method has limitations; see the comments in the linked answer.

howto change location of .vimrc and .vim

How can I change the location of the .vim folder and the .vimrc file so that I can use two (or more) independent versions of vim? Is there a way to configure that while compiling vim from source? (maybe an entry in the feature.h?)
Why do I want to do such a thing?:
I have to work on project that use python2 as well as python3, therefore I want to have two independent vim setups with different plugins, configurations etc. Moreover, one version has to be compiled with +python, the other with +python3.
You can influence which ~/.vimrc is used via the -u vimrc-file command-line argument. Since this is the first initialization, you can then influence from where plugins are loaded (i.e. the .vim location) by modifying 'runtimepath' in there.
Note that for editing Python files of different versions, those settings (like indent, completion sources, etc.) are taken from filetype plugins which are sourced for every buffer separately, so it should be possible to even edit both Python 2 and 3 in the same Vim instance. (Unless you have some badly written plugins that define global stuff.) So for that, some sort of per-buffer configuration (some :autocmds on the project path, or some more elaborate solution (search for localrc plugins or questions about project vimrc here) might suffice already.
Also note that when the Python interpreter (which you'd only need for Python-based plugins and some interactive :py commands, not for editing Python) is compiled in with dynamic linking (which is the default at least on Windows), you can have both Python 2 and 3 support in the same Vim binary.
I think the easiest solution would be just to let pathogen handle your runtimepath for you.
pathogen#infect() can take paths that specify different directories that you can use for your bundle directory.
So if your .vim directory would look like this
.vim/
autoload/
pathogen.vim
bundle_python2/
<plugins>
bundle_python3/
<other plugins>
Then inside one of your vimrc for python 2 specific stuff you would have
call pathogen#infect('bundle_python2/{}')
and for python 3 specific stuff you would have
call pathogen#infect('bundle_python3/{}')
Since each plugin folder is really just a .vim folder you can place your python specific configuration stuff in a folder of the corresponding bundle and pretend its a .vim.
This structure also has the added benefit that you can change both configurations at the same time if you feel like it by putting common stuff in .vim normally.
You can also pass multiple bundle directories if you feel like to pathogen so you can have plugins that are shared without duplicating files. You just do this by passing multiple paths to pathogen#infect('bundle/{}', 'bundle_python3/{}')
After this is all done you can just create aliases for vim to call the correct vimrc file.
I found a way to do this!
You can just create a fake $HOME, whose contents are simply the .vim folder and .vimrc. Then, when running vim, set the HOME environment variable to that folder, and change it back on VimEnter.
Run vim with:
OLD_HOME="$HOME" HOME="$FAKE_HOME" vim
Add this to your .vimrc:
autocmd VimEnter * let $HOME = $OLD_HOME
On Windows you can use
let $HOME = $HOMEDRIVE.$HOMEPATH
insetead, no need to store the old home.
This works, but if you use $HOME inside your vimrc or any plugins will see the old value, it might affect them somehow. So far I haven't seen it.
Note: I don't really recommend doing this.
If you really really want to recompile vim so that it uses a different vimrc and different configuration directory take a look at src/feature.h
Search this file for USR_VIMRC_FILE. Uncomment it and place the name of your vimrc here. This will change the defualt vimrc file.
So it should look something like this
#define USR_VIMRC_FILE "~/path/to/vimrc"
Inside src/os_unix.h or src/os_mac.h and search for DFLT_RUNTIMEPATH. Change all instance of ~/.vim to whatever folder you want. This should set the default runtime path that vim searches for settings.

Please explain Vim's configuration directories hierarchy

I am trying to update my Vim configuration scripts. There are a number of sub-directories in my ~/.vim directory and I'm not sure the specifics of what they do, nor why there are some that seem to be redundant.
Here is what my ~/.vim directory tree looks like
|-after
|---ftplugin
|---syntax
|-autoload
|-compiler
|-doc
|-ftplugin
|---latex-suite
|-----dictionaries
|-----macros
|-----packages
|-----templates
|---python
|-indent
|-plugin
|-spell
|-syntax
Now for the specific questions.
What goes in plugin vs ftplugin?
What is the difference between plugin and autoload?
When should I put something in after/... instead of in the directories directly under ~/.vim?
Whatever goes into plugin is loaded whenever vim starts whereas what you put in ftplugin is only loaded for the specific filetype it corresponds to (so if you have a folder there called python all the files there will be loaded when you open a python file.
In autoload you should have the functions corresponding to the the scripts defined in plugin. The functions here will only be loaded when called by the first time.
In after you should put settings that you want to change from the normal plugin loading.
As an example suppose you like the settings that some plugin for latex gives you, but it redefined a mapping that you had in your .vimrc. You can revert this with autocommands or by putting the correct definitions in after.
I think this would explain exactly what each folder does: http://learnvimscriptthehardway.stevelosh.com/chapters/42.html
Also might want to check :h runtimepath.
Hope this helps. :)
All those directories are part of the runtimepath. Try :h runtimepath and it will link you to your answers:
Check :h write-plugin
Check :h autoload-functions
after/... scripts are executed last, so they can override settings of earlier scripts. You can read more about this in :h runtimepath.

Resources