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

automatically comment/uncomment line and region

Expand Messages
  • Martin Norbäck
    I do a lot of editing of files in different programming languages and I often need to comment and uncomment lines and regions. I ve done the following in my
    Message 1 of 7 , Apr 2, 2003
    • 0 Attachment
      I do a lot of editing of files in different programming languages and I
      often need to comment and uncomment lines and regions.

      I've done the following in my .vimrc:

      au FileType haskell let b:comment_leader = '-- '
      au FileType vim let b:comment_leader = '\" '
      au FileType vhdl let b:comment_leader = '-- '
      au FileType c let b:comment_leader = '// '
      au FileType ada let b:comment_leader = '-- '
      au FileType sh let b:comment_leader = '# '
      au FileType java let b:comment_leader = '// '
      au FileType make let b:comment_leader = '# '

      fun! Comment()
      let savedreport = &report
      let &report = 100000
      let str = escape(b:comment_leader, '/')
      exe 'sm/^/'.str.'/e'
      let &report = savedreport
      endfun

      fun! Uncomment()
      let savedreport = &report
      let &report = 100000
      let str = escape(b:comment_leader, '/')
      exe 'sm/^'.str.'//e'
      let &report = savedreport
      endfun

      " Comment / uncomment lines
      map <silent> ,c :call Comment()<CR>
      map <silent> ,u :call Uncomment()<CR>

      I still think it's a bit clumsy, so I thought I'd extract the correct
      comment_leader from the 'comments' variable. This seems easier said than
      done, because you need a one-line comment marker, and comments contain
      all sorts of strange stuff, and many ftplugins only add stuff, they
      don't remove the old value of 'comments'.

      Does anyone have any comments on this? (Pun intended.)

      Regards,

      Martin
    • Piet Delport
      ... You could make this a bit more concise by merging lines with the same comment leader, like so: au FileType haskell,vhdl,ada let b:comment_leader = --
      Message 2 of 7 , Apr 2, 2003
      • 0 Attachment
        On Wed, 02 Apr 2003 at 16:36:27 +0200, Martin Norbäck wrote:
        >
        > I do a lot of editing of files in different programming languages and I
        > often need to comment and uncomment lines and regions.
        >
        > I've done the following in my .vimrc:
        >
        > au FileType haskell let b:comment_leader = '-- '
        > au FileType vim let b:comment_leader = '\" '
        > au FileType vhdl let b:comment_leader = '-- '
        > au FileType c let b:comment_leader = '// '
        > au FileType ada let b:comment_leader = '-- '
        > au FileType sh let b:comment_leader = '# '
        > au FileType java let b:comment_leader = '// '
        > au FileType make let b:comment_leader = '# '

        You could make this a bit more concise by merging lines with the same
        comment leader, like so:

        au FileType haskell,vhdl,ada let b:comment_leader = '-- '

        > fun! Comment()
        > let savedreport = &report
        > let &report = 100000
        > let str = escape(b:comment_leader, '/')
        > exe 'sm/^/'.str.'/e'
        > let &report = savedreport
        > endfun
        >
        > fun! Uncomment()
        > let savedreport = &report
        > let &report = 100000
        > let str = escape(b:comment_leader, '/')
        > exe 'sm/^'.str.'//e'
        > let &report = savedreport
        > endfun

        Some minor comments:

        If you're only changing 'report' here to keep :sm from displaying a
        message, it's much easier to just prefix it with :sil[ent].

        You're also not escaping the comment leader enough, i think. In
        Comment(), it can contain the escapes in :h sub-replace-special. In
        Uncomment(), it can contain just about any regex special chars.

        > " Comment / uncomment lines
        > map <silent> ,c :call Comment()<CR>
        > map <silent> ,u :call Uncomment()<CR>
        >
        > I still think it's a bit clumsy, so I thought I'd extract the correct
        > comment_leader from the 'comments' variable. This seems easier said than
        > done, because you need a one-line comment marker, and comments contain
        > all sorts of strange stuff, and many ftplugins only add stuff, they
        > don't remove the old value of 'comments'.
        >
        > Does anyone have any comments on this? (Pun intended.)

        I also think extracting it from 'comments' isn't practical,
        unfortunately. One can, however, squeeze the functions down into one
        line each, while improving the escaping:

        noremap <silent> ,c :<C-B>sil <C-E>s/^/<C-R>=escape(b:comment_leader,'\/')<CR>/<CR>
        noremap <silent> ,u :<C-B>sil <C-E>s/^\V<C-R>=escape(b:comment_leader,'\/')<CR>//e<CR>

        Here are the things i've changed, and why:

        - Added :sil to the substitute. The <C-B> and <C-E> are just to deal
        with visual mode's range, when called from there.
        - Escaped backslashes along with slashes.
        - Added \V at the start of ,u's regex, so that only backslashes are
        special in the pattern, and hence need to be escaped.
        - map -> noremap, just for in case. :-)

        --
        Piet Delport
        Today's subliminal thought is:
      • Martin Norbäck
        ... Weee, severe magic. Thanks. Now I will never be able to understand^Wchange this mapping again :) Regards, Martin -- Martin Norbäck
        Message 3 of 7 , Apr 2, 2003
        • 0 Attachment
          ons 2003-04-02 klockan 18.52 skrev Piet Delport:
          > noremap <silent> ,c :<C-B>sil <C-E>s/^/<C-R>=escape(b:comment_leader,'\/')<CR>/<CR>
          > noremap <silent> ,u :<C-B>sil <C-E>s/^\V<C-R>=escape(b:comment_leader,'\/')<CR>//e<CR>

          Weee, severe magic. Thanks. Now I will never be able to
          understand^Wchange this mapping again :)

          Regards,

          Martin

          --
          Martin Norbäck <d95mback@...>
        • Johan Svedberg
          * Apr 02 16:37 Martin Norbäck : [...] ... Shouldn t it be: au FileType vim let b:comment_leader = Escape:ing doesn t seem
          Message 4 of 7 , Apr 3, 2003
          • 0 Attachment
            * Apr 02 16:37 Martin Norbäck <d95mback@...>:

            [...]

            > au FileType vim let b:comment_leader = '\" '

            Shouldn't it be:

            au FileType vim let b:comment_leader = '\" '

            Escape:ing doesn't seem necessary?

            [...]

            --
            Johan Svedberg, winkle@..., http://www.acc.umu.se/~winkle
          • Antoine J. Mechelynck
            ... IIUC, the colon is needed if you run it from the keyboard in Normal mode, to tell (g)vim to interpret the rest (until you press ) as an ex-command.
            Message 5 of 7 , Apr 3, 2003
            • 0 Attachment
              Johan Svedberg <winkle@...> wrote:
              > * Apr 02 16:37 Martin Norbdck <d95mback@...>:
              >
              > [...]
              >
              > :au FileType vim let b:comment_leader = '\" '
              >
              > Shouldn't it be:
              >
              > au FileType vim let b:comment_leader = '\" '
              >
              > Escape:ing doesn't seem necessary?
              >
              > [...]
              >
              > --
              > Johan Svedberg, winkle@..., http://www.acc.umu.se/~winkle

              IIUC, the colon is needed if you run it from the keyboard in Normal mode, to
              tell (g)vim to interpret the rest (until you press <Enter>) as an
              ex-command. In a script, each line is interpreted as an ex-command, so the
              colon is not necessary.
            • Johan Svedberg
              ... No, it s not. It wasn t that I was talking about either. I see now that I forgot to change the line. It should be: au FileType vim let b:comment_leader
              Message 6 of 7 , Apr 3, 2003
              • 0 Attachment
                * Apr 03 20:24 Antoine J. Mechelynck <antoine.mechelynck@...>:
                > Johan Svedberg <winkle@...> wrote:
                > > * Apr 02 16:37 Martin Norbdck <d95mback@...>:
                > >
                > > [...]
                > >
                > > :au FileType vim let b:comment_leader = '\" '
                > >
                > > Shouldn't it be:
                > >
                > > au FileType vim let b:comment_leader = '\" '
                > >
                > > Escape:ing doesn't seem necessary?
                > >
                > > [...]
                > >
                > > --
                > > Johan Svedberg, winkle@..., http://www.acc.umu.se/~winkle
                >
                > IIUC, the colon is needed if you run it from the keyboard in Normal
                > mode, to tell (g)vim to interpret the rest (until you press <Enter>)
                > as an ex-command. In a script, each line is interpreted as an
                > ex-command, so the colon is not necessary.

                No, it's not. It wasn't that I was talking about either. I see now that
                I forgot to change the line. It should be:

                au FileType vim let b:comment_leader = '" '

                (i.e. no escape:ing needed for the ").

                --
                Johan Svedberg, winkle@..., http://www.acc.umu.se/~winkle
              • Johan Svedberg
                * Apr 02 18:53 Piet Delport : [...] ... [...] ... How could I change this so that when I comment C I would instead get the old fashion /* foo
                Message 7 of 7 , Apr 5, 2003
                • 0 Attachment
                  * Apr 02 18:53 Piet Delport <pjd@...>:

                  [...]

                  > > au FileType c let b:comment_leader = '// '

                  [...]

                  > noremap <silent> ,c :<C-B>sil <C-E>s/^/<C-R>=escape(b:comment_leader,'\/')<CR>/<CR>
                  > noremap <silent> ,u :<C-B>sil <C-E>s/^\V<C-R>=escape(b:comment_leader,'\/')<CR>//e<CR>

                  How could I change this so that when I comment C I would instead get the
                  old fashion /* foo */ style?

                  --
                  Johan Svedberg, winkle@..., http://www.acc.umu.se/~winkle
                Your message has been successfully submitted and would be delivered to recipients shortly.