How to select complete file in vi, to be pasted in gedit?

26,232

Solution 1

Not sure if vi is required for this operation.

There is xclip utility that allows you to copy anything from console output to x server clipboard.

You should specify DISPLAY=:0.0 environment and execute it like this:

cat file | xclip

or for remote file

ssh remote "cat file" | xclip

Or from vi (note, that this way will temporaly clear vi buffer content, to revoke it press Esc + u, data will stay in X cilpboard):

:%!xclip

Now you're able to paste it anywhere with middle mouse button (note, that CTRL+V or shift+ins won't work).

Solution 2

Rather than yanking into the a register yank into the * or + register to yank the file contents into the X clipboard using one of:

:%y*
:%y+

This does require that you're using a copy of vim that has X support compiled in and is able to connect to your X server. Which of those works better for you will depend on which type of clipboard the target editor uses.

You can even have one of those registers used as the default for yank and paste operations by including one of the following in your .vimrc file:

set clipboard+=unnamed
set clipboard+=unnamedplus

Solution 3

If you're regularly finding yourself needing to use both Vim and gedit together, you should probably switch to gVim, the GUI version of Vim.

It seems you're using Ubuntu where gVim isn't installed by default. Use this command to get it:

 $ sudo apt-get install vim-gnome

There's also vim-gtk which doesn't depend on Gnome, but since you're using gedit, you must be using one of the Gnome-based versions of Ubuntu.

Share:
26,232

Related videos on Youtube

user13107
Author by

user13107

Updated on September 18, 2022

Comments

  • user13107
    user13107 over 1 year

    I know that doing %y a will yank complete file into register a. However this doesn't help in pasting the file content in another text editor (say gedit). What alternative is there for doing so?

    • Admin
      Admin about 11 years
      Did you compile VIM with X support?
    • Admin
      Admin about 11 years
      Read in the file from gedit? At least that's what I'd do the other way around.
  • Bernhard
    Bernhard about 11 years
    I get the following error message when using your solution: E488: Trailing characters
  • user13107
    user13107 about 11 years
    :%!xclip didn't work. It just deleted all the text in the file in vi.
  • rush
    rush about 11 years
    @user13107 actually it works (all data are in clipboard), unfortunately all text is deleted. I'll put it the answer. Thanks.
  • user13107
    user13107 about 11 years
    Is there a command line alternative to middle mouse button?
  • rush
    rush about 11 years
    @user13107 use xclip -o for that.