Vim incremental search next result - vim

I have incsearch and hlsearch enabled
So I hit / to enter search mode.
Then I type text I want to search for and it finds the first occurrence of the text I typed.
Then I want to jump to next occurrence of the text I typed without exiting search mode so that I could refine my search text later without having to exit to normal mode, then hitting n and then going back to search mode by hitting / again.
If this is possible, how can I do it?

The best thing that I can find that you can do is the following:
Search for your pattern using /blah
Then, hit n or ? until you see what's out there...
Then go back to / and press CTRL-r /
this will bring back the last pattern that you searched for, so you can continue entering more text there...
I'm sure you can remap that combination to some key so that you can quickly go back to the search with the pattern already entered.

I've found a way thanks to bwana147 from #vim irc channel on freenode.net
:cnoremap <c-n> <CR>n/<c-p>
After that typing text in search mode and then hitting ctrl-n goes to next occurrence of that text without exiting search mode

I have incsearch and hlsearch enabled. So I hit / to enter search mode. Then I type text which I want to search for and it finds the first occurrence.
Okey so far ...
Here, I can't quite understand what you're asking.
Then I want to jump to next occurrence of the text I typed without exiting search mode
This is done by pressing n
so that I could refine my search text later without having to exit to normal mode, then hitting n and then going back to search mode by hitting / again.
You got me lost here ... isn't this behaviour exactly what incsearch enables you?

Use Ctrl-g or Ctrl-t for incremental search. When you want to search something hit /some, then hit Ctrl-t this will jump the cursor to first some, but you stay in search mode, you can add "thing" to search, and search pattern will be /something.

Related

Inclusive search in vim

I remarked that when I search for a pattern with / in vim, the cursor is sent to the next occurence of the pattern, even if the cursor was on an occurence before the search. How can I search for a pattern and have the cursor be still if it is already on an occurence ?
One can avoid the problem by setting wrapscan and using N, but is there an easier way ?
With incremental search enabled (via set incsearch), the next match is highlighted as you type but the cursor is actually moved only when you hit <CR>. Pressing <Esc> at any time cancels the search and you are back were you started.

how to leave search mode in VIM?

Given a file like the following:
blah
blah
collection = getCollection();
process(somethingInTheWay, target);
...after this short sequence of 'search' commands in vim...
/col<carriage return>
My cursor will be under the first appearance of "collection."
Now, what command(s) will remove the '/col' from my vim command line (in case I want to search afresh, for example)?
There are a couple of things that play a role here. Your input ("/col") is displayed in the command line for some extra time after search. Usually people don't care about this. After some time, some find it even helpful. You already left "search mode" when you pressed Enter and after vim put the cursor on the first match. vim will lazily remove the search string when redrawing screen or when the space is needed. There are some ways to remove it manually, if you want to:
Ctrl-L (redraw)
:redraw
any combination of / or : with Esc or Backspace
Now I wrote, that you already left "search mode" with Enter, but you may continue your last search at any time with n / N. These commands will just take whatever is in the special register / and use it for further searching. It would be possible to "disable" this behaviour by removing the last search pattern from /:
:let #/ = ""
You could manually put something into that register which would enable you to "continue search" without ever having started any search.
If you just want to get rid of the currently highlighted matches, type:
:noh
You can turn off or toggle highlighting matches in general:
:set nohls
:set hls!
The latter is a good candidate for a key binding. I use this to toggle highlighting of the last search (mnemonic: klear - not very accurate but works for me):
:nno <C-k> :set hls!<CR>

How to search any word in linux 'screen'

I am going screen logs of devstack with following command.
screen -x stack
I find searching in screens very difficult. Currently I start copy mode with
Ctrl A Esc and read all the logs, sometimes it is very irritating to go through all logs while searching for a single word.
Is there a good way for searching a word in screens?
from man screen I found
Searching:
/ Vi-like search forward.
? Vi-like search backward.
C-a s Emacs style incremental search forward.
C-r Emacs style reverse i-search.
but screens do not support vi-like forward search.
Once you are in scrollback mode with Ctrl + A, ESC, you should be able to search backwards through the buffer as described in the manual.
So to search for the word "string", press Ctrl + A, ESC, enter ?string and press Enter. It will take you backwards through the buffer to the word string. Pressing N will go to the next match (backwards) in the buffer.
To search forwards, the cursor should be anywhere but the end of the buffer, then use /string to search down.
In Ubuntu, there is a search button in the menu bar. You can search backwards/forward or even search with regular expressions as well.

Omnicompletion search suggestions

Is there any way to configure Omnicompletion for / searches? So searching for /be would suggest other words in the text such as:
/be<tab>
Beatles
beer
Beethoven
personally, I think after typing / you can type a regex, auto-completion doesn't make much sense here... vim doesn't know what you want to have, how can it give suggestions? It is not like in Insert mode.
However there is way to achieve it.
you type /
you type Ctrl-F
you type i (go into insert mode)
you type beTAB
now you see the popup menu.
this works not only for /, but also for : (cmd mode)
Ctrl-F is handy when we write long commands
detail:
:h cedit
You can use the CmdlineComplete plugin.
It will be triggered with <C-n> / <C-p>, and won't show a completion menu (but you can cycle through candidates by repeating the trigger).
You can use a combination of 'incsearch' and command-line completion with CtrlR CtrlW (:h c_CTRL-R_CTRL-W) to achieve something quite close to what you want:
:set incsearch.
Start typing your search pattern, e.g. /Be. The cursor moves to the next potential match as you type.
As soon as the cursor lands on the word you want to complete, hit CtrlR CtrlW. This pulls the word down into your search prompt: it effectively "completes" your search pattern.
At stage 3 you could also use these variants instead:
CtrlR CtrlA (:h c_CTRL-R_CTRL-A) pulls down the WORD instead of the word.
CtrlL (:h c_CTRL-L) completes character by character.
I used the aforementioned CmdLineComplete plugin until I learned about
set incsearch

"Find next" in Vim

To search forward in Vim for cake, I'd type /cake, but the cursor jumps to the first match when I press return. Is there a Vim command analogous to "find next"?
It is n for next and N for previous.
And if you use reverse search with ? (for example, ?cake) instead of /, it is the other way round.
If it is installed on your system, you should try to run vimtutor command from your terminal, which will start a tutorial of the basic Vim commands.
Rob Wells advice about * and # is also very pertinent.
The most useful shortcut in Vim, IMHO, is the * key.
Put the cursor on a word and hit the * key and you will jump to the next instance of that word.
The # key does the same, but it jumps to the previous instance of the word.
It is truly a time saver.
When I was beginning I needed to watch a demo.
How to search in Vim
type /
type search term e.g. "var"
press enter
for next instance press n (for previous N)
You may be looking for the n key.
Typing n will go to the next match.
As discussed, there are several ways to search:
/pattern
?pattern
* (and g*, which I sometimes use in macros)
# (and g#)
plus, navigating prev/next with N and n.
You can also edit/recall your search history by pulling up the search prompt with / and then cycle with C-p/C-n. Even more useful is q/, which takes you to a window where you can navigate the search history.
Also for consideration is the all-important 'hlsearch' (type :hls to enable). This makes it much easier to find multiple instances of your pattern. You might even want make your matches extra bright with something like:
hi Search ctermfg=yellow ctermbg=red guifg=...
But then you might go crazy with constant yellow matches all over your screen. So you’ll often find yourself using :noh. This is so common that a mapping is in order:
nmap <leader>z :noh<CR>
I easily remember this one as z since I used to constantly type /zz<CR> (which is a fast-to-type uncommon occurrence) to clear my highlighting. But the :noh mapping is way better.
If you press Ctrl + Enter after you press something like "/wordforsearch", then you can find the word "wordforsearch" in the current line. Then press n for the next match; press N for previous match.

Resources