I run vim with no file on the command-line, so that it shows the scratch buffer. When I then run
:e foo
to edit file 'foo', there is a BufUnload event triggered for file 'foo'. Here is a test vim command that I use to demonstrate the issue:
autocmd BufUnload * echom "Unloaded " . expand("<afile>")
Does anyone know what is causing this, and if there is a way to detect this situation and ignore it?
I can reproduce this with the latest Vim 7.4a.39. I guess this has to do with the fact that Vim reuses the initial scratch buffer number (1) for the first :edited file. Please report this problem to the vim_dev mailing list.
Related
I run a command every time a save a file which, among other things, lints the current file. Vim does two annoying things which I wanted to avoid:
It asks me to press enter after the command was executed.
It notifies me that the current file has been modified outside of Vim.
How to get rid of this? I just want this to behave unobtrusively as ALE linters. This is my code:
autocmd BufWritePost *.prisma :execute '!npx prisma format'
#romainl's comment gave me an idea on how to do it:
autocmd BufWritePre *.prisma silent write | silent :execute '!npx prisma format' | edit! %
It's not the cleanest way to do it but it works. silent hides the command output and edit! forces Vim to open the modified file.
I am recently moving from sublime 3 go to mvim (vim on the mac os) and am trying to get my Golang development environment to be as similar on vim to my sublime implementation as possible.
Within my sublime setup, it runs go build anytime I save the a Go file. This provides me instant feedback on if I have unused vars or other info go build provides.
I'm attempting to move to vim, and am wondering if I can have this functionality implemented there as well. I am using vim-go but have not found a setting to implement this.
In short I want to run :GoBuild upon the saving of a Go file while using vim / vim-go. Is this Possible and how do I do so?
yes, use vim autocommand to run :GoBuild:
You can specify commands to be executed automatically when reading or
writing a file, when entering or leaving a buffer or window, and when
exiting Vim. The usual place to put autocommands is in your .vimrc or
.exrc file.
Run the following command:
:autocmd BufWritePre *.go :GoBuild
Now each time you save your Go file with :w it will run :GoBuild too.
The event type is BufWritePre, which means the event will be checked just before you write *.go file.
BufWritePre starting to write the whole buffer to a file
BufWritePost after writing the whole buffer to a file
and:
When your .vimrc file is sourced twice, the autocommands will appear
twice. To avoid this, put this command in your .vimrc file, before
defining autocommands:
:autocmd! " Remove ALL autocommands for the current group.
If you don't want to remove all autocommands, you can instead use a
variable to ensure that Vim includes the autocommands only once:
:if !exists("autocommands_loaded")
: let autocommands_loaded = 1
: au ...
:endif
like this (put this at the end of your vim startup file):
:if !exists("autocommands_loaded")
: let autocommands_loaded = 1
: autocmd BufWritePost *.go :GoBuild
:endif
ref:
http://vimdoc.sourceforge.net/htmldoc/autocmd.html
http://learnvimscriptthehardway.stevelosh.com/chapters/12.html
Create ~/.vim/after/ftplugin/go.vim with:
autocmd BufWritePre *.go :GoBuild
This is in reference to the accepted answer here written by Paul Biggar:
After considering all these options for some time, I have settled with the following solution.
Set vim to write continuously as I type.
Run a script in the background to build continuously, refreshing the pdf as it goes. latexmk is nearly good enough, except that it builds in place, which gets reloaded at a bad time in okular (my viewer).
The script is available at https://github.com/pbiggar/texbuild.
Use rubber-info to get the errors and warnings from the log file. The script above saves the log file in t.log. In vim:
autocmd FileType tex set makeprg=rubber-info\ t.log
autocmd FileType tex set errorformat=%f:%l:\ %m
I want to execute his github script in order to "build [LaTeX files] continuously, refreshing the pdf as it goes." Given that he doesn't exactly offer instructions how to run it, I assume this is a rather trivial procedure. However, I'm quite new at vim and know very little about programming/github in general, so I really have no clue how to start.
I've searched high and low, and have made zero progress. I'm not sure if the code is supposed to be compiled in python, then run as a script, or anything similar. I appreciate your help!
EDIT: As per the comments discussion, I've successfully made the script executable. But I still don't know how to run it..?
The
#!/usr/bin/env python
at the top of the script states, as well as absence of import vim in the body clearly states that it should be run from the shell (or from vim) using
chmod +x /path/to/texbuild # Needed only once
/path/to/texbuild {script-args}
. Further exploration reveals that it expects to have one tex file as its first and only argument (the only place where sys.argv is mentioned is line 48) hence it is likely that you should use the following to run it:
augroup AutorunTexbuild
autocmd!
autocmd FileType tex :if !exists('b:runtexbuild') | call system('/path/to/texbuild '.shellescape(#%)) | let b:runtexbuild=1 | endif
autocmd VimLeave * :call system('killall -TERM texbuild')
augroup END
. That assumes that you edit tex files only in one vim instance at a time. It looks like author expected you to run that script manually.
According to the answer of this question, the :bd command should not quit Vim (gVim) when it is the last buffer remaining. Unfortunately, it does close gVim in my case. Did I understand something wrong about :bd?
I am also using a preconfigured vimrc file. Maybe a setting in there has that side affect, but I couldn’t find it.
Try doing the following:
:set eventignore=all | bd | set eventignore=
If this won't quit vim then you have some plugin that defines an autocommand that quits vim when no more buffers are present in the list, so after that try doing
verbose autocmd BufWinLeave,BufLeave,BufDelete,BufUnload,BufWipeout
This will show you all autocommands attached to given events (these are events that are executed when buffer is deleted) and where they were defined. Note that I do not have any autocommands attached to these events that are defined by plugins in standart vim distribution.
Update: I do not see anything bad in your output. You may also try
verbose autocmd BufNew,BufAdd,BufCreate,BufEnter,BufWinEnter
(because when you leave last buffer new empty one is created). If this does not show anything suspicious, start ignoring event types: if you are on linux try the following script:
for event in BufWinLeave BufLeave BufDelete BufUnload BufWipeout BufNew BufAdd BufCreate BufEnter BufWinEnter
do
event=$event vim file -c "set eventignore=$event | bd"
done
This script should iterate until you find event name that causes trouble. After this you can use execute "verbose autocmd" $event in vim to narrow number of plugins that should be checked. After you got list of autocmd groups (augroup names are shown just before event name in your output: railsPluginDetect is one of such groups), delete events in them (augroup {GroupName} | execute 'autocmd!' | augroup END) and find out which plugin to claim.
Alternatively, you can use debugger:
debug bd
, then s<CR>n<CR><CR><CR>... until vim quits; don't forget to remember what vim have shown above > prompt before quiting.
I have the following command run as a vim plugin under ~/.vim/plugin/autohighlight.vim
:autocmd CursorMoved * exe printf('match IncSearch /\<%s\>/', expand('<cword>'))
The thing is, it throws a bunch of errors anywhere except when I'm editing a file. (file Explorer, other windows)
Is there a way to tell the script to only take effect when a file is edited and not anywhere else in VIM?
You can try this one:
autocmd CursorMoved * :if filewritable(#%)==1 |
\ call matchadd('IncSearch', '\V\<'.escape(expand('<cword>').'\>', '\'), 10, 1) |
\endif
It will do highlighting only if current buffer name is a writable file (not directory). It will fail if you are editing something which uses BufWriteCmd to perform saving (for example, if you are editing file inside a zip archive).
By the way, can you provide these errors? I was able to get an error when I used :e ., but it was not related to the fact that you are observing a directory, it appeared just because you forgot to do escaping. If you have written escape(expand('<cword>'), '\/') instead of expand('<cword>') then such error would not appear.