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

How to skip comments during search

Expand Messages
  • Bert Roos
    Hi, Is it possible in VIM to perform a search where comments are skipped? I m not looking for a regular expression pattern but for an option that simply
    Message 1 of 4 , May 26, 2004
    • 0 Attachment
      Hi,

      Is it possible in VIM to perform a search where comments are skipped? I'm not looking for a regular expression pattern but for an option that simply ignores comments when searching. Since VIM has knowledge of what's comment and what's code, I was hoping this feature exists.

      Thanks, Bert
    • Bert Roos
      Hi, Since nobody replies on this question, I m assuming that it s not possible to skip comments when searching. Isn t this a nice feature to put on the list
      Message 2 of 4 , Jun 1, 2004
      • 0 Attachment
        Hi,

        Since nobody replies on this question, I'm assuming that it's not possible to skip comments when searching. Isn't this a nice feature to put on the list for a later release?

        Regards, Bert


        -----Original Message-----
        From: Bert Roos [mailto:broos@...]
        Sent: Wednesday, May 26, 2004 1:26 PM
        To: vim@...
        Subject: How to skip comments during search

        Hi,

        Is it possible in VIM to perform a search where comments are skipped? I'm not looking for a regular expression pattern but for an option that simply ignores comments when searching. Since VIM has knowledge of what's comment and what's code, I was hoping this feature exists.

        Thanks, Bert
      • vim
        function! SearchNoComment(str, opt) let b:found = 0 while (b:found == 0) call search(a:str, a:opt) let b:name=synIDattr( synID(line( . ), col( . ), 1), name )
        Message 3 of 4 , Jun 2, 2004
        • 0 Attachment
          function! SearchNoComment(str, opt)
          let b:found = 0
          while (b:found == 0)
          call search(a:str, a:opt)
          let b:name=synIDattr( synID(line('.'), col('.'), 1), "name")
          "Whether it's within a comment?
          if b:name !~? "comment"
          let b:found=1
          endif
          endwhile
          endfunction


          and, then map the n and N command as follows:
          nnoremap n :silent call SearchNoComment('<C-R>=@/<CR>', 'f')<CR>
          nnoremap N :silent call SearchNoComment('<C-R>=@/<CR>', 'b')<CR>

          For the first search, you use /pattern as normal, for the later search of
          the same pattern, just use n or N.
          It won't work for the first search, unless you use :call
          SearchNoComment('pattern', 'f')
          I think it's impossible to enable such a feature in a incremental search, is
          it?

          Note, you needn't to map # and * command, it's naturally n and N,
          separately.
        • Bert Roos
          Hi, Thanks for this function! It works fine. I ve extended it a little bit with a global variable that determines whether the behavior should be enabled or
          Message 4 of 4 , Jun 3, 2004
          • 0 Attachment
            Hi,

            Thanks for this function! It works fine. I've extended it a little bit with a global variable that determines whether the behavior should be enabled or disabled. It would be very useful to have this as a standard option in VIM.

            Regards, Bert



            -----Original Message-----
            From: vim [mailto:vim9527@...]
            Sent: Wednesday, June 02, 2004 12:07 PM
            To: vim@...
            Cc: Bert Roos
            Subject: Re: How to skip comments during search

            function! SearchNoComment(str, opt)
            let b:found = 0
            while (b:found == 0)
            call search(a:str, a:opt)
            let b:name=synIDattr( synID(line('.'), col('.'), 1), "name")
            "Whether it's within a comment?
            if b:name !~? "comment"
            let b:found=1
            endif
            endwhile
            endfunction


            and, then map the n and N command as follows:
            nnoremap n :silent call SearchNoComment('<C-R>=@/<CR>', 'f')<CR>
            nnoremap N :silent call SearchNoComment('<C-R>=@/<CR>', 'b')<CR>

            For the first search, you use /pattern as normal, for the later search of
            the same pattern, just use n or N.
            It won't work for the first search, unless you use :call
            SearchNoComment('pattern', 'f')
            I think it's impossible to enable such a feature in a incremental search, is
            it?

            Note, you needn't to map # and * command, it's naturally n and N,
            separately.
          Your message has been successfully submitted and would be delivered to recipients shortly.