Go to search result without having to press enter in vim - vim

I would like for my cursor to automatically go to the search result after I stop typing. For example, I type in /new, after 100ms (or whatever the debounce/delay is), I'd like the cursor to automatically go to the first match (if it exists) rather than me having to constantly hit return with my pinkie, which gets a bit sore having to press return 100 times per day.
Is there a setting to do this in vim, or how could something like this be achieved if there is not? From vim's help for incsearch:
You
still need to finish the search command with <Enter> to move the
cursor to the match.

First, important information:
When you are performing a search and incsearch is set, the current match will be highlighted as the highlight group IncSearch. If you also have hlsearch set, then the other non-current matches on your screen will also be highlighted, instead as the highlight group Search. For example, if you search for thing and the word thing appears three times on screen in your current file, the first match after your cursor will be the 'current match' and highlighted so. The other matches will be highlighted differently.
Now to answer your question
You can move between matches while searching without ending the search by pressing <Enter>. This is done with <C-G> and <C-T> (That's CTRL-G and CTRL-T). Reference :h /_ctrl-g and :h /_ctrl-t. This allows you to move through the file and see all the matches of the pattern you have already typed without actually ending the search. This also allows you to use <C-L> to add characters to the search. I'll let you look that one up (:h /_ctrl-l>).

Related

vim copy paste a block with different line lengths

Is there a way in vim / nvim to block copy paste a set of lines with different lengths.
I want to edit the below text from:
select
date
, impression_cnt
, click_cnt
, like_cnt
from table
to:
select
date
, sum(impression_cnt) as impression_cnt
, sum(click_cnt) as click_cnt
, sum(like_cnt) as like_cnt
from table
I know I can do two separate operations using visual line mode and doing something like
:s/^/sum(
:s/$/) as
However this won't handle the column alias at end.
In VSCode you block enter multiple cursor edit mode and block copy paste the columns, and simple <C-C> and <C-V> and type out max( and ) as in the block mode.
How can I perform this operation without a complex regex that is difficult to remember?
With a single, very simple substitution, assuming the cursor is on the first line to change:
:,+2s/\w\+/sum(&) as &<CR>
The pattern, \w\+, matches one or more "keyword characters", as many as possible, it covers the column names, which are the only part of the lines that need changing,
the & in the replacement part reuses the whole match.
With a manual macro, same assumption:
:,+2norm $ciwsum(<C-v><C-R>") as <C-v><C-r>"<CR>
$ places the cursor on the last character of the line,
ciw cuts the word under the cursor to the default register and enters insert mode,
sum( is just part of the new text you type,
<C-R>" inserts the content of the default register (the <C-v> is used to insert a literal ^R which is needed in this case),
) as is the rest of the new text you type,
and then you insert the column name once again.
Bonus, with the famous "dot formula", same assumption:
$ciwsum(<C-R>") as <C-r>"<Esc>
j.
j.
See :help :range, :help s/\&, :help :normal, :help c, :help iw, :help i_ctrl-r, :help ..
How can I perform this operation without a complex regex that is difficult to remember?
Complex patterns are supposed to be composed on the fly, not remembered so that's not where the problem with complex patterns is. The problem with complex patterns is that it can take time to get them right, which can be a productivity killer.

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

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

VIM, incremental search: How can I jump to the next result and get it highlighted?

When using VIM's incremental search option I usually search and after I got to the first result (it will be highlighted) I use the / key to jump to the next result.
But:
For this I need to 'cancel' my search and go back to Normal Mode in order to jump the next result.
Pressing / makes VIM jump to the next result but it won't highlight it.
I am wondering if there is a more efficient way to jump between the results using incremental search and get them highlighted.
The customary key for jumping to the next search result is 'n'.
I'm not entirely sure what you mean about highlighting - all search results should be highlighted if hlsearch is set, and for me it makes no difference if I go to the next result with 'n' or with '/'.
With Ctrl-t and Ctrl-g you can move between matches without leaving the search mode
If you want to highlight all search hits you should set hlsearch not incsearch.

Resources