UNIX - How to copy and paste between different bash windows with files opend with VI?

372

Solution 1

You cannot do it unaided in a standard generic way with the vim you have.

Firstly make sure you have a good version compiled and installed on your distro.

Then I would suggest either using gvim with separate windows, or using vim -o to open multiple files and just between the frames using ^W^W to switch and then copy and paste.

Solution 2

It's no wonder that it didn't work for you. You're mis-spelling the commands. It's not shift-8-y-y. Nor is it double_quote-p. The commands are:

  • Yank current line to X11 clipboard: "+yy
  • Paste from X11 clipboard: "+p
  • Yank current line to X11 (primary) selection: "*yy
  • Paste from X11 (primary) selection: "*p

Modify the motion part according to taste. vim has to know that an X server is there, of course. So you'll have to explicitly tell it where the X server is and what display on that server to use if you aren't running it from within an X session where it implicitly inherits the DISPLAY environment variable and so forth.

Solution 3

You may want to install the "general purpose mouse" (gpm) package, which allows the use of mouse copy-and-paste without a GUI.

Solution 4

The vi that's included in many GNU/Linux distributions is a tiny version of Vim which has been compiled without many of Vim's features, including the interface to the X Window System's clipboard. Even their terminal-mode vim program is often built without support for X. Without support for X, your vi and vim will be unable to access the clipboard and you will be unable to copy and paste between different instances of the program.

One solution to that is to run the gvim program in terminal mode by using the -v option, as

$ gvim -v your_file

To avoid typing that, or forgetting to type that, every time, you could add these aliases to your ~/.bashrc file:

alias vim='gvim -v'
alias vi='gvim -v'

Once you're using a version of Vim compiled with support for X, you can use the commands described in JdeBP's answer. You can also read about those command's in Vim's internal user manual and reference manual by executing

:help 04.7
:help 09.3

Solution 5

Since you seem to be using the console (no X windows, so many of the other answers are not relevant) you might just edit the multiple files in one vim session. Try :split ... to edit the two files at once and use vim's cut buffers to move text between them.

Alternatively you can use gpm, and cut and past between consoles.

Share:
372

Related videos on Youtube

user3287740
Author by

user3287740

Updated on September 18, 2022

Comments

  • user3287740
    user3287740 over 1 year

    I am trying to fetch all the calls in the android call log using this code:

    ArrayList<Call> list = new ArrayList<Call>();
        Cursor cursor;
        // The fields we want to select from the internal database. Setting this
        // to null is equivalent to * (e.g., SELECT * FROM...)
        String[] projection = {CallLog.Calls.NUMBER, CallLog.Calls.DATE, CallLog.Calls.TYPE};
        String sortOrder = CallLog.Calls.DATE + " desc";
        int numberCol = 0;
        String contactName;
        String contactNumber;
        String contactDate;
        int callType;
        Call phone_call;
    
        // Query the CallLog table, selecting only the number and date then sorting it by the date.
        cursor = context.getContentResolver().query(CallLog.Calls.CONTENT_URI, projection, null, null, sortOrder);
    
        numberCol = cursor.getColumnIndex(CallLog.Calls.NUMBER);
    
        if(cursor.moveToFirst()) {
    
            while(cursor.moveToNext()) {
              //do stuff
            }
        }
    
        cursor.close();
    
        return list;
    

    This works, for most calls, except for the top one (the newest, since I order by date, descending).

    How could this be?

  • Marius
    Marius almost 13 years
    this will not work with larger buffers... And when pasting it may reformat as-you-type undesirably.
  • xtian
    xtian almost 13 years
    This is running on a laptop which boots to the terminal. Are you referring to what garyjohn called the "X Windows system clipboard" when you refer to the X server?
  • xtian
    xtian almost 13 years
    And, I tried the commands "+yy and "*yy; when I type "+ together then y I get "E78 unknown mark" error; If I type "+y then y I get E20 Mark not set error. I am close?
  • evaryont
    evaryont almost 13 years
    If you're worried about the formatting, vim in vi emulation mode may not have the 'paste' option, but still has the = operator.
  • JdeBP
    JdeBP almost 13 years
    You're still mis-spelling the commands. I wrote " yet you're typing '. What garyjohn says about different binaries compiled with and without X support is important, too.
  • xtian
    xtian almost 13 years
    Then what is *?
  • xtian
    xtian almost 13 years
    To use the X system's clipboard wouldn't X need to be running, then? If I'm using X I don't suffer this problem, I can simply use the mouse :P
  • garyjohn
    garyjohn almost 13 years
    Yes. When you wrote about opening a new terminal and using different bash windows, I overlooked that you were using Alt-F2 from a console to open another console and assumed that you were opening terminal windows in some sort of X window manager. My mistake. As for the mouse, I try to avoid using it, so I copy and paste between my various Vim windows with the yank and put commands.