Inclusive search in vim - 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.

Related

How to perform Ctrl+Del in Vim while in insert mode?

Does anyone know how to delete the word in front of the cursor while in insert mode in Vim? Most systems use Ctrl+Del to do this.
I know to delete the word behind the cursor in Vim is Ctrl+w while in insert mode, but I don't know how to delete forward.
Add this to your ~/.vimrc (for VIM or GVIM):
imap <C-D> X<Esc>lbce
; 1 2 3 4 5 ; just a comment for the further description
(In GVIM, <C-Del> also works.)
Description
Set the following binding on Ctrl+D in the insert mode.
Add a dummy letter to deal with words that initially contain only one character.
Leave the insert mode. (The cursor is on the added letter now.)
Move to the beginning of the current word.
Remove characters from the current position to the next end-of-word (i.e., from the first character of the current word to its last character). Then enter the insert mode again.
Behavior
If the cursor was on a character of a word or immediately after the last character of a word (but not at the end of the current line), then this word will be removed.
Otherwise, if there's a word to the left (on the same line), then it will be removed (without any blank characters).
Otherwise (when there's no next word) the behavior is undefined. (Feel free to update the answer to cover these cases too.)
If a word is removed, then the cursor will be at the same position as the first character of the word was.
Use a single normal mode command, whilst in insert mode.
To do this, whilst in insert mode, type
Ctrl+o
This tells vim to accept exactly 1 command as if it were normal mode. Then press
dw
to delete a word.
The accepted solution is not exactly correct, right? It does delete backwards but not forward
In nvim this works:
imap <C-Del> X<Esc>ce
You could also add below to your .vimrc
inoremap <C-Del> <C-o>dw

How to access the last expression used in search in Vim?

When doing more complicated search & replace operation in Vim I often try it out in search and only if it finds what I expect I use search & replace.
Is there a way how to access the last value from search and put it into search & replace or alternatively, to put this last value into a register?
From the VIM wiki page:
:%s//<c-r>//g
Replace each match of the last search pattern with the / register (the last search pattern).
After pressing Ctrl-R then / to insert the last search pattern (and before pressing Enter to perform the command), you could edit the text to make any required change.
If you don't need to edit the search regex you can omit it and vim will use the last search for the pattern.
:%s//<replacement>/
From :h :s | /If the {pattern}
If the {pattern} for the substitute command is empty, the command uses the
pattern from the last substitute or `:global` command. If there is none, but
there is a previous search pattern, that one is used. With the [r] flag, the
command uses the pattern from the last substitute, `:global`, or search
command.
You can edit your search history by pressing q/.
Your search history will open in a separate file, spit vertically.
You can navigate and edit each line like a normal Vim session.
To execute a search, hit ENTER on any line.
To cancel, hit :q to close the search history window.
You can copy and paste things to and from this window.
You can also edit your command history by pressing q:.

Vim EasyMotion search matches

Vim EasyMotion has search character using:
<Leader>fc
which highlights all occurrences of character from which i can choose.
Is there an equivalent motion for search matches like
<Leader>fadd
which hightlights all occurrences of phrase "add" from which i can choose?
<leader>f{char} brings you to character, not a search pattern. (now default <leader><leader>f )
The way I am using this plugin is, I am watching the position (char or word) I want to go to, type <leader><leader>f or w..., type the highlighted letter to move my cursor to the right position. So I don't have a requirement of going to a certain pattern using easymotion.
however what you want is possible. you could search add first, by / or ?, then type <leader><leader>n or <leader><leader>N all adds are with a prefix, which you could choose.
(if your easymotion mapping is with single <leader>, change the double leaders to single)
read
:h easymotion
2.1 Default mappings
for detail

Vim incremental search next result

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.

"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