Highlight search pattern without moving cursor - vim

In vim, is it possible to highlight a search pattern without moving the cursor?
For example, if I want to find m_depthTable I could do:
/m_depthTable
and that will highlight all instances of m_depthTable, but it will also move to the next occurance.
I want to highlight without moving. Possible?

You could do a substitute command with the n flag. This won't move the cursor or do the substitute.
:s/pattern//n

just
/pattern<enter>
then press ``

You can write directly into register that contains last search pattern:
:let #/="m_depthTable"

Related

Vim: substitute once, starting from CURSOR position (not line)

Is there a simple way (without too many keystrokes), to substitute the next occurrence of a pattern (in the line or in the whole document, both would be interesting), starting from the cursor position?
So far, I've only come up with selecting onwards, going to normal mode, and doing :s/\%Vpattern/rep. It's too cumbersome. Perhaps there's a nice way to select the next occurrance of a pattern, and then one can "change" the selection?
Thanks
You can use the confirm option with your substitution. Use the command
:%s/pattern/replacement/gc
It will take you through each occurrence of the pattern. Type y to replace, n to move on without replacing, and q to quit the search.
You can use gn
Just search for the pattern, using /
Select using gn
Once selected, you can perform any action on it. Like y to copy, c to change etc
:h gn

How can I highlight multiple lines in gVim without using the mouse?

Vim noob here. I am trying to select multiple lines of code to copy and paste in other areas. Is there a way to do this without using the mouse?
A few other ways that don't use visual mode at all:
using marks
leave a mark somewhere with ma
move somewhere else
yank from here to there with y'a
using search motions
localize some unique token at the end of the part you want to yank
yank from here to there with y/foo<cr> (forward search) or y?bar<cr> (backward search)
using text-objects
determine what text-object would map to what you want to yank:
inner/outer word, iw/aw
inner/outer pair, i'"([{</a'"([{<
inner/outer html tag, it/at
sentence, s
paragraph, p
"block", ]
…
yank that text-object with, say, yip
using other motions
yank to end of function: y]}
yank to end of file: yG
all of the above solutions with visual mode
V'ay
V/foo<cr>y
V?bar<cr>y
Vipy, etc.
V]}y
VGy
:h motion.txt will hopefully blow your mind, like it did to mine.
You can place your cursor in the first line you want to copy and then type nyy where n is the number of lines you want to copy. For example, type 2yy to copy the two lines under the cursor.
Then, you can paste them using p.
You can also select multiple lines by placing your cursor somewhere and keeping Shift pressed. Move your cursor to the end of the desired selection and stop pressing Shift. Then copy using just y (and not yy) and paste with p.
Yep, in normal mode type V[direction] and you will highlight multiple lines. If you don't want whole lines, use v instead of V. To copy it, hit y and move to the area which you want to paste in and hit p. To delete it, instead of y use x.
Alternatively, you can simply use [number of lines]yy to yank some number of lines or [number of lines]dd to cut some number of lines. In this case pasting is the same.

Highlight line(s) containing search word

I am aware that it is possible to configure vim to highlight all matches for the searched word.
But, is it possible to highlight the entire line(s) on which the searched for word is found? If so, how?
If you generally want the current line highlighted:
:set cursorline
If you just want the searches highlighted, the only easy way is by extending the search pattern to cover the entire line:
:set hlsearch
:let #/ = '.*'.#/.'.*'
Note that on n / N, the cursor will now jump to the beginning of the line, not the matched word. Also, you won't be able to do :%s//... substitutions of the matched word (without repeating the search pattern) any more.
The exact solution depends probably on your goal:
Do you want to make the matched lines stand out a little more?
Do you want to know the line numbers for further use?
Do you want to act directly on those lines?
You could use the quickfix window to list the lines containing a match:
:vim! foo . | copen
You could use the :global command to list or act on every line containing a match:
:g/foo<CR> " list the lines
:g/foo/<Ex command> " act on each line
:set hlsearch
will highlight the searched-for word.
Note also that you can highlight your current line i.e. the line your cursor is on. So when you move between matches, the complete line you move to will be highlighted.
You may install this plugin highlight.vim. One of the shortcut key <C-h><C-j> allowed you to highlight the line contains the previous search pattern.
Please use this:
To highlight all the search pattern matches in a file set the following option:
:set hlsearch
To disable the highlighting temporarily, use:
:nohlsearch

Vim - Commands to visually select a regex match?

Can't figure out how to automatically select a regex match in visual mode.
For example, manually, I could search for a word
/word
It lands the cursor on the first character of the match "word".
Then I press v to enter Visual mode, and press llll to select the whole "word".
Now I want to do this by a macro, and I don't know the length of the match ahead of time.
I expected that Vim would automatically define some built-in marks at the beginning and end of the current match, so that I could ` to them. But I couldn't find any information on that.
What I want is to reassign Ctrl+n to a macro to take me to the next match and select it in visual mode, i.e. not just highlight the match. (To parallel how n takes you to the next match.)
If you're wondering why, its because I want to create folds based on regex matches (like Ctrl+n, zf), but I'm sure it will come in handy in other cases too.
//e takes you to the end character of last search.
More info -- :help {offset}.
You can find how to restore old search buffer here.
For the benefit of folks who stumble across this question in the future, vim now has this feature built-in: gn (and its close sibling gN) will jump to the next (respectively, previous) match and visually select it. It can also be used as a motion; e.g. cgn will jump to the next match and change it.
:noremap <C-n> //s<CR>v//e+1<CR>
Edit summary: was //e, but //e+1 worked for me (selected the last character of the match too).

Move to start and end of search lookup

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.
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.
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
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>
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.

Resources