Thursday, February 25, 2016

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

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

--
--
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:

Post a Comment