Enabling a vimrc setting from the current buffer

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Enabling a vimrc setting from the current buffer

neilhwatson
Greetings,

High level goal: Optionally have vim run the current buffer right after writing
it to disk.

A setting like this does make the post write run work, but on all buffers.

au BufWritePost * !./%

How can I make this work on just the current buffer and how can a quickly
toggle it on or off with less typing?  I did try making a function, but calling
it did nothing:

if exists("b:Autorun")
   au BufWritePost * !./%
endif

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Enabling a vimrc setting from the current buffer

Ben Fritz
On Wednesday, February 1, 2017 at 9:20:18 AM UTC-6, Neil Watson wrote:

> Greetings,
>
> High level goal: Optionally have vim run the current buffer right after writing
> it to disk.
>
> A setting like this does make the post write run work, but on all buffers.
>
> au BufWritePost * !./%
>
> How can I make this work on just the current buffer and how can a quickly
> toggle it on or off with less typing?  I did try making a function, but calling
> it did nothing:
>
> if exists("b:Autorun")
>    au BufWritePost * !./%
> endif
For your attempted method you would just need to move the check to *within* the autocmd:

    au BufWritePost * if exists("b:Autorun") | !./% | endif

A better method would probably be to use a buffer-local autocmd (:help autocmd-buffer-local):

    au BufWritePost <buffer> !./%

and then get rid of it:

    au! BufWritePost <buffer>

Map these to a keypress or a user-defined command and you're ready to go.

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Enabling a vimrc setting from the current buffer

neilhwatson
On Wednesday, February 1, 2017 at 10:32:54 AM UTC-5, Ben Fritz wrote:
> A better method would probably be to use a buffer-local autocmd (:help autocmd-buffer-local):
>
>     au BufWritePost <buffer> !./%
>
> and then get rid of it:
>
>     au! BufWritePost <buffer>
>
> Map these to a keypress or a user-defined command and you're ready to go.

This works. Thank you.

command Autorun   au BufWritePost <buffer> !./%
command Noautorun au! BufWritePost <buffer>


--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.