38

In vim/gvim I would like to be able to move to the front and end of the current search lookup. Is this possible?

For example, with this file:

A dog and a cat
A hat and a bat

I would like to be able to perform a search, for example /dog\sand and then be able to move from the beginning of the 'dog and' expression to the end and back so that my cursor starts on column 3, under the letter 'd' of the word 'dog' and then moves to column 9 under the letter 'd' or the word 'and'.

The reason I want to be able to do this is so that I can search for an expression and then use the change command, c, combined with a movement command to replace that particular search expression. I don't want to use substitue and replace here, I want to perform this operation using the change command and a movement key.

Pan Thomakos
  • 34,082
  • 9
  • 88
  • 85

4 Answers4

55

You are looking for cgn. From :help gn:

Search forward for the last used search pattern, like
with `n`, and start Visual mode to select the match.
If the cursor is on the match, visually selects it.
If an operator is pending, operates on the match.
E.g., "dgn" deletes the text of the next match.
If Visual mode is active, extends the selection
until the end of the next match.

The beauty of this is you can do cgn and then repeat the command with . and, yes, it will do the same operation on the next search pattern match. This becomes extraordinarily useful when you start searching with complicated regular expressions.

Quinn Strahl
  • 1,718
  • 1
  • 16
  • 22
  • Bless you, Hacker, for answering the question as asked. Search engines bring us to relevant questions, but so often we find "instead of that do this". You were dead on with "the beauty of..." that's why I knew this was the question to search for. – Bruno Bronosky Aug 08 '22 at 16:20
30

Try ths:

/pattern<cr> to place the cursor at the start of search pattern
/pattern/e<cr> to place the cursor at the end of search pattern
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • How would I use this as a move command? I see how this allows me to search to the end of an expression, but it's not a move command. – Pan Thomakos May 10 '11 at 20:33
  • Sorry for confusion with the word `move` here. `/pattern` will place the cursor at the **start** of search pattern. And `/pattern/e` will place the cursor at the **end** of search pattern. – anubhava May 10 '11 at 20:37
  • 1
    Right, but how can this be used as a movement command? Please see my original question. I can't use your answer with the change command, for example. – Pan Thomakos May 10 '11 at 20:44
  • Searches *are* motions. The answer is not so great though because there is a shorthand for "last search pattern"; `//`. – Quinn Strahl Apr 19 '13 at 17:20
  • Short hand `//` is only applicable after pattern has been searched. – anubhava Apr 19 '13 at 17:24
  • 5
    Note because I struggled a bit: If you're using `?`, you need to use `?pattern?e`. – Ven Aug 07 '14 at 12:48
  • @PanThomakos You can re-perform the search with `e` using `//e` or `??e`, as appropriate. – Kyle Strand Jul 05 '18 at 16:07
22

You can change to the end of the match with c//e<CR>. And //<CR> will take you to the beginning of the next match. You could probably work out some kind of bindings for those that make sense. For instance I just tried the following, and it seems to work nicely:

:onoremap <silent>m //e<CR>

So that you can say cm for change match. I'm not sure what I would map //<CR> to, though. I tried mapping it to n, and it seemed to work fine. Don't know if that would be a problem for you.

:nnoremap <silent>n //<CR>
Austin Taylor
  • 5,437
  • 1
  • 23
  • 29
  • 2
    This is a little gross because it overwrites default Vim behaviour with less useful behaviour; for example, you wouldn't be able to do `[count]n`. – Quinn Strahl Apr 19 '13 at 17:24
  • Agreed. The `gn` command mentioned in another answer is much better, but I don't think it existed in May 2011. – Austin Taylor Apr 24 '13 at 14:21
  • @AustinTaylor Thankyou for the //e and // I was trying to figure out how to get to the end of search text. And cgn with period is awesome for repeated replacement. Vim kicks. – kjl Aug 12 '16 at 03:26
  • Use the answer by @QuinnStrahl instead, it's exactly what a vim user would want here and works in pretty much all related situations. Nothing against Austin, who found something that can work for some similar situations, and in a timely fashion for the question-asker. – Starman Jul 22 '20 at 13:50
-2

Since you know just how many words you're searching for, why not just move there and then back using word movement commands? I.e., "2w" to go to end of current search result, and "2b" to go back.

vergueishon
  • 2,686
  • 1
  • 18
  • 10
  • 2
    I do in the trivial example, but in more complex cases it's not that easy. What if the search expression included punctuation and numbers? – Pan Thomakos May 10 '11 at 20:44