I have these in my vimrc:
if has("autocmd")
augroup Misc
" Jump to the last known position when reopening a file
autocmd BufReadPost *
\ if line("'\"") > 1 && line("'\"") <= line("$") |
\ exe "normal! g`\"" |
\ endif
augroup END
augroup FTOptions
autocmd!
autocmd FileType cpp,java setlocal commentstring=//\ %s
autocmd FileType markdown setlocal textwidth=78 formatprg=par\ -w78
augroup END
In markdown files I'd like to do the formatting using par. It works well, but I get a warning if "par" is not installed on the system. I'm wondering if there's a way to check and only set "formatprg=par\ -78" when par is installed, otherwise use VIM's default formatting.
I would be grateful for any suggestion.
if executable('par')
" stuff
endif
Related
I'm writing a small snippet for quickfix buffers. I need to add an autocmd for quickfix buffers for BufDelete event.
I have following in ~/.vim/ftplugin/qf.vim
augroup quickr_preview
autocmd!
autocmd BufDelete <buffer> echom "Hey"
augroup END
The autocmd is getting executed before the quickfix buffer is loaded. So the BufDelete autocmd gets set for the calling buffer and not the quickfix buffer.
I've also tried putting autocmd FileType qf autocmd BufDelete <buffer> echom "Hey" directly in my ~/.vimrc, but that has same effect.
How to go about this?
For now I'm going with following.
function! QuickFixBufDelete()
if &buftype == 'quickfix'
autocmd BufDelete <buffer> echom "Hey"
endif
endfunction
autocmd BufCreate * call QuickFixBufDelete()
This is not good as the autocmd gets invoked for all filetypes and then I check for the filetype in my function.
The current answer is no longer working for me. I am now trying the BufReadPost auto command, and it seems to be playing nicely ...
augroup quickr_preview_auto_cmds
autocmd!
autocmd BufReadPost quickfix
\ if !exists('b:quickr_preview_auto_cmds')
\ | exe 'autocmd BufDelete <buffer> pclose! | sign unplace 26'
\ | let b:quickr_preview_auto_cmds = 1
\ | endif
augroup END
This matches what is described in the vim help for qiuckfix-window ...
When the quickfix window has been filled, two autocommand events are
triggered. First the 'filetype' option is set to "qf", which triggers the
FileType event. Then the BufReadPost event is triggered, using "quickfix" for
the buffer name. This can be used to perform some action on the listed
errors.
I've got vim setup to jump to the last known cursor position when opening a file, but sometimes that position is in the middle of some javascript or css on a html page, and the syntax highlighting doesn't catch that. I can manually run
:syntax sync minlines=200
to fix the highlighting, but I'd like vim to run that for me whenever I open a file.
I tried to do this in my vimrc, but it doesn't fix the syntax highlighting when I open a file.
" Jump to last known cursor porition
autocmd BufReadPost *
\ if line("'\"") > 1 && line("'\"") <= line("$") |
\ exe "normal! g`\"" |
\ endif
" Sync syntax highlighting
autocmd BufReadPost * syntax sync minlines=200
I close and reopen vim, so it should be getting the new .vimrc settings.
Not sure what I'm missing here. Thanks.
Ok, I got it using the BufWinEnter event instead of BufReadPost
" Jump to last known cursor porition
autocmd BufReadPost *
\ if line("'\"") > 1 && line("'\"") <= line("$") |
\ exe "normal! g`\"" |
\ endif
" Sync syntax highlighting
autocmd BufWinEnter * syntax sync minlines=200
I put together the following pile of awesomeness:
if !exists("g:AsciidocAutoSave")
let g:AsciidocAutoSave = 0
endif
fun! AsciidocRefresh()
if g:AsciidocAutoSave == 1
execute 'write'
execute 'silent !asciidoc -b html5 -a icons "%"'
endif
endf
fun! AsciidocFocusLost()
augroup asciidocFocusLost
autocmd FocusLost <buffer> call AsciidocRefresh()
augroup END
endfun
augroup asciidocFileType
autocmd FileType asciidoc :call AsciidocFocusLost()
augroup END
The only problem is: It saves twice every time vim loses focus when in an asciidoc file.
When I put :autocmd asciidocFileType into the command line, it shows:
---Auto-Commands---
asciidocFileType FileType
asciidoc :call AsciidocFocusLost()
:call AsciidocFocusLost()
The same with :autocmd asciidocFocusLost and AsciidocRefresh().
Why this duplication?
I can't tell you for sure why you got those duplicates (multiple sourcing?), but the canonical way to prevent this is clearing the augroup first:
augroup asciidocFileType
autocmd!
autocmd FileType asciidoc :call AsciidocFocusLost()
augroup END
Since you only have one definition, you can also do this in one command:
augroup asciidocFileType
autocmd! FileType asciidoc :call AsciidocFocusLost()
augroup END
#ingo-karkat's answer is great, but I think I know why you get twice as many autocmds and how to prevent it naturally:
Some distributions of vim / Neovim make sure by default that filetype plugin indent is on. Test it for neovim with env XDG_CONFIG_HOME=/dev/null nvim -c 'filetype' or for vim with env HOME=/dev/null vim -c filetype
If the output is
filetype detection:ON plugin:ON indent:ON
Then You don't need to add filetype * on to your vimrc.
I`m using following VIM settings for folding:
highlight Folded guibg=black guifg=#524A4D
set foldmethod=syntax
I`m also using Powerline plugin:
Bundle 'Lokaltog/vim-powerline'
I would like to save my foldings, so i found this two lines of code:
au BufWinLeave * silent! mkview
au BufWinEnter * silent! loadview
But after that my powerline status vanish after saving.
How to compare this three things.
I created screencast to show exactly whats going on:
http://screencast.com/t/ZnXTxdAVUZse
I think it is conflict between Powerline and mkview. So is then a chance to save foldings in other way??
The issue you have created on Github is likely to be more useful than this question here but I think your issue could be related to this other one.
You should remove the highlight Folded guibg=black guifg=#524A4D from your vimrc and delete the session files.
I found a answer:
set viewoptions-=options
augroup vimrc
autocmd BufWritePost *
\ if expand('%') != '' && &buftype !~ 'nofile'
\| mkview
\| endif
autocmd BufRead *
\ if expand('%') != '' && &buftype !~ 'nofile'
\| silent loadview
\| endif
augroup END
After literally days of tweaking, I think I got vim's autocomplete (omnicomplete + Acp plugin) to work acceptably. But the annoying thing is that it is still enabled for plain text files so I would have to type :DisableAcp for README or plain text files. How do I add logic to my vimrc so that if it detects an empty filetype it disables the Acp on startup?
here is what I wrote:
if &filetype == ""
let g:acp_enableStartup = 0
endif
but this disables Acp on start up for all files.
Add this to your vimrc:
autocmd WinEnter README,*.txt :DisableAcp
autocmd WinLeave README,*.txt :EnableAcp
Update:
autocmd WinEnter * :if &ft=='text' | DisableAcp | else | EnableAcp | endif
The function/method is :AcpDisable, not :DisableAcp
I decided to change the logic after not knowing why vim doesn't honor my
autocmd Filetype * | if &omnifunc=="" | call acp#disable() | endif
so I did this:
let g:acp_EnableAtStartup = 0
autocmd FileType python call acp#enable()
autocmd FileType ncl call acp#enable()
autocmd FileType javascript call acp#enable()
autocmd FileType html call acp#enable()
autocmd FileType css call acp#enable()
autocmd FileType xml call acp#enable()
autocmd FileType php call acp#enable()
autocmd FileType c call acp#enable()
and this works.
Thanks for your help.