Set autocmd for every time I change file - vim

How can I set up a autcmd command for every time I switch to another file?
I want to call to a Rooter command (a plugin for setting the root directory).
What I've tried:
au BufEnter * Rooter
But it's not working always, sometimes when I select a file from MRU for instance.

How can I set up a autcmd command for every time I switch to another file?
From your description, I think you need event BufLeave ?
if your Rooter() is a function,
au BufLeave * call Rooter()

What you are after it is already implemented in the plugin itself:
see: vim-rooter's source code
So no need to implement it yourself. I think you should make plugin, buffer local! so replace all occurances of g:loaded_rooter with b:loaded_rooter in source code. then save, close vim, open and test.

Related

In Vim, how to I set an autocommand to be run after a plugin has loaded?

One of the Vim Plugins I use has a bug, causing it to set :syntax spell notoplevel. The bug is easily mitigated if I run the command :syntax spell toplevel after opening a file. However, I'm lazy and I'd like to put the fix in my init.vim / .vimrc file, so that it's run automatically.
How can I ensure that my fix is executed after the buggy plugin code, so that my setting is not overridden by the plugin?
Create a file in ~/.vim/after/plugin/ such as ~/.vim/after/plugin/fix-spell.vim containing the command you'd run without the colon:
syntax spell toplevel
The files in ~/.vim/after/plugin are sourced after your plugins have loaded, thus providing a convenient hook to change settings that might have been set by a plugin.
Alternatively, you can set it as an autocommand. There are a whole slew of events you can tie autocommands to (:help events for all of them). VimEnter is an event that fires after plugins are loaded, so you could set your command to run then with a line like this right in your vimrc:
autocmd VimEnter * syntax spell toplevel
That's what I am using to apply a plugin theme that is not available until after plugins load.

is it possible to set BufLeave autocmd in vim on the fly?

What I want to do:
Inside a file/buffer in vim, especially an unnamed one (for example, created with :enew), sometimes I feel the content I'm writing is worth saving (say, I suddenly want to send it via an email), but don't feel the particular need to save it to a temp file, nor do I trust myself enough to "remember" to save it upon exit.
So I thought, what if I run
autocmd BufLeave * ggvG"+y
in the vim command line once and be free from the fear of losing this content.
But it doesn't work. When I exit vim, the system clipboard reminds intact.
My questions:
does it do anything if we run autocmd on the fly, as opposed to in vimrc?
is there a way to tell vim to "hey, when you exit, run these"?
Thanks a ton!
Two problems:
1) You didn't leave the buffer to go to another buffer (BufLeave); you left Vim (VimLeave).
2) autocmd expects a command, not normal mode keystrokes.
With that in mind,
autocmd VimLeave * normal gg"+yG

How to tell when a vim script is being sourced

How can I tell if my script is being sourced on or after startup?
For example, I want to write a function that responds to the ttymouse setting, say CheckMouseSetting(). This setting, however, is oddly enough loaded after startup scripts are loaded -- I'm not sure why. I could use a VimEnter autocommand, but this won't activate if the user is simply sourcing this file after startup. I could have both, ie:
call CheckMouseSetting()
au * VimEnter call CheckMouseSetting()
But this is not ideal as it may produce unwanted error messages -- so, hence my question.
I would solve this with a guard variable, like the multiple inclusion guard at the start of plugin files.
function! myplugin#CheckMouseSetting()
if exists('s:hasBeenChecked')
return
endif
let s:hasBeenChecked = 1
...
If you define this function in an autoload script, you can invoke it from the VimEnter event, and alternatively instruct users who want to have this manually to call the function, which is easier than :source / :runtime, because it avoids the path issues.
Clarification: you want the function to know whether it is being sourced at startup or later, right?
I think you have a little blind spot: the simplest solution is to tell the function:
:call CheckMouseSetting('myscript')
:au * VimEnter call CheckMouseSetting('VimEnter')
In response you your comment: if you want to tell when the file is sourced (during startup or interactively) then you can add a script-local variable:
:let s:source_count = exists('s:source_count') ? s:source_count + 1 : 1
:call CheckMouseSetting('myscript', s:source_count)
If you really want a more "automatic" way to tell, then #romainl's comment is on target. (I think there was a time when I was the only one besides Bram who had ever read that section of the help.)

tail like functionality for gvim

I want to use gvim to view a log file which is being updated continuously, such that I always see the last updated line, much like tail command in unix. Is it possible?
Open logfile and
:setlocal autoread
There is a plugin (Tail Bundle) on the vim site.
I like it short and without a lot of hacking or external scripts.
You can run this oneliner from ex (whithin vim) when needed (or put each command in vimrc, for when log-files are opened.)
:set autoread | au CursorHold * checktime | call feedkeys("lh")
and additionally you can :set syntax=logtalk to color the log
(if you would want to jump (nearly) to the end of the file, just use "G" instead of "lh" with feedkeys)
Explanation:
autoread: reads the file when changed from the outside (but it doesnt work on its own, there is no internal timer or something like that. It will only read the file when vim does an action, like a command in ex :!
CursorHold * checktime: when the cursor isn't moved by the user for the time specified in updatetime (which is 4000 miliseconds by default) checktime is executed, which checks for changes from outside the file
call feedkeys("lh"): the cursor is moved once, right and back left. and then nothing happens (... which means, that CursorHold is triggered, which means we have a loop)
To stop the scrolling when using call feedkeys("G"), execute :set noautoread - now vim will tell, that the file was change ans ask if one wants to read the changes or not)
*from this answer (refering to an answer by PhanHaiQuang and a comment by flukus)
Or a less elegant solution on a vim 7.x would be just do :e! whenever you need the update .

Using vim Sessions Only With GUI?

My usage-scenario may seem a bit unusual, but here it is: When using vim (it's one of about 4 different editors I use regularly), I use it in two different situations. The first is via the GUI, in which I'll have multiple buffers and have some settings different than when I use it from the command-line (by testing "if has('gui_running')"). The other is when I need to do something short-and-quick, from the command-line, such as make a small change to a dot-file or other type of config.
What I would like to do, is have sessions enabled for the GUI, but have any command-line invocations ignore them. That is, I don't want to bring up the full existing session on a CL invocation, nor do I want it (and whatever buffer/file it involved) to alter the session that the GUI is using. As I'm fairly new to the post-vi-functionality of vim, I'm not really sure how to pull this off.
do your session magic in your .gvimrc and everything else in your .vimrc. The GUI will source both, but the CL version will only source the .vimrc.
The session magic is to set up autocommands to write your session to a file on exit, and reload it by sourcing the file upon entrance.
au VimLeave * mksession ~/.gvimsession
au VimEnter * source ~/.gvimsession
You may want to add a ! to mksession so that you won't get an override error message upon exiting everytime.
au VimLeave * mksession! ~/.gvimsession
au VimEnter * source ~/.gvimsession

Resources