Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Move to start and end of search lookup

Tags:

vim

macvim

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.

like image 874
Pan Thomakos Avatar asked May 10 '11 20:05

Pan Thomakos


2 Answers

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.

like image 96
Quinn Strahl Avatar answered Sep 22 '22 21:09

Quinn Strahl


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 
like image 30
anubhava Avatar answered Sep 24 '22 21:09

anubhava