searching in vim editor - vim

this is a simple query about the searching in vim editor.
consider that i searched for a string like "str" with
/str
now after this search i wanted to search for "strcat" like
/strcat
but, the point in here is that i dont want to type entire /str again.... just wanted to add the new text to /str. that is when we made the first search we type /str and for the second search i just wanted to type cat for searching the entire /strcat.
can any of you vi guru's tell me if it is possible for us to do some search like this in vi.
thanks in advance

The / key begins a search string. up arrow recalls the previous line(s). Therefore,
/ up-arrow cat
will resume your search.

Besides up-arrow to get the previous search, you can also do /^f to be able to fully edit many previous searches. (That's / followed by Ctrl+F). Put the cursor on the previous search that you want to edit, edit it, and hit return.

Others have answered the question you asked, but on the off chance that incremental search would better suit your workflow, I thought I would mention it.
If you're only searching once and finding out that what you're searching for is not unique enough and you need to add more characters, incremental search will show you that before you press enter.
Put the following in your .vimrc to enable incremental search:
set incsearch

Related

How can I search for a second pattern in vi without deleting the first search pattern

In vi text editor, let's suppose I want to navigate through all instances of "cheese" in a text file, so I type "/cheese" and press ENTER. I can use n or N to go to the next/previous instances. If I change my mind and I want to search for "cheesecake", pressing "/" will delete "cheese" so I need to type "cheesecake" instead of only typing "cake".
Most graphical text editors will have the capability to save the last search pattern. Emacs also has this capability. I haven't found how to do this in vi.
I know of no way to make it keep the last command on the line when you open it but you are certainly able to press the up arrow and page through previous commands you've issued. So just type /, hit the up arrow key, type "cake".
PS - This doesn't really belong here. Stackoverflow is about programming questions. You might be better off asking this kind of question over at SuperUser.com.

Vim: How to exclude already typed result from CTRL+N autocomplete

I've searched the manual, but really have no idea what I'm looking for. Here's a screenshot of what happens when I'm typing a word and press CTRL+N to autocomplete it:
I obviously do not want to autocomplete the word I just typed as it's already typed, therefore don't need it to show in the results dropdown.
It doesn't show up every time I use CTRL+N, which is odd.
The controlN autocompletes with words starting
with the keyword at your curson position, starting the search forward
(think of N as "next). And in your case, you do have a word
right on the same line that is requiredF, which was found by the auto
complete.
If your desired keyword is before your cursor, you can use the
similar command controlP which does the search
backwards, thus searching for the previous possible completion. This
is the most common command you will use when you're writing new text,
for example.

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

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.

Get the last search or search&replace string back in vim?

How can I bring back the last string I used for a search or a search&replace?
For example, assume that I enter :%s/some_text/some_other_text/gc and vim gives me the E486: Patterns not found: some_text error message back. I then realize that I actually meant to write some_magic_text instead of some_text. At that point, how can I get back my original string in the bottom command row (or whatever it is called) so I can change it and do a second search? Is there a nifty little command for that?
In this brief example it looks unnecessary, but when the text you are looking to replace is mighty long and you just typed one letter wrong, it is fantastically annoying to have to retype everything.
And I am using MacVim if that makes any difference.
From the normal mode, hit q/ to navigate through your search history!
Check out this vimvcast which explains what you want.
More generally, you can recall any command you have previously typed by entering the first few characters, and then use arrow multiple times to navigate in history.
In your case, you could type:
:%s<Up>
See :help history
This answer might be good an improvement to what you are after, after all.
Use search with highlighting, to interactively check if the regex you are crafting is definitely working, and then use it in a search-replace.
:se is (incsearch, better put se is in your .vimrc)
/<search term>
check with n/N if you are happy with the matches
:s%//<replace term>/g
When omitting the <search term> in the search-replace in 4., the last used search will be used.
For acessing the list of last (search-replace) commands use q:, or as already noted q/ for the list of last search terms.
Bonus:
When using :se gd, s/<search>/<replace> will behave as s/<search>/<replace>/g.
Accessing just the first search match in each line can then still be done with adding /g, so essentially both behaviours are just switched.
/ and then up to bring up the last search query.

Resources