So in my .vimrc I have this:
let g:syntastic_python_pylint_post_args="--max-line-length=80"
I want to have something like:
let g:syntastic_python_pylint_post_args="$(cat .line_length.txt)"
And I want this cat command to run whenever I open vim.
For example if I have a file in a project
myproject/.line_length.txt
And the contents of the .line_length.txt is 120 for example and I run
vim
inside that directory, it should read that file and set the contents of the file to the variable.
...
Is this possible somehow?
I have tried using project specific .vimrc files but it does not seem to read
let g:syntastic_python_pylint_post_args="--max-line-length=120"
It will read set ... lines though, but not let g: ... lines.
A literal translation of your attempted $(...) syntax would be this:
let g:syntastic_python_pylint_post_args = substitute(system('cat .line_length.txt'), '\n\+$', '', '')
But there's actually no need to run an external command for this. Vim has the low-level :help readfile() function:
let g:syntastic_python_pylint_post_args = readfile('.line_length.txt')[0]
Caveats
Depending on the location you start Vim in, the configuration may not exist. You need to account for that, probably using a filereadable() conditional around it, or just ignoring any errors with :silent!.
Some plugins only read their configuration variables during startup, and after that ignore any changes to it. This should be fine for your plan to run in ~/.vimrc, but it may affect your ability to "reload" a different project during runtime (see below).
Refresher on local configuration
Your solution depends on Vim being started inside the "project directory". You've also mentioned project-specific vimrc as an alternative. Here are some options for that:
Central configuration
If it's okay to configure the specific commands / local exceptions centrally, you can put such autocmds into your ~/.vimrc:
:autocmd BufRead,BufNewFile /path/to/dir/* setlocal ts=4 sw=4
It is important to use :setlocal instead of :set, and likewise :map <buffer> ... and :command! -buffer ....
On the other hand, if you want the specific configuration stored with the project (and don't want to embed this in all files via modelines), you have the following two options:
Local config with built-in functionality
If you always start Vim from the project root directory, the built-in
:set exrc
enables the reading of a .vimrc file from the current directory. You can place the :set ts=4 sw=4 commands in there.
Local config through plugin
Otherwise, you need the help of a plugin; there are several on vim.org; I can recommend the localrc plugin (especially with my own enhancements), which even allows local filetype-specific configuration.
Note that reading configuration from the file system has security implications; you may want to :set secure.
Related
I wonder if I can quickly edit some specific files based on project structures. For example if the folder identified as Laravel project this keymap will activated:
" Laravel framework
if(artisan_exist)
nmap <leader>lr :e routes/web.php<cr>
nmap <leader>lca :e config/app.php<cr>
nmap <leader>lc :e composer.json<cr>
nmap <leader>len :e .env<cr>
endif
if(webpack_exist)
nmap <leader>js :e webpack.mix.js<cr>
#endif
so what is the correct function for artisan_exist or webpack_exist so I don't need to use ctrlp or nerdtree because I think isn't necessary to tap many keys only to open those common files.
If you start Vim in a certain (project) directory, you can test relative file paths. For example, to check whether the file webpack.mix.js exists in the project root, use
if filereadable('webpack.mix.js')
nmap ...
endif
There's also isdirectory(), and glob() if you need to search entire subdirectories.
If this is for certain files, opened from any running Vim instance, and the mappings should be local to the file (and not only dependent on the filetype, for which Vim has a built-in detection and handling), you need a more fine-granular approach, usually called a local vimrc:
Central configuration
If it's okay to configure the specific commands / local exceptions centrally, you can put such autocmds into your ~/.vimrc:
:autocmd BufRead,BufNewFile /path/to/dir/* setlocal ts=4 sw=4
It is important to use :setlocal instead of :set, and likewise :map <buffer> ... and :command! -buffer ....
On the other hand, if you want the specific configuration stored with the project (and don't want to embed this in all files via modelines), you have the following two options:
Local config with built-in functionality
If you always start Vim from the project root directory, the built-in
:set exrc
enables the reading of a .vimrc file from the current directory. You can place the :set ts=4 sw=4 commands in there.
Local config through plugin
Otherwise, you need the help of a plugin; there are several on vim.org; I can recommend the localrc plugin (especially with my own enhancements), which even allows local filetype-specific configuration.
Note that reading configuration from the file system has security implications; you may want to :set secure.
I want to know if it's possible to setup vim in a way that when I am working in a certain repo, it will load additional functions/mappings etc.
Like ftplugin with file types, but the type is not defined by the file itself but by its location (or by the result of a command, e.g. hg path default).
This may work for your need, but it is just an example.
augroup repo
autocmd!
autocmd BufReadPost * call RepoMappings()
augroup END
function! RepoMappings()
let repo = '^/tmp/test/'
if expand('%:p') =~# repo
nnoremap <buffer> <f7> :echo 'yohoo'<cr>
endif
endfunction
You change the let repo = '^/tmp/test/', which would be the prefix of your repo path. In the above example, only for files in your "repo", the <f7> mapping works.
You can extend the repo variable to a list, or for different repo call different functions...
One thing is not handled, if you open a new buffer, without filename, you may want to check the pwd to see if it is in the "repo". But you got the idea, you can impl. it on your own if it was required.
Just give it a try.
Central configuration
If it's okay to configure the specific commands / local exceptions centrally, you can put such autocmds into your ~/.vimrc:
:autocmd BufRead,BufNewFile /path/to/dir/* setlocal ts=4 sw=4
It is important to use :setlocal instead of :set, and likewise :map <buffer> ... and :command! -buffer ....
On the other hand, if you want the specific configuration stored with the project (and don't want to embed this in all files via modelines), you have the following two options:
Local config with built-in functionality
If you always start Vim from the project root directory, the built-in
:set exrc
enables the reading of a .vimrc file from the current directory. You can place the :set ts=4 sw=4 commands in there.
Local config through plugin
Otherwise, you need the help of a plugin; there are several on vim.org; I can recommend the localrc plugin, which even allows local filetype-specific configuration.
Note that reading configuration from the file system has security implications; you may want to :set secure.
I am trying to customize vim highlighting by placing additional instructions into local config $project/.lvimrc, which is managed by the https://github.com/embear/vim-localvimrc plugin.
Unfortunately, it seems that commands like
syntax match Operator "\<MYOP\>"
located in .lvimrc are ignored silently by vim. Typing the command in the command line works as expected. Other commands from .lvimrc also work. So what may stop vim from interpreting local highlighting correctly?
That was because https://github.com/embear/vim-localvimrc plugin launches local files in a sandbox by default. Syntax commands are not allowed in a sandbox (at least in my setup), so the exception was raised. For some reason, Vim handles such exceptions silently.
In my case, the following modifications formed a solution:
Disable sandboxes for localvimrc by adding let g:localvimrc_sandbox = 0 to master .vimrc file
Add set conceallevel=2 to the localvimrc
It could be a problem with the loading order, i.e., your .lvimrc is loaded, then the filetype syntax is loaded and overwrites the .lvimrc syntax commands. You could check that by including echom statements on both files.
Also notice that the local vimrc is not the standard way of customizing syntax highlight. From Vim FAQ 24.11:
You should not modify the syntax files supplied with Vim to add your
extensions. When you install the next version of Vim, you will lose your
changes. Instead you should create a file under the ~/.vim/after/syntax
directory with the same name as the original syntax file and add your
additions to this file.
For more information, read
|mysyntaxfile-add|
|'runtimepath'|
currently there are a few paths where .vimrc files are being searched. (as can be seen in :scriptnames command).
How do I add another path?
You're mistaken. :scriptnames tells you which scripts were loaded. It will be:
.vimrc,
possibly .gvimrc,
plus all plugins in 'runtimepath',
plus all ftplugins, syntax files and indent files in runtimepath that apply for all the buffers you have edited,
plus the autoload plugins loaded by the previous files,
plus the files you've sourced manually.
If you want to change the places where to search for your .vimrc, it will be more complex as vim has a very specific heuristic to search for a .vimrc. See :h startup.
Any way. If you really want to add a path where the .vimrc file will be searched, it's not possible unless you define an alias to vim that use the -u flags.
If you want to add other paths where to look for plugins, you'll have to set the 'runtimepath' option in your .vimrc. For instance, :set rtp+=~/.vim/addon/foobar will have all plugins named $HOME/.vim/addon/foo/plugin/*.vim and $HOME/.vim/addon/foo/after/plugin/*.vim loaded automatically, plus the ftplugin/syntax file/indent files loaded automatically as well if you enable them, and when you enter a buffer related to them.
The 'runtimepath' option specifies the locations of the Vim configuration subdirectories (i.e. directories containing autoload/, plugin/, syntax/, etc.) The Pathogen plugin made it popular to extend this so that each plugin is installed into a separate such subdirectory, and other plugin managers (like Vundle) do that as well.
Now, there's only one .vimrc (and you can change its location via the -u command-line argument), but nothing prevents you from using :source path/to/another/script.vim to load other Vim scripts during startup.
TL;DR
To execute a separate Vimscript file during startup, just :source it from your ~/.vimrc. If you have a plugin(s) that you want to install in a separate location, use :set runtimepath+=path/to/pluginroot in your ~/.vimrc, or just use Pathogen or another plugin manager.
I'm on a system (linux) that always recognizes cpp files (*.cc) as tcl files. I don't know what file type that is, but I wanted to override it. The correct syntax highlighting is chosen if I manually do :set ft=cpp. However, I'm having troubles setting that automatically and I don't want to use the modeline option. My own .vimrc doesn't interfere (same result if I rename it).
From the vim help (:help ftplugin-override)
*ftplugin-overrule*
If a global filetype plugin does not do exactly what you want, there are three
ways to change this:
1. Add a few settings.
You must create a new filetype plugin in a directory early in
'runtimepath'. For Unix, for example you could use this file:
vim ~/.vim/ftplugin/fortran.vim
You can set those settings and mappings that you would like to add. Note
that the global plugin will be loaded after this, it may overrule the
settings that you do here. If this is the case, you need to use one of the
following two methods.
I have used this option before on another machine and that worked. I've tried
<file> .vim/ftplugin/tcl.vim
set filetype=cpp
"au BufRead,BufNewFile * set filetype=cpp
The first line correctly sets the filetype (:set ft? returns cpp), but syntax highlighting is not the same as if I said :set ft=cpp. It's still the tcl syntax highlighting. The second line does nothing.
2. Make a copy of the plugin and change it.
You must put the copy in a directory early in 'runtimepath'. For Unix, for
example, you could do this:
cp $VIMRUNTIME/ftplugin/fortran.vim ~/.vim/ftplugin/fortran.vim
Then you can edit the copied file to your liking. Since the b:did_ftplugin
variable will be set, the global plugin will not be loaded.
A disadvantage of this method is that when the distributed plugin gets
improved, you will have to copy and modify it again.
There seems to be no file in my $VIMRUNTIME directory /usr/share/vim/vim72/ftplugin/ called tcl.vim.
3. Overrule the settings after loading the global plugin.
You must create a new filetype plugin in a directory from the end of
'runtimepath'. For Unix, for example, you could use this file:
vim ~/.vim/after/ftplugin/fortran.vim
In this file you can change just those settings that you want to change.
Has the same effect as 1. Is there anything else I can try? Thanks a lot in advance.
cpp is the default filetype for *.cc and *.cpp files.
The tcl filetype is only set for *.tcl, *.tk, *.itcl, *.itk and *.jacl.
I see no reason why Vim would default to tcl when loading a *.cc file but you could check if theses files are installed:
/usr/share/vim/vim7x/ftplugin/cpp.vim
/usr/share/vim/vim7x/indent/cpp.vim
/usr/share/vim/vim7x/syntax/cpp.vim
and if the correct checks are done in:
/usr/shhare/vim/vim7x/filetype.vim
Are you the only person working on this machine? Do you use modelines?