1

I'm trying to add include an unsupported filetype, gf, with vim commentary. The comments are simple, just prepend -- to any line, as in the Haskell tradition.

According to the vim-commentary git,

Relax! You just have to adjust 'commentstring':

autocmd FileType apache setlocal commentstring=#\ %s

If I try the following, I still get /*...*/ instead of --.

autocmd FileType gf setlocal commentstring=--\ %s

The adjust commentstring is still an ambiguous adjustment for those unversed with vim syntax. I'm not sure what where the syntax after comment string begins and ends, for instance, what does %s mean? Also, perhaps unrelated, how do I update the vim environment without closing all windows and opening a fresh session. For instance, the following yields an error:

$ source ~/.vimrc 
autocmd: command not found

Command 'map' not found, but there are 28 similar ones.

-bash: /home/wmacmil/.vimrc: line 5: syntax error near unexpected token `;;'
-bash: /home/wmacmil/.vimrc: line 5: `noremap ;; ;'

Any references to documentation for the above issues are also welcome.

6
  • Why are you sourcing vimrc with bash source? Just run vim, open gf file and try to comment. – Maxim Kim Nov 13 '20 at 12:27
  • @Maxim, I wrote If I try the following, I still get /*...*/ instead of --. meaning the incorrect comment syntax is being used when i write gcc – Warrick Macmillan Nov 13 '20 at 12:30
  • 1
    What is the output of set filetype? – Maxim Kim Nov 13 '20 at 12:34
  • 1
    I wonder if you are mixing "filetype" and a file with some gf extension... – Maxim Kim Nov 13 '20 at 12:39
  • Open your gf file, run :set commentstring=--\ %s and try to comment with gcc – Maxim Kim Nov 13 '20 at 13:15
3
  1. Make sure you vim-commentary works, e.g. you can open python file and gcc to comment current line.
  2. To add support for a new filetype -- filetype has to provide commentstring, so if you are the author of the filetype -- then add setlocal commentstring=--\ %s to your ftplugin/YOURFILETYPENAME.vim.
  3. if you want to add support for a filetype you are not the author, you have to set commentstring either using autocommands or a separate ~/.vim/after/ftplugin/gf.vim

~/.vim/after/ftplugin/gf.vim

setlocal commentstring=--\ %s

or add to your vimrc:

augroup MyGroup | au!
    autocmd FileType gf setlocal commentstring=--\ %s
augroup END
0

I was facing a similar problem with PowerShell file-types.

This answer helped me, and appending this to my .vimrc fixed my issue:

autocmd BufNewFile,BufRead *.ps1,*.psm1,*.psd1 set filetype=powershell syntax=powershell commentstring=#\ %s

I'm posting this for reference. What this does is it associates the filetype "powershell" for the 3 filetypes, and the corresponding commentstring.

1
  • You might be interested in :help ftdetect, :help new-filetype, and :help ftplugin; there are standard and separate files to put the code in, and setting syntax shouldn’t be necessary if you set the filetype. – D. Ben Knoble Mar 9 at 13:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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