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

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.

Related

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.

altering Vim's text input behavior

[Edit: after reading comments I realize that the Surround plugin is adequate for my needs after all, so I'll leave this question for purely academic purposes to gain a better understanding of vimscript's inner workings]
I'd like to make adding/deleting tags, quotes, braces, and other symmetrical text structures easier to do in Vim, and I find the surround.vim plugin a little too quirky and specialized for my needs.
What I really need is more generally a "mirrored" input mode and "mirrored" deletion mode, whereby I could visually select a block of text, then type onto or delete from both ends of the selection at once. As an example workflow, I'd like to:
select the word hello
hit some keystroke combo to enter "mirror mode"
type "
my text now says "hello"
In this example I only typed one character at each end, but it's important that in step three I could have typed many characters, not just one, for instance I should be able to type <b> to produce <b>hello<b> (I still would need to manually add the / in the closing tag, which I'm OK doing).
So is this even possible in Vim? Could someone provide a broad outline of functions that would be involved in the solution? Specifically, I don't know how to intercept text as it's being inserted and then alter the location where it appears so that it's tacked onto the beginning and ending of the selection block instead of the cursor location. And ditto for deletion.
Well, the behavior you describe is exactly what Surround does:
select the word hello
hit S
type "
my text now says "hello"
The difference with what you ask is the "live updating" or "live mirroring" which I have no idea how to do. You could probably take a look at SnipMate or UltiSnips for that part.

What does <++> mean in gvim? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What does <++> mean in vim (latex-suite), and how do I jump there?
My default editor is vim but I shifted to gvim for the latex suite. Whenever I type $$ in quick succession it adds a <++> after the second $ symbol. What does that mean and how do i disable that?
From the Vim-Latex FAQ:
Q: What are those annoying «» characters whenever I invoke a
mapping?
Those are called placeholders and when you get used to them, they
will prove invaluable. They are essentially markers left in the text
file to tell latex-suite where the next point of interest is. This
lets you directly go to the next placeholder with a single key-press.
Consider a working example. Pressing EFI will insert the following
micro template:
\begin{figure}[h]
\centerline{\psfig{figure=«eps file»}}
\caption{«caption text»}
\label{fig:«label»}
\end{figure}«»
The text «eps file» will be selected and vim will be left in
select-mode so that the user can continue typing straight away. After
having typed in the file name, she can press Control-J (while still in
insert-mode). This will take her directly to the next "place-holder".
i.e, «caption text» will be visually selected with vim in select
mode again for typing in the caption. This saves on a lot of key
presses.
If you still do not feel like using placeholders, include let g:Imap_UsePlaceHolders = 0
in your .vimrc.

searching in vim editor

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

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