Need some help on this to explain why I can't call a Plugin command from autocmd in VIM:
autocmd! Syntax * if !empty(&l:filetype) | RainbowToggleOn | endif
Vim detects a syntax error with:
Error detected while processing Syntax Auto commands for "*":
E488: Trailing characters: RainbowToggleOn | endif
I believe this issue caused by a reversed loading order. For some reason my autocmd in vimrc is running before the plugin is loaded. My guess is that it has something to do with the way Rainbow.vim is structured.
According to this answer the loading order is correct: Does Vim load plugins after loading vimrc?
So I was able to solve by delaying the ref lookup of the command:
autocmd! Syntax * if !empty(&l:filetype) | exec 'RainbowToggleOn' | endif
Related
when I use this script in my .vimrc:
"Save and restore folds when a file is closed and re-opened
autocmd BufWinEnter ?* if(!empty(glob(expand("%:p")))) | silent loadview | endif
autocmd BufWritePost,BufLeave,WinLeave ?* if(!empty(glob(expand("%:p")))) | mkview | endif
It creates errors when I open the command line window via q: (which opens up all the previous commands that I typed):
How can I fix that, and make sure that this error does not occur?
Certain commands are not allowed in the command-line-window; :help E11 explains that.
To deal with this in your :autocmd, there are several options:
suppress any error by prepending :silent! to the :mkview command
surrounding the call with :try ... catch /^E11:/ to just selectively ignore this error, but report anything else
add a check for the command-line window in your :if condition; in recent Vim versions, you can use getcmdwintype(); it returns an empty String in other windows; for a check that also works with older Vim versions, see ingo#compat#window#IsCmdlineWindow() in my ingo-library plugin
Vim used to syntax highlight plain text files(*.txt) as conf files if the first character of the file was a #. But after I updated to 8.0.3, this feature has disappeared.
Is there a way to fix this other than the solution mentioned here? i.e without modifying the file.
function SetConfType()
if !empty(matchstr(getline('1'), '^#\s.*'))
set filetype=conf
endif
endfunction
autocmd BufEnter *.txt call SetConfType()
Update:
This oneliner does not require a function. It is a little bit more elegant.
au BufRead * if getline(1) =~ '^#\s.*' | setlocal ft=javascript.flow | endif
In vim I've written a plugin in order to enable operator coloring,
file: ~/.vim/sources/operatorColoring.vim
if has('autocmd')
autocmd BufRead,VimEnter * syn match parens /[(){}]/ | hi parens ctermfg=082
autocmd BufRead,VimEnter * syn match sqparens /[\[\]]/ | hi sqparens ctermfg=087
autocmd BufRead,VimEnter * syn match operatorsA /[=+%&*^;<>-]/ | hi operatorsA ctermfg=063
autocmd BufRead,VimEnter * syn match operatorsB /[,.;!]/ | hi operatorsB ctermfg=063
autocmd BufRead,VimEnter * syn match operatorsC /[:]/ | hi operatorsC ctermfg=196
endif
I enable this, depending on the file type. For example for python:
file: ~/.vim/ftplugin/python.vim
source ~/.vim/sources/operatorColoring.vim
This principle works perfect, however it breaks when I switch the colorscheme by issuing the command :colorscheme mycolorschme
I believe filetype plugins are loaded automatically when a colorscheme is switched. when I issue the command :filetype the output is:
filetype detection:ON plugin:ON indent:ON
Just in case, I've also tried to issue the command filetype=python, still operator-coloring doesnt work. I've also tried to manually source the operatorColoring script by issuing the command source ~/.vim/sources/operatorColoring.vim, nothing happens when I do this. I'm a bit lost here, hope somebody can help.
I've found the solution for this behaviour, simply typing :e will tell vim to reload all buffer-specific files (more info here), hence in this use case it will reload the operatorColoring script which solves the problem.
Let's say I have the following in my .vimrc:
au bufenter * RainbowParenthesesToggle
However I'm on a unfamiliar machine and I haven't installed all of my plugins yet. This means when I start up Vim I get this error message:
E492: Not an editor command: RainbowParenthesesToggle
How can I guard against this, or what if statement do I want to wrap these calls in to avoid getting this error message when I start Vim?
suppress
The easiest would be to just suppress the error message via :silent! (note the !):
:au bufenter * silent! RainbowParenthesesToggle
check each time
It's cleaner (especially for an autocmd that runs on each BufEnter) to avoid the call. The existence of a command can be checked with exists(':RainbowParenthesesToggle') == 2.
:au bufenter * if exists(':RainbowParenthesesToggle') == 2 | RainbowParenthesesToggle | endif
avoid definition
It would be best to check only once, and avoid defining the autocmd at all. The problem is that your ~/.vimrc is sourced before the plugins! There are two ways around this:
1) Explicitly source the plugin before the check:
runtime! plugin/rainbowparentheses.vim
if exists(':RainbowParenthesesToggle') == 2
au bufenter * RainbowParenthesesToggle
endif
2) Move the definition and conditional to a location that is sourced after the plugins. ~/.vim/after/plugin/rainbowparentheses.vim would be a good place for this.
You can check for the command using exists():
au bufenter * if exists(":RainbowParenthesesToggle") | RainbowParenthesesToggle | endif
(I have no such command defined myself, so I can verify that this works. :) )
I do not want to install another plugin, like pylint.vim,
And today, I decide to use vim edit python instead of pydev which is a eclipse plugin. But I got issues.
I have add this in my vimrc
autocmd BufWritePost *.py !pylint <afile>
but pylint does not contains filename in output
************* Module mymodule
E: 22: invalid syntax
shell return 2
so it can not jump to the line 22 , so I use sed change the output
autocmd BufWritePost *.py !pylint <afile> | sed 's/^\(\w*\):\s*\([0-9]\+\)/<afile>:\2: \1: /g'
it returns:
mymodule.py:22: E: : invalid syntax
but without shell return 2 by vim. so it still can't jump to that line. vim consider it as compile successfully
========================= new comment ===========
Call a function in Vim’s `autocmd` command
I think maybe I should use make command and set makeprg, so I use below config
autocmd FileType python let &makeprg='pylint <afile> | sed s/^\(\w*\):\s*\([0-9]\+\)/<afile>:\2: \1: /g'
autocmd BufWritePost *.py make
when I save, vim returns:
************* Module count
E: 3: invalid syntax
(1 of 2): ************* Module count
Error detected while processing BufWritePost Auto commands for "*.py":
E492: Not an editor command: sed s/^\(\w*\):\s*\([0-9]\+\)/<afile>:\2:
\1: /g
why so complicated with sed which just works properly on Linux? Try the following:
set makeprg=pylint\ --reports=n\ --output-format=parseable\ %:p
set errorformat=%f:%l:\ %m
pylint.vim is old, use syntastic instead:
https://github.com/scrooloose/syntastic
at last I resolve it myself. I'd like share with you guys. 2 lines in vimrc.
autocmd FileType python let &makeprg='pylint %\|sed "s/^\(\w*\):\s*\([0-9]\+\)/%:\2:\ \1:\ /g"'
autocmd BufWritePost *.py make
I would recommend to use A.L.E (Asynchronous Lint Engine) https://github.com/w0rp/ale
It supports a range of python linters and formatters including pylint. Great thing about A.L.E that it supports many other languages.
Nowadays vim ships a compiler file for pylint.
This means that if you have enabled filetype detection (filetype plugin indent on), this is already available without external plugins.
:set makeprg? should show you that pylint is what will be called when issuing :make. If it doesn't, you need to set it as the current compiler with :compiler! pylint.
Now, to have this working, you need to pass some arguments so pylint knows what to lint, most notably what you want to lint, i.e. the filename or directory to link. So to lint the current buffer, run :make %. To lint the current directory, run :make .
That same mecanism could be extended to use flake8 for example, or any linter for any kind of file. See :h :compiler.
autocmd FileType python let &makeprg=‘/usr/local/bin/pylint %’
autocmd BufWritePost *.py make
autocmd FileType python let &makeprg=‘/usr/local/bin/pyflakes %’
autocmd BufWritePost *.py make
you may want to try running epylint instead of just pylint.
epylint (shipped with pylint) is the one that is used within emacs (with flymake). It has a few changes, especially regarding path handling, see the docstring at the start of pylint/epylint.py for more information. It may help you in vim too.
Sidenote: I'm not a vim user for programming myself, but pylint.vim still seems to be a decent option. But I won't question your prerequisites.