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.
Related
I use Vim-Airline plug in Vim, but statuline incomplete display after source .vimrc, so I want use :redraw! in augroup but it not take effect.
augroup Vim
autocmd!
autocmd BufWritePost $MYVIMRC ++nested source $MYVIMRC ":redraw!"
augroup End
I can only exec :redraw! in command line.
How to improve my code?
I am not sure where those quotes around :redraw! come from or what you expect them to do since " introduces a comment.
In Vim, you use a "bar", |, to separate Ex commands like :source and :redraw:
augroup Vim
autocmd!
autocmd BufWritePost $MYVIMRC ++nested source $MYVIMRC | redraw!
augroup END
See :help :bar.
I share lab server with my colleagues, but want my separate .vimrc file. How to get that?
$ cat .vimrc
color desert
$ pwd
/fvs101/home
We have our separate working directory, so my directory is inside
/fvs101/home/sp
At /fvs101/home there is common .vimrc file.
I want to play with vim but do not want to touch this /fvs101/home/.vimrc file.
I want to create my own .vimrc file.
++++++++++my vimrc file
[sp]$ cat .vimrc
noremap - ddp
noremap _ dd2kp
inoremap <c-u> <esc>lviwU<esc>i
nnoremap <c-u> <esc>viwU<esc>
let mapleader = "-"
let maplocalleader = "\\"
nnoremap <leader>ev :vsplit $MYVIMRC<cr>
nnoremap <leader>sv :source $MYVIMRC<cr>
iabbrev ssig -- <cr>Sachin Pawar<cr>sachin.sp.pawar#oracle.com
vnoremap <leader>' vi<esc>`<<esc>i'<esc>`><esc>i'<esc>
nnoremap H 0
nnoremap L $
inoremap jk <esc>
inoremap <esc> <nop>
augroup filetype_js
autocmd!
autocmd FileType javascript nnoremap <buffer> <localleader>c I//<esc>
augroup END
augroup filetype_python
autocmd!
autocmd FileType python nnoremap <buffer> <localleader>c I#<esc>
augroup END
augroup filetype_sql
autocmd!
autocmd FileType sql nnoremap <buffer> <localleader>c I--esc>
augroup END
augroup filetype_shell
autocmd!
autocmd FileType shell nnoremap <buffer> <localleader>c I#esc>
augroup END
augroup filetype_html
autocmd!
autocmd FileType html nnoremap <buffer> <localleader>f Vatzf
augroup END
++++++++++
If i use my separate .vimrc file i am not able to see the mapping for some mappings like
inoremap jk
Why is this happening and how to avoid it.
In order to use my own .vimrc file i have create a separate alias.
alias vi='vim -u /fvs101/home/sp/.vimrc'
Make a different .vimrc (as you have stated in the comments, /refresh/oracle/spawar/.vimrc) and make an alias for your own user
myvi='vim -Nu /refresh/oracle/spawar/.vimrc'
Note that without the -N flag, using the -u flag will start vim in compatibility mode causing all sorts of nasty, unexpected side-effects such as mappings not working entirely.
I found this out by going to see what the -u flag really does and following from there.
Helpful links below.
u flag
N flag
Seeing as you specifically want everyone to log into the same user, the easiest way I can think of how to do this is if you make your own .vimrc (give it a short name, such as ~/.vrc) and every time you open vim, you type
:so ~/.vrc
which will load that vimrc for you. Every time you open vim it will use the defaults, until you load your own vimrc, so you will have to do this each and every time. Also remember that this way, ~/.vimrc loads automatically before you manually load ~/.vrc
You can call Vim with a different runtime configuration file than the default one with
vim -N -u path/to/your/vimrc
If you want to use this always, edit your users shell runtime configuration to define an alias for that.
alias vim="vim -N -u path/to/your/vimrc"
I think the simplest solution would be to append a single line to the end of the default vimrc which reads:
source ~/.vimrc.local " Or whatever you want to call it
Where .vimrc.local contains your custom configuration.
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
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 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.