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

Highlighting current search hit

Expand Messages
  • gumnos (Tim Chase)
    I ve got hls set, and am getting all sorts of visual results back from my search. However, it s a bit cluttering to see a whole screenfull of hits. Is
    Message 1 of 4 , Apr 23, 2004
    • 0 Attachment
      I've got 'hls' set, and am getting all sorts of visual results back from
      my search. However, it's a bit cluttering to see a whole screenfull of
      hits. Is there any way to simply highlight only the first hit, as
      happens in most other programs? You do a / or ? to search and the first
      hit gets highlighted. You hit n/N to go to the next hit, and it
      highlights that one. Is there any way to duplicate this behavior that
      I've not managed to dig out of the help?

      While many times it's nice to have all the hits highlighted, there are
      other times when you don't want to be distracted (or lose your place on
      the screen)

      Thanks,

      -tim
    • Antony Scriven
      ... This might be a starting point. You may want to map this rather than using cursorhold. se ut=1 au cursorhold * call TC_search_highlighting() hi Other_hits
      Message 2 of 4 , Apr 27, 2004
      • 0 Attachment
        Tim Chase wrote:

        > I've got 'hls' set, and am getting all sorts of visual results back from
        > my search. However, it's a bit cluttering to see a whole screenfull of
        > hits. Is there any way to simply highlight only the first hit, as
        > happens in most other programs? You do a / or ? to search and the first
        > hit gets highlighted. You hit n/N to go to the next hit, and it
        > highlights that one. Is there any way to duplicate this behavior that
        > I've not managed to dig out of the help?
        >
        > While many times it's nice to have all the hits highlighted, there are
        > other times when you don't want to be distracted (or lose your place on
        > the screen)

        This might be a starting point. You may want to map this
        rather than using cursorhold.

        se ut=1
        au cursorhold * call TC_search_highlighting()
        hi Other_hits gui=none guifg=fg guibg=bg
        let g:line = 1
        let g:col = 1
        fun! TC_search_highlhting()
        if strpart(getline('.'), col('.') - 1) =~ '^' . @/
        let g:line = line('.')
        let g:col = col('.')
        endif
        exe 'match Other_hits /\(\%' . g:line . 'l\%' . g:col . 'c\)\@!'
        \ . @/ . '/'
        endfun

        Plus there's also

        cno <C-G> <C-M>/<C-R>/
        cno <C-T> <C-M>?<C-R>/

        for doing a similar thing during an incremental search.

        Antony
      • Hari Krishna Dara
        ... Just a comment about the original requirement. The OP is comparing the selection by search in other editors with that of search highlighting in Vim. These
        Message 3 of 4 , Apr 27, 2004
        • 0 Attachment
          On Tue, 27 Apr 2004 at 10:21am, Antony Scriven wrote:

          > Tim Chase wrote:
          >
          > > I've got 'hls' set, and am getting all sorts of visual results back from
          > > my search. However, it's a bit cluttering to see a whole screenfull of
          > > hits. Is there any way to simply highlight only the first hit, as
          > > happens in most other programs? You do a / or ? to search and the first
          > > hit gets highlighted. You hit n/N to go to the next hit, and it
          > > highlights that one. Is there any way to duplicate this behavior that
          > > I've not managed to dig out of the help?
          > >
          > > While many times it's nice to have all the hits highlighted, there are
          > > other times when you don't want to be distracted (or lose your place on
          > > the screen)
          >
          > This might be a starting point. You may want to map this
          > rather than using cursorhold.
          >
          > se ut=1
          > au cursorhold * call TC_search_highlighting()
          > hi Other_hits gui=none guifg=fg guibg=bg
          > let g:line = 1
          > let g:col = 1
          > fun! TC_search_highlhting()
          > if strpart(getline('.'), col('.') - 1) =~ '^' . @/
          > let g:line = line('.')
          > let g:col = col('.')
          > endif
          > exe 'match Other_hits /\(\%' . g:line . 'l\%' . g:col . 'c\)\@!'
          > \ . @/ . '/'
          > endfun
          >
          > Plus there's also
          >
          > cno <C-G> <C-M>/<C-R>/
          > cno <C-T> <C-M>?<C-R>/
          >
          > for doing a similar thing during an incremental search.
          >
          > Antony
          >

          Just a comment about the original requirement. The OP is comparing the
          selection by search in other editors with that of search highlighting in
          Vim. These two are not same. May be the right way to do this is to turn
          off 'hlsearch' and put the current hit in visual mode (ideally selection
          mode, but you will loose the convenience of pressing 'n' or other Vim
          commands quickly).

          HTH,
          Hari




          __________________________________
          Do you Yahoo!?
          Win a $20,000 Career Makeover at Yahoo! HotJobs
          http://hotjobs.sweepstakes.yahoo.com/careermakeover
        • gumnos (Tim Chase)
          ... [snip] ... Heh, sorry if I wasn t totally clear there. Though Antony was remarkably close in his stab at things, despite my possibly muddy description of
          Message 4 of 4 , Apr 27, 2004
          • 0 Attachment
            > > > I've got 'hls' set, and am getting all sorts of visual results
            > > > back from my search. However, it's a bit cluttering to see a
            > > > whole screenfull of hits. Is there any way to simply highlight
            > > > only the first hit, as
            [snip]
            > Just a comment about the original requirement. The OP is comparing the
            > selection by search in other editors with that of search highlighting
            > in Vim. These two are not same. May be the right way to do this is to
            > turn off 'hlsearch' and put the current hit in visual mode (ideally
            > selection mode, but you will loose the convenience of pressing 'n' or
            > other Vim commands quickly).

            Heh, sorry if I wasn't totally clear there. Though Antony was
            remarkably close in his stab at things, despite my possibly muddy
            description of the matter.

            No, I don't need to make the hit into a visual selection (though that's
            a kinda cool idea too). The main idea was to have something along the
            lines of a built-in highlight group that highlights the hit onto which
            the cursor jumps when you do a search, much like there's a highlight
            group for Search (":he hl-Search"). However, with this, all the hits on
            the screen get highlighted. I'm looking for a highlight group (or
            something that mimics one) that only matches the first hit (doesn't have
            to last past a single keystroke) where the cursor jumps to on a search
            (with "/", "?", "*", "#", "g*", "g#", "n", or "N"...perhaps also "gd" or
            "gD" as well?).

            eg.

            foo barXfoo bar foo bar foo bar
            foo bar foo bar foo bar foo bar

            with the cursor on the X, if you do a

            /bar

            I'm looking to have a highlight group that matches:

            foo bar foo[Xar]foo bar foo bar
            foo bar foo bar foo bar foo bar

            where the X is the cursor on top of the "b" in "bar" and the [...] is
            the desired highlight group. Rather than (or in addition to) what "set
            hls" shows:

            foo[bar]foo[Xar]foo[bar]foo[bar]
            foo[bar]foo[bar]foo[bar]foo[bar]

            (same X and [...] notation) Ideally, it would be function in concert
            with hls, so that one could highlight them differently such as

            foo[bar]foo{Xar}foo[bar]foo[bar]
            foo[bar]foo[bar]foo[bar]foo[bar]

            where [...] wraps a highlight search (as "Search" already does), and
            {...} wraps this desired next-search-hit target. Doing anything
            subsequently (moving, mousing, whatever) could revert the {...} to a
            [...] group.

            Sorry for the moderately verbose exampling, but the pictures seem to
            give a better portrayal of what I was hoping to find a way to do. I
            suspect it would have to be an internal vim thing, to create a built-in
            highlight group, much like the Search group. It might be even easier to
            add in, as vim is already moving the cursor to the desired hit, and
            highlighting it with hls. Granted, the search-location modifiers
            ("/foo/+3", etc) could throw a wrench in this pipe-dream of it being
            nice and easy to implement :) Also, if it were internal, it could
            readily embody the multitude of ways to search (*, #, /, ?, g*, g#, n,
            N, :/, etc)

            Well, thanks anyways,

            -tim
          Your message has been successfully submitted and would be delivered to recipients shortly.