There is already a question regarding how to copy text in commandline in Vim. There are two alternative answers:
":p, and
Ctrl+F, followed by finding the previous command.
But these methods don't work when the previous command is a search. That is if I enter into command mode with / or ?, then the search term used cannot be accessed with these methods.
Is there a way to copy text for search text as well?
I believe you are looking for the / register. You can use "/p to paste the last search. While on the command-line you can use <c-r> followed by a register to insert the contents of the given register. example: <c-r>/ will paste the last search.
You may also wish to use q/ to do more extensive editing of your previous searches.
:h registers
:h q/
:h c_CTRL-R
Taking the approach similar to the one proposed in the accepted
answer to the question “How to copy text from commandline mode
in Vim”, one can use the / register to paste the most recent
search pattern:
"/p
The whole history of searches can be explored using the command-line
window (see :help cmdwin). To open it for editing of search strings
from Normal mode, use the q/ or q? commands. To do the same when
entering a search pattern for the / or ? commands, press the key
combination specified by the cedit option (Ctrl+F,
by default).
Related
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:.
I like to use "*" to search text in vim. after hight light the target text, I want to edit all of them, is there any way I can do it in vim? for example, after highlight text, I just need to press ctrl+i then the highlight text can be edited simultaneously
Simultaneous editing (like seen in other editors) is not built into Vim (but there are plugins). You don't need them, though. After *, the text is stored in the last search pattern register, and you can just :substitute// without repeating what you're searching for:
:%s//replacement/g
The % is a range and applies this to the whole buffer; the /g is a flag that replaces all (globally) instances, not just the first in each line. Read :help :s for details.
You can check out the vim-multiple-cursors plugin.
Personally, I like #Ingo's solution. The less plugins the better.
As a nice alternative. You can use gn and the . command.
Set your search pattern i.e. * or /foo
Change your highlighted pattern via c operator over the gn motion
cgnbar<esc> will change the highlighted area to bar.
Now you can use . too repeat this change. You can also use n to skip places.
Note: This requires at least 7.4
For more help see:
:h gn
:h .
If you wish to edit the word with another you can use the substitute command. (e.g. :%s/hi/hello/g)
This will change all occurrences of hi to hello in the file.
I'm aware of the vim replace command, which is of the form, eg:
:%s/old/new/gc
But what if either of these strings is long? How can I use something like visual selection mode, the clipboard or vim registers instead of having to type the old/new text in?
You can use q: to bring up a command-line window. This lets you use all the vim editing commands to edit the vim command line, including p to paste. So, you could copy the text into a register, paste it into the command line window, and execute it that way.
I recently discovered this feature via vimcasts.
According to the manual, you can use Ctrl+R to insert the contents of a register into the current position in the command line. The manual also claims that Ctrl+Y inserts the text highlighted with the mouse into the command line. Remember that in X11 and some other systems, you can also paste text into a program from the system clipboard using the middle mouse button or a menu command in your terminal emulator.
I think to avoid have your command line be huge you can use this to solve your issue
:%s/foo/\=#a/g
That replaces "foo" with whatever is in register a.
If you're trying to do a substitute with a long complicated search pattern, here's a good way of going about it:
Try out the search pattern using some test cases and refine it until you have the pattern you want. I find incsearch really helps, especially with complicated regular expressions.
You can then use :%s//new to replace all instances of the last searched for pattern.
If you've entered a pattern and want to copy it out of the search history, you can use q/ to bring up a command line window containing recent search patterns very similar to the q: one that contains recent command history.
On the other hand, if you're asking about how to copy and paste text into the substitute command:
I'd write the pattern out in insert mode and yank the search and replacement into two distinct registers using, say, "ay and "by and then use :%s/<C-R>a/<C-R>b/gc to do the substitute. There are lots of variations of the yank command, but this one should also work automatically when using a visual selection.
If you're copying in text from the clipboard, you can use <C-R>* to paste it's contents in insert mode.
I have the following mapping in my .vimrc
vnoremap <leader>r "ry:%s/^Rr/
So I visually select the thing I want to replace, and hit ,r, type the replacement and hit return. If I want to paste the replacement, I yank it before selecting the text to replace, and then use <C-r>" to paste it as the replacement before hitting return.
Note: to insert ^R in your .vimrc, you actually type <C-v><C-r>.
I'm trying to copy text from my vim search term. I spend a fair bit of time building regular expressions for sed search&replace. Since my regex is often quite complicated, I like to build it up in a search before executing :%s/regex/new text/g
In a terminal vim, I can copy my new regex from the search line using the mouse. I'd like to use gvim as much as possible, but it doesn't let right mouse clicks through for me to copy.
Any ideas how to get the search term into a buffer?
Thanks,
Andrew
In command mode (where you are when you hit : in normal mode), you can do ctrl-R /, which will expand to your last search term (other ctrl-R favorites are " for your yank buffer, or % for the full path of the current window)
You actually don't need to do that though. If you leave out the search term for :s, it will assume you want to use the last thing you searched for. so you can /searchregex, and then right after do :%s//replaceregex/ and it will use search regex to do the replace.
Use q: to open an editable window containing your commandline history. From there you can use all your usual Vim toolset to copy/paste/etc.
For the equivalent feature regarding search history, type q/.
q/ shows the search history. For the reverse action of copying a string from your normal buffer into command buffer or search buffer, which doesn't allow the normal use of p, use Ctrl-R 1 to paste.
You can copy text between registers using :let, e.g. copy last search term into register b:
:let #b=#/
Then use ctrl-R b to insert it as in Matt Briggs' answer. Of course that isn't necessary when you can insert it directly using ctrl-R / (in insert or ex mode) or "/p (in normal mode), but with this mapping:
nnoremap <silent> y/ :let #"=#/<CR>
you can type y/ to copy the last search term to the unnamed register for easy pasting. You could use the * register instead to copy to the system clipboard and have the text available to other apps:
nnoremap <silent> y/ :let #*=#/<CR>
See
:help 'clipboard'
:help registers
for more info about Vim registers and using the system clipboard.
I would like to have the following search in Vim too
(reverse-i-search)`':
Enter a word of your previous command, and you get the full command.
I know the chronological history tool in Vim
q:
However, it is not that useful as the fuzzy reverse search.
How can you have a similar reverse search in Vim as in the terminal?
Type q: in the normal mode to open commands window. You can search/edit here using regular vim commands. You start in Normal mode. Press Enter to execute a command.
This approach lets you search across whole command not just beginning of line.
Enter the first letters of your previous command and push <Up> arrow (or Ctrl+p).
:set li<up>
:set lines=75
Don't forget to check history option and set it to big enough value
:set history=1000
Press Ctrl+F in command mode to open the command history window. Then, you can use / , ? , and other search commands. Press Enter to execute a command from the history.
For more about the command history window, see :h cmdwin .
Here are the docs for Vim's commandline history, also see this part of the docs on Vim's commandline history that covers the key bindings while in the history. It looks like you can say :foo and then hit the up arrow to find the last command that started with foo.
With FZF fuzzy search command: :History:
Source: https://github.com/junegunn/fzf.vim
I was looking for this as well (finally after wondering why it wasn't built-in for some time) and decided I couldn't resist whipping up an implementation, so here you go: https://github.com/goldfeld/ctrlr.vim
It should work just like the shell's--well there are still a couple basic things missing (like pressing ^R again to skip to next match), but all that I use is in this first release, and I plan to add the rest in the coming weeks as I get time.