Vim: persistent search buffer between vim instances (in Ubuntu) - search

I have two separate instances of Vim running. I would like to set up my system so that when I search for something in one Vim (using /, ?, * or #), then press n or N in the other Vim, it searches for the same something in the second Vim. I am running Ubuntu.
I already make use of the primary system clipboard for things yanked in vim by using set clipboard=unnamed in my .vimrc file.
Research so far
The command-line tool "xsel" lets me access three different clipboards in Ubuntu: the primary clipboard (used when you select text and middle-click to paste), the clipboard clipboard (used for Ctrl+C, Ctrl+V etc.), and the secondary clipboard (which seems to be unused). My thought at the moment for Vim to use xsel to set the secondary clipboard when /, ?, * or # are used, and use xsel to access the same clipboard when n or N are pressed.

I was going to just post the code I cobbled together for this here, but then decided to bundle it up into a tiny plugin: https://github.com/dahu/VimSharedSearch

With clipboard=unnamed set, you can copy between the last-pattern register (the last thing you searched for), and the shared clipboard using let #/=#* and let #*=#/ (* is the clipboard, and / is last-pattern).
So you can then create a function that copies the last-pattern into the clipboard and map to /, ?, * or #, and another which copies the clipboard to the last-pattern for n or N.

Not exactly what you were asking, but I use split windows (:sp or :vsp) for that purpose.

Related

How to copy all the text from vim editor using vim command line?

I want to select all the text from the vim editor, I tried the command :%y+ but getting error E850: Invalid register name. I get this command from this link. Please help me how to copy all the text from file which is open in vim. They are using yank, what is meaning of it..
I had a similar problem. Don't know why you got so many down votes.
The problem is that you haven't installed vim-gnome which takes about 24 MB and adds a feature to the inbuilt vim.
sudo apt-get install vim-gnome
then your command will work. :%y+ This command will copy all the text in system's clipboard.
TLDR: If you want to copy text in Vim to the system clipboard type ggVG"*y. Explanation below...
Vim runs in the terminal and, depending upon how you are using it and which type of Vim you are running, it's not really designed for you to select text with a mouse and copy and paste in the traditional way.
If you want to select all of the text using Vim then use ggVGy (note the uppercase VG in the middle). This command moves the cursor to the top of the file, enters visual mode, moves to the bottom of the file (thus, selecting all of the text) and then yanks (copies) it. You can then use p to put (paste) this code but only inside of Vim.
If you want to copy to the clipboard to use somewhere outside of Vim then try this:
First, select everything using the commands outlined above but without the final y: (ggVG). Then press "*y. This should now copy it to your operating system's clipboard and you can just paste (Ctrl/Cmd+v) anywhere you want outside of Vim. This can vary depending on what settings you have for Vim but it should work.
A brief explanation of the commands used. gg goes to the top of the file. V enters visual mode by lines. G goes to the end of the file. y yanks (copies) the text but not to the clipboard. p puts (pastes) the text.
The more advanced (i.e. cool) stuff:
" allows you to access registers. For example "a provides access to register a.
The * is the system clipboard so "* provides access to the system keyboard. Therefore, "*y yanks into the system clipboard.
While there's a great explanation of how to exploit the system clipboard in vim, it sounds like you're just having trouble getting your vim to access the clipboard in the first place. Try installing vim-gnome, it gives you the packages you need to get to the system clipboard.
For some reason, "* didn't work for me, but the exact same command with the "+ register did.
To select the whole file you can jump to the beginning, start visual mode, jump to the end:
ggVG
This question is a few years old now, but I had this same problem on Linux Mint 18. I found using xclip worked for me. You can map the command vmap <F7> :!xclip -sel c<CR><CR> in your .vimrc to have your current selection in visual mode copied to the system clipboard.
Here is a thread containing the above (and other) solutions.
You can use
Vggy/vggy or,
VGy/VGy
To visually select any number of text and then copy it, in your case it is gg / G as you want all text on the file,
gg is to copy while your cursor is at bottom of the file, gg for go to top
G is to copy while your cursor is at top of the file
Or even you can always use
Vk(as number of time)y to copy the selected lines of text.

gvim copy to and paste from external clipboard (Windows 7) using y and p keys?

My current gvim copy to and paste from external clipboard is set to ctrl+y and ctrl+p based on a tip I've found on the Internet last year. Is there a way to change these keys to y and p using vim's default yank and paste keys? This way, I don't have to select + copy the text twice (once for vim memory and another for clipboard memory).
I've tried to find the answer on the Internet, but wasn't able to find the answer to my exact problem.
Thank you.
You can create mapping, there is an alternative, however I don't have any windows system, so I cannot test it on win7, if it works. At least it works for gvim.
There is an option clipboard, you can set unnamedplus, to let vim use + as "default" register.
set clipboard=unnamedplus
:h 'clipboard' to check details.
This works on my windows gvim...
:map y "+Y
:map p "+gP
Personally, I just always type "+gP when I want to clipboard paste. It's not a hard muscle memory.

vim: access to clipboard / pasting text from external clipboard

I've seen a guy (in a tutorial) pasting text, which he has copied (or yanked?) in a browser, in a file in vim. I was not able to reproduce this - I'm not very experienced in using vi or vim.
I can quote from this site:
Vim has extended vi to allow use of the * register as a reference to
the system clipboard. So we can use normal mode commands like: "*dd or
1G"*yG to copy things into the * register and "*p to paste text from
it.
So my question (slightly related is this one or this one, which I don't quite understand) is: how can I get access to any clipboard using vim in Fedora/Gnome?
:"*p
did not work for me,
:*p
neither.
Thanks!
First, check whether your Vim has support for this: :version must include +clipboard.
On Linux, there's a distinction between the primary selection (register *) and the system clipboard (register +). See :help quoteplus for details. Best select / copy something and check the output of :register *+ whether it's there.
Finally, the way to get and put things from / to the clipboard in Vim is through the register name. For normal mode commands, that means prefixing "+ to the p (put) or y (yank) command, e.g. typing " + P. There's also an Ex command for command-line mode (which you've attempted), but this takes the register as an argument (and without the "): :put +
VIM 8.1.2269 with +clipboard & +xterm_clipboard enabled.
I do / Ctrl+r +

gvim: Easy copying into system clipboard

I am using gVim on Ubuntu 10.10. I want to copy (yank) text to the system clipboard, so that the copied text is available in other applications.
This works with "+y. But I want to have it working with y.
I have tried to map y to "+y but then yy doesn't work anymore (since it produces "+y"+y).
I have also tried
:set clipboard=unnamed
but this works only the other direction: Text in the system clipboard I can paste with p.
Did you try to map with this command:
noremap y "+y
? This mapping contains a serious mistake: it maps y in normal, visual and operator-pending modes, while you need only normal and visual modes. Use the following:
nnoremap y "+y
vnoremap y "+y
Also try set clipboard=unnamedplus (it requires at least vim-7.3.74). set clipboard=unnamed works in both directions, but it sets «mouse» (*) register which is different from clipboard register (+).
Most online solutions simply tell you to map y to "+y. But sometimes the issue is "+y doesn't even work. You do need to check what features your vim has been compiled with.
Try the steps below:
Open your terminal, run vim –-version | grep xterm_clipboard
Check the sign before xterm_clipboard, if it’s a + (plus sign), go to step 4.
If it’s a - (minus sign), run sudo apt-get install vim-gnome, then sudo update-alternatives –config vim, select vim.gnome in the list (You should use the proper command that corresponds to your system). Run command vim –-version | grep xterm_clipboard again, now you should be able to get + (plus sign).
Check whether your system clipboard uses + (plus sign) or * (star sign) register of vim, this depends on the OS you're using, sometimes they’re equivalent. How to check? Just copy some random text, then open vim and type :reg, check which register shows the string you just copied. If it’s a + (plus sign), add set clipboard=unnamedplus to your .vimrc. If it’s a * (star sign), add set clipboard=unnamed.
Test it out. Copy something inside vim, and then type :reg to check if the system clipboard has changed. If it does, your will get whatever is in that register when you paste outside of vim.
I have the very same idea as you, but I did it for years.
nnoremap yy yy"+yy
vnoremap y ygv"+y
Note that now yy command does two things: First it yank to register as normal, and then it yank to " register (system clipboard). The y command does the same thing. This is because I want to keep the multiple clipboard functionality of Vim.
For pasting from system clipboard, I used to have noremap gp "+p (global pasting), but now I use the excellent plugin Yankring.
Select some text in visual mode and it will be inserted into the system clipboard (the one where you middle-click to paste, I cannot recall the exact name).
If you set mouse=a you can use the mouse for visual selection like you would in many other applications.
In my case, I can sometimes copy from gvim to the system clipboard and sometimes not. I found a workaround, though I don't understand the underlying problem. If I copy text in another application (e.g. Notepad, as I am on Windows 7), then I can copy text from gvim and paste it elsewhere. It looks I need do this for each copy out of gvim.

Copy and paste from external source

I use vim (Actually gvim on windows) as my main text editor. In my work flow I have to copy sentences to/from various external sources, therefore I use clipboard=unnamed to save me key strokes (p instead of "*p).
I copy text from an outer source and I want to paste it over two different places in vim. I mark the first one (v) and then use p to paste over it. The problem is that at this point I lose the original buffer and can't paste it in the second place. It does not exist in the unnamed buffer, the * buffer or the numbered buffers. My guess is that pasting over selection is putting the "pasted over" text in the unnamed buffer.
How can I paste my original string in two locations? i.e. prevent it from getting lost from the buffers.
Thanks.
Try this:
:vmap p "_xP
vmap means to make a mapping that only applies in visual mode.
p is the key to create the mapping for.
"_ is the black hole register. This is used in any situation where you want to delete text without affecting any registers.
xP means delete the selected text, then paste before the resulting cursor position.
You could set up a mapping to ease your pain:
:vmap <F5> "zxP
This will delete the visually selected text, but put it in a different register, so the clipboard isn't affected. Change <F5> to whatever is easiest for you.
I don't know if I misunderstand you but I tried what you are doing and I have no problem in doing that with the + drop-register.
My workflow:
copy a sentence in an external application (ie. browser)
visual select a sentence in vim and replaced it with "+p or p (with clipboard=unnamed set)
visually select another sentence and replace it with "+p
Sadly when pasting the second time you have to explicitly paste from the + register. Therefore I would recommend a mapping for p/P instead of using clipboard=unnamed
nmap p "+p
Try using
:registers
to see the contents of the different registers.
I don't know how to do that on Windows. With KDE, the clipboard has a history that you can select from, so you could do the paste, select the previous selection from the clipboard, and paste in the new location.
That said, it sounds like it might make more sense for you to have it in only one location, then write a script to take that input and create the output you need. Can you elaborate more on what it is you are trying to accomplish?
Check the value of the 'guioptions' options. Make sure the 'a' flag is not set. Also, check that the 'clipboard' option and verify that neither the 'unnamed' or 'autoselect' flags are set.
:set go-=a
:set clipboard-=unnamed

Resources