Loading ...
Sorry, an error occurred while loading the content.
 

Re: Best way to set "comments" in autocommands?

Expand Messages
  • Gary Johnson
    ... I won t say this is the best way, but here is what I would do because I think it is simple and robust. First, set the default the way you want it, e.g.,
    Message 1 of 2 , Nov 3, 2003
      On 2003-11-03, David Coppit <vim@...> wrote:
      > Hi all,
      >
      > My current default comments string includes "s1:/*,mb:*,ex:*/". Since I
      > like to use * for bullets, I want the default to have "fb:*" instead, and
      > reserve the C-style comments just for C and C++ code. What's the best way
      > to achieve this? Here's what I'm thinking:
      >
      > :au BufNewFile,BufRead * set comments-=s1
      > :au BufNewFile,BufRead * set comments-=mb
      > :au BufNewFile,BufRead * set comments-=ex
      > :au BufNewFile,BufRead * set comments+=fb:*
      >
      > :au BufNewFile,BufRead *.c,*.h set comments+=s1:/*
      > :au BufNewFile,BufRead *.c,*.h set comments+=mb:*
      > :au BufNewFile,BufRead *.c,*.h set comments+=ex:*/
      >
      > But this seems not to be robust... For example, I assume that it's s1 and
      > not just s. I would think that it would be better to go to where the
      > default is defined and change it there. (I also wonder if VimEnter might
      > be better than BufNewFile,BufRead.)

      I won't say this is the best way, but here is what I would do
      because I think it is simple and robust. First, set the default the
      way you want it, e.g.,

      :set comments=://,b:#,:!,n:>,fb:-,fb:*

      Then use autocommands to set 'comments' the way you want it for
      particular file types, e.g.,

      :au BufNewFile,BufRead *.c,*.h setlocal comments=s1:/*,mb:*,ex:*/,://

      where those examples are from my own ~/.vimrc. Note that the
      autocommand uses setlocal rather than set. This will ensure that
      that 'comments' option will affect only those buffers containing .c
      or .h files; other buffers will still use your default 'comments'.
      See

      :help setlocal

      You should not use VimEnter for this because it is triggered only
      once each time you start vim. This would work fine if you started a
      new vim instance each time you edited a file, but wouldn't work if
      you opened additional files of different file types in one vim
      instance.

      Another approach to this would be to use filetype plugins. Several
      of those that come standard with vim include 'comments' settings,
      and it is easy to modify these with your own filetype plugins. To
      do this, put in your ~/.vimrc the "set comments=..." line as shown
      above and also add:

      filetype plugin on

      Then put the C-specific 'comments' setting,

      setlocal comments=s1:/*,mb:*,ex:*/,://

      in a new file, ~/.vim/after/ftplugin/c.vim. See

      :help filetype

      HTH,
      Gary

      --
      Gary Johnson | Agilent Technologies
      garyjohn@... | Wireless Division
      | Spokane, Washington, USA
    Your message has been successfully submitted and would be delivered to recipients shortly.