how to disable vim autocomplete popup for plain text files? - vim

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.

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.

VIM: Open netrw on startup or open files provided

I've added a little quality-of-life improvement to the .vimrc which invokes netrw # startup, namely:
augroup ProjectDrawer
autocmd!
autocmd VimEnter * :Explore!
augroup END
Works like a charm, however this interferes with invoking vim to edit a particular file vim file_foo (I end up with netrw not with file_foo).
How can I modify my .vimrc to e.g. invoke ProjectDrawer when there were no arguments to the vim on invocation (vim), otherwise open provided files (vim file_foo)?
You can add a conditional that checks argc(), which gives the number of arguments passed to Vim (the arguments itself are returned by argv({nr})):
augroup ProjectDrawer
autocmd!
autocmd VimEnter * if argc() == 0 | Explore! | endif
augroup END

Set BufDelete autocmd for particular filetype

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.

autocmd function always executed twice

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.

Can't use vim-perl unless I stop use "filetype indent on"

I find the vim-perl plugin here, but I can't enable it with:
filetype plugin indent on (I changed the order of "plugin" and "indent" after seeing that link)
I can't disable plugin support, so I tried an alternative way,
if &filetype == 'perl'
filetype plugin on
else
filetype plugin indent on
endif
But that doesn't work either! When I do filetype command in VIM, I see plugin, autodetection, indent are all ON
Any thoughts?
But that doesn't work either! When I do filetype command in VIM, I see plugin, autodetection, indent are all ON
Of course, it would not. :filetype also enables filetype detection, thus there is no way you can automatically have &filetype equal to perl until you have run :filetype on.
You can try something like
function s:DisablePerlIndent()
augroup DisablePerlIndent
if &ft is# 'perl'
autocmd! BufEnter <buffer> filetype indent off
else
autocmd! BufEnter <buffer>
endif
augroup END
endfunction
augorup DisablePerlIndent
autocmd! FileType * call s:DisablePerlIndent()
augroup END
, but I doubt this will actually work. It would be much better if you follow #IngoKarkat advice and say what does “can’t enable” mean.

Resources