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

564 views
Skip to first unread message

Igor Forca

unread,
Feb 25, 2016, 7:12:48 AM2/25/16
to vim_use
Hi,
I have gVim 7.4 on Windows 7 working perfectly.

I write a lot of SQLs code and I need some easy way to comment portion of my code and remove comment after I debug the code.

I have found outstanding superb Vim commentary.vim plugin that actually adds new operator "gc". Using this new operator I can combine it with motion, so operator+motion and I can use for example:
gcc to comment out current line
gcG to comment out from current line to end of buffer
gcgg to comment out from current line to start of buffer
gcap to comment out a paragraph
V{some movement}gc to comment out selected lines
gc3j comment out current line and additional three lines bellow
Note: I was telling you superb idea to use operator-pending mode to get a new operator.


I did the following:
1. Downloaded file https://github.com/tpope/vim-commentary/blob/master/plugin/commentary.vim to some location on my local disk.
2. Started gVim and sourced the file with: source commentary.vim
3. To comment out a line just use: gcc

Script works perfectly.

The script default comment string is:
*/ some text */
but I need:
-- some text

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).

I have read the following excellent tutorial: http://learnvimscriptthehardway.stevelosh.com/chapters/12.html and I tried to use the following command:
:autocmd BufNew * setlocal commentstring=--\ %s

Then opened a new buffer:
:enew
and try to comment out a line:
gcc
and line is commented out with /* some text */ which is default comment string, but I expected to get: -- some text

If I check the local variable:
:setlocal commentstring?
and the output is:
commentstring=/*%s*/
but I expected to be:
commentstring=--\ %s

It looks like my autocmd command was no fired or something override my autocmd.

If I manually set the string to
setlocal commentstring=--\ %s
and then command
gcc
works fine it comments out a line with prefixing it with:
-- some text

Now my question. How to set local variable to:
setlocal commentstring=--\ %s
when new buffer is created?

Thanks

Ben Fritz

unread,
Feb 25, 2016, 10:27:07 AM2/25/16
to vim_use
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.

Igor Forca

unread,
Feb 26, 2016, 2:19:35 AM2/26/16
to vim_use
@Ben, thank you for your kind reply.

I tried to do the following:
1. In my $MYVIMRC file I put only:
:set nocompatible
2. In my $MYGVIMRC I have deleted all content, so there is empty file.
3. Started gVIM.
4. Typed in command:
:source commentary.vim


:autocmd BufNew * setlocal commentstring=--\ %s

5. New buffer:
:enew!
6. Typed in some text.
7. Comment out a line:
gcc
And it works fine. Comment string at front of line is: --


Then I did the following:
1. In my $MYVIMRC file I put only:
:set nocompatible
:source commentary.vim


:autocmd BufNew * setlocal commentstring=--\ %s

2. $MYGVIMRC I have put empty as before.
3. Closed gVim and strated it again to make sure $MYVIMRC file was read.
4. New file:
:enew!
5. Typed in some text.
6. comment out a line:
gcc
And it does not work as expected. It sets: /* text */ commemnt.
7. I tried to get more info about this string as you suggested:
:verbose set commentstring?
and output is:
commentstring=-- %s
Last set from ~\_vimrc

Note: ~\_vimrc is file $MYVIMRC.
Don't understand why???


The only work-around I have found now is to replace "BufNew" with "BufEnter" in autocmd command inside $MYVIMRC file, restarted gVim and then it works fine.
It is still mistery to me why BufNew does not have effect in my case and BufEnter has. Any idea how to debug?

Thanks

Ben Fritz

unread,
Feb 26, 2016, 10:26:21 AM2/26/16
to vim_use
On Friday, February 26, 2016 at 1:19:35 AM UTC-6, Igor Forca wrote:
> The only work-around I have found now is to replace "BufNew" with "BufEnter" in autocmd command inside $MYVIMRC file, restarted gVim and then it works fine.
> It is still mistery to me why BufNew does not have effect in my case and BufEnter has. Any idea how to debug?
>

Yes. You've already pretty much ruled out your .vimrc, so next check if the problem is in your plugins by starting Vim with:

gvim -N -u NONE -i NONE

(technically this turns off both your plugins and your .vimrc, plus it sets nocompatible mode)

Then, enter your autocmds, and the one plugin you may need. I assume your problem will go away. The usual advice after that would be to enable half your plugins and do a "binary search" for the offending plugin.

But if you don't have very many, it may be faster do debug directly. Instead of :enew do :debug enew

Then enter 's' (step) and/or 'n' (next) until you see where the heck your commenstring gets overwritten. Then go zap that line.

Reply all
Reply to author
Forward
0 new messages