How to copy paste from vim to gedit? - vim

I am new in vim and i want to copy text from vim and paste in gedit. In vim I know copy paste by command mode and visual mode but from vim to gedit I have no idea.

you cannot access the clipboard from vim unless you have a +xterm_clipboard
run this command
vim --version | grep "xterm_clipboard"
if you get +xterm_clipboard then its okay but if you get -xterm_clipboard then you need to compile your vim with xterm_clipboard enabled.
Furthermore, you can install the vim-gnome
sudo apt-get install vim-gnome
and after that when you check the
vim --version | grep "xterm_clipboard"
you will get a +xterm_clipboard
open your file in vim
$ vim text.txt
type the following keys in this order
gg"+yG
this will copy the whole text into the system clipboard, then you can directly copy it into any other thing (gedit, browser etc) using Ctrl+v
make sure to keep the file open in vim or else closing the vim editor will clear the contents of the keyboard.
another option can be using
gg"*yG
which copies the text to the external tools using middle mouse click

find the file on disk. using the console, type
cat thefile | xsel -b
paste (ctrl-v) in gedit
You might have to first sudo apt-get install xsel

highlight the text in vim with the mouse, then use the middle click button on your mouse in gedit

Related

If I have vim open in two terminals, how can I share the copy/paste buffer?

I want to yy a line from one instance of vim running in one terminal emulator, and p that line into another instance of vim running in another terminal emulator. Possible? Is there a special setting to use?
There is a system clipboard which you can use to share data not only between multiple terminals, but also using shift + insert in GUI application.
First of all, check if system clipboard is enabled for your vim installation
vim --version | grep clipboard
if you see +clipboard you are good to continue, otherwise, follow this page to enable the system clipboard.
Once the system clipboard is enabled, you can copy line via "+yy into + buffer which is a system clipboard, and paste it in another terminal with vim with "+p command.
You can instead:
Esc for canceling all modes
:r! cat file.txt all content of file.txt pasts in your file, but if you want paste for example from 5 to 11 lines of file then
:r! sed -n 5,11p file.txt

Why does Vim copy/paste between iTerm/Tmux windows work in Bash but not Zsh?

I'm trying to switch from Bash to Zsh but have run into one major problem. I can't copy and paste between Vim editing sessions when using zsh. My typical way to edit multiple files is to open iTerm2 and Tmux, create two vertical Tmux panes, and then edit two files with Vim, one in each pane. In a Bash environment, I can select text in one file/pane with "y" or I can yank lines with "yy" and then switch to the other pane/file and type "p" to paste the selected text or line. With Zsh this doesn't seem to work at all whether I'm using Oh-My-Zsh or Prezto. I've tested this scenario with a very minimal .vimrc file as shown below and with both of the minimal .zshrc configurations that are created when one uses OMZ or Prezto. I know that Vim and these two shells handle buffers differently for security reasons, but I wouldn't think you'd have to know these internals to do something as basic as this. I've read numerous articles on installing and configuring Zsh with both OMZ and Prezto, often within a Vim/iTerm2 environment, and none of them mention that any additional configuration is necessary. I've also tried all of the usual remedies such as setting the iTerm2 preference that will allow applications in the terminal to access the clipboard and setting "clipboard=unnamed" in my .vimrc file.
# .vimrc
set nocompatible
set backspace=indent,eol,start
syntax on
filetype plugin indent on
set number
set hidden
# OMZ .zshrc
export ZSH="/Users/flaugher/.oh-my-zsh"
ZSH_THEME="robbyrussell"
plugins=(git)
source $ZSH/oh-my-zsh.sh
export TERM="xterm-256color"
bindkey -v
export KEYTIMEOUT=1
autoload -Uz compinit && compinit -I
# Prezto .zshrc
if [[ -s "${ZDOTDIR:-$HOME}/.zpretzo/init.sh" ]]; then
source "${ZDOTDIR:-$HOME}/.zprezto/init.sh"
fi
Why isn't Vim copy/paste working in Zsh?
Environment:
macOS 10.14.6
iTerm2 3.2.9
tmux 2.9a
Vim 8.1
zsh 5.7.1
GNU bash 3.2.57
OMZ & Prezto latest versions
After thinking through the problem, I realized that the root cause was that I was using the default version of Vim that comes installed on macOS Mohave. This version doesn't have the clipboard feature enabled.
vim --version | grep "clipboard" # -clipboard
I solved it by installing Vim using Home-brew. And before I ran "brew install vim", I first edited Homebrew's Vim formula and added a line to the configuration parameters to ensure that the clipboard feature is compiled in:
brew edit vim
Add this line:
system "./configure", ...
...
"--enable-clipboard",
...
Then install but add the "-s" option so Homebrew compiles the formula from source:
brew install -s vim
After you do this, run vim --version again and look for +clipboard.
Also be sure to add this to your .vimrc file:
" yank to clipboard
if has("clipboard")
set clipboard=unnamed " copy to the system clipboard
endif
Yank the text into the clipboard buffer and paste from the clipboard buffer.
How I would do it would be to yank with "*y and paste with "*p
It may be different depending on your setup. You need to also make sure you have +clipboard enabled in your vim.
For more info, check out this link that explains in more detail

How to copy paste from notepad to vim?

I am new in vim and i want to copy text from gedit and paste in vim. In vim I know copy paste by command mode and visual mode but from gedit to vim I have no idea.
Is it possible?
Copy whatever you like, in Vim, press Shift+Insert to paste content in clipboard into file. Make sure you are in Insert Mode. It works well in Ubuntu 12.10 as long as I tried.
EDIT
Before the paste, it is recommended to enter the paste mode (by :set paste) of vim in case you meet some un-wanted feature like incorrect auto-indent. (Thanks to #DmitryFrank)
I assume you are running windows and using gvim. To copy from Notepad, just select the text and use the normal Ctrl-C. To paste in vim, specify the clipboard register in normal mode and use the normal p command like this "+p.
Similarly, you can go the other way with "+yy to grab a line from vim.
If you are in Ubuntu or some other Linux, the above trick should still work, but you need to make sure you have the full version of vim with clipboard enabled. In Ubuntu, try running sudo apt-get install vim-gtk.

How to select all and copy in vim?

how to select all and copy in vim insert mode? and is there another way to do it in normal mode?
I have tried visual mode and gg and shift + gg to select all and then yank, however that doesn't transfer it to the clipboard to be able to paste it in another application like skype or chrome browser.
I am sure this is a common task, and there are a lot of varieties by smarter ppl than me out there, please feel free to share yours.
In normal mode:
gg"+yG
In ex mode:
:%y+
There are a few important informations missing from your question:
output of $ vim --version?
OS?
CLI or GUI?
local or remote?
do you use tmux? screen?
If your Vim was built with clipboard support, you are supposed to use the clipboard register like this, in normal mode:
gg"+yG
If your Vim doesn't have clipboard support, you can manage to copy text from Vim to your OS clipboard via other programs. This pretty much depends on your OS but you didn't say what it is so we can't really help.
However, if your Vim is crippled, the best thing to do is to install a proper build with clipboard support but I can't tell you how either because I don't know what OS you use.
edit
On debian based systems, the following command will install a proper Vim with clipboard, ruby, python… support.
$ sudo apt-get install vim-gnome
ggVG
will select from beginning to end
#swpd's answer improved
I use , as a leader key and ,a shortcut does the trick
Add this line if you prefer ,a shortcut
map <Leader>a :%y+<CR>
I use Ctrl y shortcut to copy
vmap <C-y> y:call system("xclip -i -selection clipboard", getreg("\""))<CR>:call system("xclip -i", getreg("\""))<CR>
And ,v to paste
nmap <Leader>v :call setreg("\"",system("xclip -o -selection clipboard"))<CR>p
Before using this you have to install xclip
$ sudo apt-get install xclip
Edit: When you use :%y+, it can be only pasted to Vim vim Ctrl+Insert shortcut.
And
map <C-a> :%y+<Esc>
is not conflicting any settings in my Vimrc.
If you just want a simple way to copy file content from inside the terminal (you mention "copy text to another application"), then the cat command is easy to select/copy the output:
cat <filename>
For VS Code + vim you can:
"vim.useSystemClipboard": true,
and then
ggVGy
gg - go to the beginning of the file
V (capital) - start linewise visual mode
G (capital) - go to the end of file
y - yank (copy)
This works for me across applications in Ubuntu 22.04 and in Windows 11.
gg"+yG is more correct because it explicitly targets the clipboard (rather than a vim register), but "vim.useSystemClipboard": true, + ggVGy seems to work across applications so I use that since it's easier for me to type.
BTW. If using VS Code + vim then
"vim.useCtrlKeys": false,
allows using Ctrl+A.
My approach is to 'cat' the file content then make a selection with the mouse and scroll finally copying to the clipboard with Mac+C / Ctrl+C or even right click and then selecting 'copy'.

How to copy to clipboard in Vim?

Is it possible to copy to clipboard directly from Vim? yy only copies stuff to Vim's internal buffer. I want to copy to the OS's clipboard. Is there any such command in Vim or you can only yank stuff within Vim?
The * register will do this. In Windows, + and * are equivalent. In unix there is a subtle difference between + and *:
Under Windows, the * and + registers
are equivalent. For X11 systems,
though, they differ. For X11 systems,
* is the selection, and + is the cut buffer (like clipboard).
http://vim.wikia.com/wiki/Accessing_the_system_clipboard
* is probably what you want most of the time, so I use * because it functions as I expect it to in both environments.
In Linux distros you have to install vim-gtk (aka gvim) first to gain clipboard functionality. This is because non-gtk vim is typically compiled without X11 support. This is to allow it to run on console only machines (often servers).
And for those confused about how to use registers when yanking or putting, you merely write " then the name of the register. So for copying something to the clipboard register you type "*y and then to put you type "*p (credit: Kyle Mathews)
On Mac OSX
copy selected part: visually select text(type v or V in normal
mode) and type :w !pbcopy
copy the whole file :%w !pbcopy
paste from the clipboard :r !pbpaste
On most Linux Distros, you can substitute:
pbcopy above with xclip -i -sel c or xsel -i -b
pbpaste using xclip -o -sel -c or xsel -o -b
-- Note: In case neither of these tools (xsel and xclip) are preinstalled on your distro, you can probably find them in the repos
In your vimrc file you can specify to automatically use the system clipboard for copy and paste.
On macOS and Windows set:
set clipboard=unnamed
On Linux set (vim 7.3.74+):
set clipboard=unnamedplus
NOTE: You may need to use an up to date version of Vim for these to work.
http://vim.wikia.com/wiki/Accessing_the_system_clipboard
Use the register "+ to copy to the system clipboard (i.e. "+y instead of y).
Likewise you can paste from "+ to get text from the system clipboard (i.e. "+p instead of p).
#Jacob Dalton has mentioned this in a comment, but nobody seems to have mentioned in an answer that vim has to be compiled with clipboard support for any of the suggestions mentioned here to work. Mine wasn't configured that way on Mac OS X by default and I had to rebuild vim. Use this the command to find out whether you have it or not vim --version | grep 'clipboard'. +clipboard means you're good and the suggestions here will work for you, while -clipboard means you have to recompile and rebuild vim.
Summing up and make it easier for newbies,
To copy the current line, in command mode type:
"*yy
To copy the whole file/buffer, in command mode, first go to the beginning via gg, then type
"*yG
As noted, this requires +clipboard out of vim --version, which indicate the availability of clipboard support, -clipboard means no.
For Ubuntu - July 2018
Use the register "+ to copy to the system clipboard (i.e. "+y instead of y).
Likewise you can paste from "+ to get text from the system clipboard (i.e. "+p instead of p).
You have to also make sure that vim is compiled with support for the clipboard. Try:
vim --version | grep .xterm_clipboard -o
and if it's -xterm_clipboard (a minus prefix) then you do not have support.
Here are some instructions for swapping out with a working version of vim that has clipboard support.
$ sudo apt-get purge vim
$ sudo apt-get autoremove (removes any extraneous vim dependency packages from system)
$ sudo apt-get install vim-gnome (or `sudo apt-get install vim-gtk3` for newer Ubuntu versions)
Check again with vim --version | grep .xterm_clipboard -o and you can confirm the clipboard is now available (ie. +xterm_clipboard)
If you are using vim in and old version of macOS, unfortunately the shipped version of vim may and not be compiled with clipboard options. Luckily, homebrew can easily solve this problem.
Install vim:
brew install vim
Install gui verion of vim:
brew install macvim
Restart the terminal to take effect.
Append the following line to ~/.vimrc
set clipboard=unnamed
Now you can copy the line in vim with yy and paste it system-wide.
Updated Method:
I was never satisfied with set clipboard method for years. The biggest drawback is it will mess up your clipboard history, even when you use x for deletion. Here is a better and more elegant solution.
Copy the text [range] of vim into the system clipboard. (Hint: use v or V to select the range first, and then type the colon : to activate the Ex command):
:[line-range]yank +
E.g., to copy/yank lines 5-10 to the system clipboard * register use:
:5,10y *
Paste the content from the system clipboard into vim on a new line:
:put +
Note:
If you select a word range, the above will not work. use "*y or "+y to save this visual block to clipboard. However this is hard to type, I am still thinking about alternatives.
:help "* or :help *+ for more informations
brew info vim will be able to see other options for installing vim. Currently it does not have any other options.
I wasn't able to copy to my clipboard's system because I had this in my ~/.vimrc file:
if has('mouse')
set mouse=a
endif
But if you add this line next, it will allow you to simply Ctrl+c to get whatever you have selected into your clipboard.
vmap <C-c> "+y
Original discussion and more details: Copy text out of vim with set mouse=a enabled
This answer contains details specific to macOS users.
Append the following line to ~/.vimrc:
set clipboard=unnamed
If this does not work, check if your installed version maybe has the clipboard feature not enabled. When this answer was written (2019), the default vim shipped with macOS did not come with clipboard option enabled. You need that option to access the system clipboard.
To check if your vim has that option enabled use the below command
vim --version | grep clipboard
In the result, you should have +clipboard. If it is -clipboard, then your VIM does NOT have the option to access the system clipboard.
You need to MAKE and install your VIM with the option you need. Following are the commands.
# Create the directories you need
$ sudo mkdir -p /opt/local/bin
# Download, compile, and install the latest Vim
$ cd ~
$ git clone https://github.com/vim/vim.git
$ cd vim
$ ./configure --prefix=/opt/local
$ make
$ sudo make install
# Add the binary to your path, ahead of /usr/bin
$ echo 'PATH=/opt/local/bin:$PATH' >> ~/.bash_profile
# Reload bash_profile so the changes take effect in this window
$ source ~/.bash_profile"
The above will install the latest VIM with the option +clipboard enabled.
Now you can yank text to system clipboard. Below steps explains how to yank.
In vim command mode press v, this will switch you to VISUAL mode.
Move the cursor around to select the text or lines you need to copy.
Press y, this will copy the selected text to clipboard.
Go to any external application and CMD + v to paste.
I use MACBook Pro with macOS Mojave and the above works in it.
In vim under ubuntu terminal only,
press shift + drag mouse to select a text in vim then ctrl + shift + c on the terminal
then ctrl + v on other editor
This question already has a lot of answers. I am adding my way which I think is quick.
Quickly, you can press V (Shift + v) to active visual mode. In visible mode, you can use j and k to select the text you want to copy. After selection, use
"*y
Now, selected text is copied to clipboard.
the solution for me was to install additional vim that has the clipboard option included:
sudo apt-get install vim-gnome
You can find the answers here
Arch Wiki
For Linux:
First of all you have to enable Clipboard in your vim version by installing
gvim.
Next you have to put this line on your .vimrc file.
set clipboard=unnamedplus
I have been using these for many years now:
nnoremap Y "+y
vnoremap Y "+y
nnoremap yY ^"+y$
You can now just use upper case Y to copy to clipboard, and lowercase y won't be affected e.g. as by set clipboard=unnamed so you can still choose if the copy will go to the clipboard or not.
Tested on ubuntu 21.04, vim 8.2.
If you are using GVim, you can also set guioptions+=a. This will trigger automatic copy to clipboard of text that you highlight in visual mode.
Drawback: Note that advanced clipboard managers (with history) will in this case get all your selection history…
If you have xclip an easy way of copying text to the clipboard is as follows:
Yank text you want to copy. (y command in vanilla vim)
Type in :call system("xclip -selection clipboard", #")
:call system() runs a terminal command. It takes two arguments, the first the command, the second what to pipe to that command. For example :echom system("head -1", "Hello\nWorld") returns Hello (With some padding). echom returns the output of a command, call doesn't.
xclip -selection clipboard just copies text into the system clipboard as opposed to the default X clipboard, (Accessed by the middle moue button).
#" returns the last yanked text. " is the default register, but you could use any register. To see the contents of all registers, type :registers.
If your vim happens to be compiled without +xterm_clipboard option like it is by default in Debian and I guess Ubuntu, you can pipe selection or entire buffer to external program that handles desktop clipboard. For xclip (which you may need to install previously), the command will be :w !xclip -sel clip
I've been struggling with this for months till now on MacOsX using keyboard shortcuts.
I know question isn't about using keyboard shorts. But this might help someone with the same concern.
I found that if you uncheck:
View -> Allow Mouse Reporting
from Terminal menu, you'll be able to copy to clipboard using
command + c
again.
Put set clipboard=unnamed in your vimrc.
Select what you want to copy in Visual mode (Press v to enter).
Back to Normal mode (Press escape[esc]), press y to copy.
If you want to paste something from OS's clipboard, press p/P in Vim Normal mode.
I'm a Vim newby but, to copy all the text to system clipboard (e.g if you want to paste it to a word processor or another text editor like gedit, mousepad etc...), in normal mode:
ggVGy
or, more simply:
:%y
As suggested, I also installed vim-gtk and put
set clipboard=unnamedplus
in my .vimrc
and everything works fine
If you want to copy only a portion of text, use visual mode (v), select the text you want to copy and press y.
Finally, I suggest a clipboard program like Clipman (my favorite), Clipit, Parcellite or similar.
(I'm using vim 8.0 in Debian Stretch Xfce)
FORGIVE ME FOR MY ENGLISH! :-)
My solution was putting the following line to .vimrc:
map <C-y> :w !xclip -sel c <CR><CR>
The script copies the selected line (trough visual mode) or the file content (if none is selected) to the clipboard using Ctrl + y. I'm using Manjaro Linux if that matters.
Maybe someone will find it useful. I wanted to stay independent from X clipboard, and still be able to copy and paste some text between two running vims. This little code save the selected text in temp.txt file for copying. Put the code below into your .vimrc. Use CTRL-c CTRL-v to do the job.
vnoremap :w !cp /dev/null ~/temp.txt && cat > ~/temp.txt
noremap :r !cat ~/temp.txt
I'm on mac osx (10.15.3) and new to vim. I found this so frustrating and all the answers on here too complicated and/or didn't apply to my situation. I ended up getting this working in 2 ways:
key mapping that uses pbcopy: works on the old version of vim that ships with mac.
Add vmap '' :w !pbcopy<CR><CR> to your ~/.vimrc
Now you can visually select and hit '' (two apostrophes) to copy to clipboard
Install newer version of vim so I can access the solution most recommended in other answers:
brew install vim alias vim=/usr/local/bin/vim (should add this to your ~/.bashrc or equivalent)
Now you can visually select and hit "+yy to copy to clipboard
In case you don't want to use any graphical interface for vim and you prefer to just stick with terminal emulator there may be a much simpler approach to this problem. Instead of using yank or anything like this, first take a look at documentation of terminal you use. I've been struggling with the same issue (trying to use +clipboard and xclip and so on) and in the end it turned out that in my terminal emulator it's enough to just press shift and select any text you want to copy. That's it. Quite simple and no need for messing with configuration. (I use urxvt by the way).
I had issue because my vim was not supporting clipboard:
vim --version | grep clip
-clipboard +insert_expand +path_extra +user_commands
+emacs_tags -mouseshape +startuptime -xterm_clipboard
I installed vim-gnome (which support clipboard) and then checked again:
vim --version | grep clipboard
+clipboard +insert_expand +path_extra +user_commands
+emacs_tags +mouseshape +startuptime +xterm_clipboard
Now I am able to copy and paste using "+y and "+p respectively.
I wrote a simple line in my .vimrc to get copy working. Hope this helps someone. My vim is not installed with Clipboard support, unfortunately, so none of these suggestions worked for me. Basically, paste this line in your .vimrc:
map <C-c> y:e ~/clipsongzboard<CR>P:w !pbcopy<CR><CR>:bdelete!<CR>
If you'd like to read details about what this does, you can read about this on my blog
For some international keyboards, you may need to press "+Space to get a ".
So in those case you would have to press "Space+y or "Space*y
Besides vim-gnome, "+y is also supported by default in neovim on Ubuntu 20.04.
If you don't want to install a new program, you could always do the lazy method of cat file.txt or gedit file.txt and copy from there.
for OSX, like the 10342 answers above made clear, you need to make sure that vim supports the clipboard feature, said the the one that comes pre-shipped with OSX does NOT support clipboard, and that if you run
brew install vim it would work.
Except that running vi will still make you run the preshipped OSX version, not the one you installed from brew.
to get over this, I simply aliased my vim command to the brew version, not the OSX default one:
alias vim="/usr/local/Cellar/vim/8.0.1100_1/bin/vim"
and now i'm golden

Resources