Search the real next occurrence of pattern in vim - vim

Let's say I have the text "testestestest"
If I search for "test", vim will only match the 1st & 3rd "test"
I wonder if there's an option to change this default behavior? It's not practical, but I'm just curious
Match four "test" above

I think this is primarily a question of regular expressions, not vim. In vim, you can use \zetest or test\& or \(test\)\#= to meet your needs.

Related

is there a way to see the replaced selection after search and replace in vim

When replacing a regex in vim using search and replace in vim, is there a way to see the result before moving onto the next replace.
The interactive option shows the string matching the regex before replace, but with a complex regex it might or might not work as expected so.
If theres a plugin or hack or default option to achieve this i would really like to know.
The traces.vim plugin provides range, pattern and substitute preview for Vim.
Neovim has a native method to do substitutions previews. I believe it is the 'inccommand' option.

Vim: How to search for more than one word in the same search?

Is it possible to search within vim for more than one word?
For example:
Let's say i'm inside vim and I press / in order to start a search for a certain word, is it possible to search for more than one word in the same search?
Yes, you can have several branches in a search pattern:
/foo\|bar
See :help \|.
Use the OR operator
/first\|second
If you do this often, my SearchAlternatives plugin may be useful to you.
The plugin provides mappings and commands to add and subtract alternative
branches to the current search pattern. Currently searching for "foo", but
also want to find matches for "bar"? You could type /foo\|bar<CR> or
/<C-R>/\|bar<CR>, but once you want to limit the search to whole \<words\>
(like the * command), and juggle several alternatives, adding and
dropping them as you search, this plugin is quicker than manually editing the
search command-line (which you can still do).

Is there a way to elegantly comment specific lines in vim search history, similar to " in command-line history?

I am using vim to edit structured text files.
Sometimes I use search-and-replace feature, and sometimes I am better off with search, followed with a macro, in which case I have several macros at hand, and the choice of one depends upon the particular search result.
In both cases, though I have to spend some effort to arrive at an acceptable regex to satisfy my editing needs. As the regexs are often very long and sophisticated, I end up with both command-line history and search history full of my trial-and-error by-products. The correct regex is not always the last one in a series of attempts, so if I want to reuse the hard-earned regex in a similar editing situation, I have to dig through the pile again.
In search-and-replace scenario I have quickly fixed this with comments that I now put in place at the end of a would-be reusable search-and-replace command string, for example:
:%s/very_long_and_sophisticated_regex/another_long_and_sophisticated_regex/gc "comments on what this search and replace command does and how it might be reused
This way I can easily ignore the piles of stuff in my command line history and quickly find relevant re-use candidates, they are shown in different color. The commands are reusable right away, comments are ignored.
Not so with the search history, though.
Having rtfmed and searched the web, I have not found anything similar. At the moment I put quasi-comments using XXX at the end of reusable search strings, for example:
/search_string_containing_very_long_and_sophisticated_regex XXX comments on what it finds and how it might be re-used
This way I can at least find the right string for re-use, but I have to first delete 'XXX' and the comments.
I wonder if there is a more elegant way of commenting the search strings in search history, similar to command-line history.
You used the word "elegant" in your title, and that I don't have on offer. If instead you can also accept a quirky workaround that relies on Vim internals, here's one.
To restate your problem, it is possible to add comments after Ex :commands,
:AComplicatedExCommand -42 -quux " this fizzes the brobble
:HardToRememberCommand test.txt " use this to brew the framble
but the same is not possible for complicated search /queries (or ?queries).
I've just discovered that you can trick Vim by terminating your search query with a literal null byte:
/[Complicated]*regexp/^# this regexp finds all scrobbles
/another\+Rege\x*p/^# use this to search foo bars
The ^# here is a literal NUL. You can enter it by pressing CtrlV and then 000.
Vim will ignore everything after the null byte, but it'll still show the whole line in the search history, including the "comment".
You can add a regexp branch that never matches, e.g. /\%$indicator\|search string
\%$ is a special Vim atom matching the end of the file. Since that will never match when followed by the indicator text, the first branch (up to \|) will never match and therefore can represent your indicator.
I've created the TaggedSearchPattern plugin to make adding the tag and recalling it even easier.

VIM: use incremental search for file replace

I know that the incsearch setting controls how search in vim highlights as you type. I would like to have the same incremental search and highlight when using the replace command (:%s/foo/bar/ )
The easiest way to do that is to do a search like normal, using 'incsearch' to help ensure the pattern is matching what you want. Once you've got that nailed down, you can either
Leave out the search pattern in :%s//bar/. When there's no specified search pattern, the current value of the / register is used, which will be the search you just did.
Insert the search pattern into the :s command using Ctrl+r/ (see :help c_ctrl-r) or Ctrl+rCtrl+o/ (if the search contains control characters, like ^H). This is useful if you want to make some final tweaks to the pattern or if you want to have it in your command history so you can reuse it even after performing another search.
You could add c at the end of your command like that:
:%s/foo/bar/c
It will walk through all the instances of foo and ask for your confirmation (that's what the c stands for) before changing it to bar.
It's not exactly what you are after though.

Vim - Search and replace the results

I'm getting more and more comfortable with Vim after a few months.
BUT, there is only one simple feature I can't get any answer from the web. That is "Search and replace the results". The problem is that I know:
:/keyword to search, and hit enter "keyword" will be highlighted (of course with set hlsearch)
n, or N to navigate
:% s/keyword/new_keyword/g to replace all occurences of keyword with new_keyword.
BUT, I would think that there must be a way to search, and replace the matched keyword (highlighted) with any new_keyword WITHOUT doing ":% s/keyword/new_keyword/g", which is a lot of typing considering search & replace is such a day-to-day feature.
Any answers/comments will be greatly appreciated!
If you've already done a search you can do a substitution for the same pattern by simply leaving out the pattern in the substitute command. eg:
/keyword
searchs for "keyword", and then:
:%s//new_keyword/g
will replace all occurrences of "keyword" with "new_keyword".
Searching and using the dot command (you didn't meantion you are using the dot command, that's why I highlight it) to repeat the last input action is my best bet here.
I use s///g for search and replace.
Well, since #keyword# and #new_keyword# account for most of the characters, and you need some way to differentiate between them (i.e., a character in vim, or tab between entry fields in dialog in a different editor), you're left with maybe four or five keystrokes beyond that.
So I think you're probably overestimating number of keystrokes and also forgetting that (1) it becomes very natural, and (2) working this way allows you also to naturally modify the action performed by specifying a different range or option flag.
But you can cut down on keystrokes. If you want you can map a key to automatically bring up the command line with '%s/' already in place. e.g.:
nmap s :%s/
The command above would remap 's' (I'm not recommending remapping to that key, but it gives the idea) and set you up to insert the keyword.
Also, you can set the 'gdefault' option to default to substituting multiple times per line. This lets you skip the ending '/g' in your keystrokes:
set gdefault
See ':h gdefault' for help section on that option.
In the end I would say just get used to the default way it works, because using it that way allows you to keep same basic operation when you want to specify different ranges or option flags, and creating a new special map is just another thing to remember. gdefault may be worth setting if you think you're going to want it majority of time, adding /g flag at end when gdefault is set has effect of turning /g off. . .
Move to the first highlighted word then record a macro for replacing the word and moving to the next one, e.g:
gg
n
qq
caw new_word^[
n
q
#q
##
##
...

Resources