0

I am trying to define a buffer variable based on the FileType as it follows:

vim9script

def g:MyComment(comment_symbol: string)
    # Do something
enddef

augroup commenting_blocks_of_code
    autocmd!
    autocmd FileType c,cpp,java.      b:comment_symbol = '//'
    autocmd FileType sh,vim,python    b:comment_symbol = '#'
    autocmd FileType tex              b:comment_symbol = '%'
augroup END

So far, I tested in my .vimrc, in a .c and in a .tex file and it works but it won't work on .py files as I get error E121: undefined variable b:comment_symbol

How do I solve the problem?

2
  • Not on vim 9 but have you tried declaring the variable first like var b:comment_symbol = '//'
    – Hoblovski
    Mar 27, 2023 at 12:43
  • Yes. Then it complains about E1016: Cannot declare a buffer variable error.
    – Barzi2001
    Mar 27, 2023 at 13:22

2 Answers 2

0

I found the problem. I erroneously changed python with py, i.e. I had the following in my script:

autocmd FileType sh,vim,py        b:comment_symbol = '#'

using py as FileType for python won't work, but one has to use python instead:

autocmd FileType sh,vim,python        b:comment_symbol = '#'
2
  • And yet the sample in your question spells python correctly.
    – romainl
    Mar 30, 2023 at 6:23
  • Yes, I am aware of that. But you know, when the time spent becomes considerably high you get frustrated and you start to mess up stupidly. I have made the attempt to replace python with py but I forgot to revert the change locally. Things happen. in-fact I was thinking to delete the post in first place, but then I thought that some people may confuse file extension and file type and make my same mistake.
    – Barzi2001
    Mar 30, 2023 at 6:39
0

Use let for variable assignments. (also, there's an extra dot after java).

augroup commenting_blocks_of_code
    autocmd!
    autocmd FileType c,cpp,java       let b:comment_symbol = '//'
    autocmd FileType sh,vim,python    let b:comment_symbol = '#'
    autocmd FileType tex              let b:comment_symbol = '%'
augroup END

Also the standard way to define a vimscript function is using function instead of def, see :h def and :h E124

function g:MyComment()
  "...
endfunction

So finally you may be desiring this piece of code:

augroup commenting_blocks_of_code
    autocmd!
    autocmd FileType c,cpp,java       let b:comment_symbol = '//'
    autocmd FileType sh,vim,python    let b:comment_symbol = '#'
    autocmd FileType tex              let b:comment_symbol = '%'
augroup END
" map Ctrl-Slash to creating a comment section
" NOTE WE'RE USING <C-_> for Ctrl-Slash
vnoremap <silent> <C-_> :s@^@\=b:comment_symbol<CR>
4
  • I am using vim9script.
    – Barzi2001
    Mar 27, 2023 at 12:22
  • @Barzi2001 Sorry but I cant help; I'm still on 8
    – Hoblovski
    Mar 27, 2023 at 12:28
  • Then you should add vim9script at the top of your sample, just like at the top of your script.
    – romainl
    Mar 27, 2023 at 12:30
  • @romainl fixed.
    – Barzi2001
    Mar 27, 2023 at 12:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.