Thursday, February 25, 2016

Re: How to write autocmd command to set local variable to some value when new buffer is created?

On Thursday, February 25, 2016 at 6:12:48 AM UTC-6, Igor Forca wrote:
>
> According to FAQ https://github.com/tpope/vim-commentary commentary string can be changed using:
> autocmd FileType apache setlocal commentstring=#\ %s
>
> I have changed above command to my needs:
> autocmd FileType sql setlocal commentstring=--\ %s
>
> Now when I save a buffer with:
> :w test.sql
> and then execute for example gcc to comment out a line, scripts works fine it adds -- at the beginning of line.
>
> So far everything is OK, but now I have the following new wish:
> I would ALSO like to enable comment string "--" when new buffer is created (but obviously not saved yet, if it was above autocmd would be fired).
>

One option:

Instead of opening a new file with just ":new" or ":enew", give it a file name right when you start editing, to trigger filetype detection:

:new test.sql
:e test.sql

The BufNew autocmd you're using should also work. In fact, it works fine for me.

You can use ":verbose set commentstring?" to find what it is set to, and what script is responsible, when it is set to something you did not expect.

--
--
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 vim_use+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments: