How to run autocmd in vim with double extension? - vim

How to make autocmd work for double extensions in vim?
This command works for .scss files
:autocmd BufWritePre *.css echo "hello css!"
But this doesn't work for blade.php
:autocmd BufWritePre *.blade.php echo "hello blade!"

Related

Vim autocmd for FileType not working indirectly

I have a custom ~/.vimrc I made with this augroup:
augroup filetype_vim
autocmd!
autocmd! BufWritePost .vimrc* source %
autocmd FileType vim |
setlocal foldlevel=0 foldmethod=marker foldmarker={{{,}}}
augroup END
When I open vim directly to edit the ~/.vimrc like this: vim ~/.vimrc, the folding works as expected, I can fold {{{ marker:
:set foldmethod?
> foldmethod=marker
When I open vim without specifying a file: vim, and then trying to edit: :e ~/.vimrc, the foldmethod is different!
:set foldmethod?
> foldmethod=syntax
Which obviously comes from a different part of my vimrc.
Why doesn't it recognizes the file type when I open the file indirectly?
You've failed with VimScript syntax. Must be
autocmd FileType vim
\ setlocal foldlevel=0 foldmethod=marker foldmarker={{{,}}}
What you did instead is
autocmd FileType vim <nothing> | <nothing>
setlocal foo bar
Therefore setlocal applies to the current buffer only (i.e. command-line argument), not to anything else.

Highlight txt files but not help files

I would like to apply the following autocmd:
autocmd BufNewFile,BufRead *.txt set filetype=markdown
autocmd BufNewFile,BufRead *.txt colorscheme OceanicNext
However, I do not want this to apply to any vim help files that are loaded whenever I do :h <something>. Is there a way to exclude that from the autocmd?
Use FileType event whenever possible.
Use setfiletype to set filetype if it wasn't already set.
~/.vim/ftdetect/markdown.vim
autocmd BufNewFile,BufRead *.txt setf FALLBACK markdown
~/.vim/after/ftplugin/markdown.vim
setlocal foo bar

How to ban python's tags completion when to edit php file?

Here is some completion config in my .vimrc for python , php.
au FileType python call pyFun()
function pyFun()
set tags+=/home/debian8/.vim/python.tags
autocmd FileType python set omnifunc=pythoncomplete#Complete
endfunction
au FileType php call phpFun()
function phpFun()
autocmd FileType php set omnifunc=phpcomplete#Complete
endfunction
python.tags was created by ctags.
ctags -R --fields=+l --languages=python --python-kinds=-iv -o /tmp/python3.tag anaconda3/lib/python3.6
Now to edit a file test.php.
To call tags completion with ctrlxctrl] after chracters fil,words menu pop up.
There is no function such as fileConfig and fileDescriptorReceived in php.
grep 'fileConfig' /home/debian8/.vim/tag/python.tag
fileConfig anaconda3/lib/python3.6/logging/config.py /^def fileConfig(fname, defaults=None, disable_existing_loggers=True):$/;" f
grep 'fileDescriptorReceived' /home/debian8/.vim/tag/python.tag
fileDescriptorReceived anaconda3/lib/python3.6/site-packages/twisted/internet/endpoints.py /^ def fileDescriptorReceived(self, descriptor):$/;" m class:_WrappingProtocol
All of them is not i wanted.
How to ban vim to call python's tag when to edit php file,in other words ,
all of them can't be displayed when to press ctrlxctrl] after chracters fil?
Create or edit files ~/.vim/ftplugin/python.vim and ~/.vim/ftplugin/php.vim.
In ~/.vim/ftplugin/python.vim add:
setlocal tags+=/home/debian8/.vim/python.tags
setlocal omnifunc=pythoncomplete#Complete
In ~/.vim/ftplugin/php.vim:
setlocal omnifunc=phpcomplete#Complete

vim autocmd silent execution

I have my .vimrc file configured in such a way that each time I save a Python file or enter a python buffer (also for Matlab and Latex but it is all the same) it silently executes exuberant ctags:
set tags+=./tags
autocmd BufWritePost *.py silent !ctags *.py
autocmd BufEnter *.py silent !ctags *.py
autocmd BufWritePost *.m silent !ctags *.m
autocmd BufEnter *.m silent !ctags *.m
autocmd BufWritePost *.tex silent !ctags *.tex
autocmd BufEnter *.tex silent !ctags *.tex
I then sometimes use this information to navigate my files. This works nicely in Mac OS X and Linux. The last month or so, I've had to use Windows 7. When I add these lines to my .vimrc it flashes a cmd prompt in front of me and it is very obnoxious. I also tried !start instead of silent, and that brings a host of other issues, chief among them having to press enter each time after :w.
What am I missing? Is it possible to duplicate my OS X configuration, where the command gets executed, the tags file gets updated, everything gets reloaded without flashing the cmd prompt and without further issues?
You may want to check
autocmd BufEnter *.tex silent! !start /min ctags *.tex
Alternatively, try /b /wait.
See also https://serverfault.com/questions/31608/can-i-run-a-gui-program-in-the-background-on-the-windows-command-line/31621#31621
You may also check the shell.vim plugin:
enables execution of external commands in the background without opening a
command prompt window on Windows.
It also provides some interesting additional features.

Vim: Saving on FocusLost and executing auto command

I've setup my vim editor (I use MacVim) to save files automatically when the focus is lost:
autocmd FocusLost * silent! wall
I also automatically strip trailing whitespace from python files using this auto command:
autocmd BufWritePre *.py :%s/\s\+$//e
This auto command works perfectly when I save the file manually (either by typing :w or by pressing ⌘s) but it is not executed (i.e. the whitespace is not stripped) when I switch to another application and the buffer is automatically written.
How can I modify these auto commands to make them work together?
You need to change your FocusLost autocommand to:
autocmd FocusLost * nested silent! wall
See :h autocmd-nested for details.
I can't test this in a graphic Vim, but you can try some options:
Join some events in the same autocommand autocmd BufWritePre,FocusLost *.py ...
Execute an autocommand from an event, something like:
autocmd BufWritePre *.py :execute "%s/\s\+$//e" | doautocmd FocusLost %

Resources