How to copy a line in vi and paste it to console?

6,830

Solution 1

GPM , "General Purpose Mouse" provides mouse functions in a console, most useful perhaps is cut and paste.

From the man page:

This package tries to be a useful mouse server for applications running on the Linux console. It is based on the "selection" package, and some of its code comes from selection itself. This package is intended as a replacement for "selection" as a cut-and-paste mechanism; it also provides additional facilities.

Additional features are outlined in the "Special commands" section

sudo apt-get install gpm

Generally it works out of the box, at least I have not had to manually configure it.

but a nice overview of how to configure it see http://www.cyberciti.biz/tips/howto-linux-configure-the-mouse-at-a-text-based-terminal-for-copy-and-paste-operation.html

or the man page

http://manpages.ubuntu.com/manpages/vivid/man8/gpm.8.html

Solution 2

In bash, it is possible to edit the current command in an editor, by pressing CtrlxCtrle.

So:

  1. Start with a new prompt, press CtrlxCtrle. This will open up a new, blank, editor, preferably Vim (I believe the editor is decided by the VISUAL and EDITOR variables).
  2. Open the file containing commands in a new tab or split.
  3. Copy the relevant commands over to the original buffer (probably named something like bash-fc-xxxxxxxx).
  4. Save and quit. Et Voila!

Solution 3

In vi there is a visual mode which allows you to visually select text. You can enter this mode by pressing v. Once you have entered this mode you can use the arrow keys to select the text you wish to copy and paste. Then use y to copy, p to paste (to your desired location), and finally you may use d to cut text (or delete it).

There is a special version of vim you can get which supports X and therefore allows access to the system clipboard. But as it is easiest not to have lots of them hanging around (as the default version does not have those extended capabilities), it is good to compile from source as this person suggested. So to do this first make sure that you have mercurial installed, and if not then install it:

sudo apt-get install mercurial

Once you are sure that that is installed get the compile-dependencies of vim:

sudo apt-get build-dep vim

Then get the source with:

hg clone https://vim.googlecode.com/hg/ vim_source

Finally we need to compile it:

cd vim_source
./configure \
    --enable-perlinterp=dynamic \
    --enable-pythoninterp=dynamic \
    --enable-rubyinterp=dynamic \
    --enable-cscope \
    --enable-gui=auto \
    --enable-gtk2-check \
    --enable-gnome-check \
    --with-features=huge \
    --with-x \
    --with-compiledby="Your Name <[email protected]>" \
    --with-python-config-dir=/usr/lib/python2.7/config
make && sudo make install

PLEASE NOTE: This will install it in /usr/local, so you need to be sure that it's in your PATH before /usr so that it's used instead of the default Ubuntu version.

Then you should be able to get this working with:

"+y

To copy to the system clipboard. And:

"+p

To paste from it.

Solution 4

I do not know if there is a global buffer in linux console.

I found a workaround. It is possible to copy & paste within vi or nano.

If some command, part of a command an or amended comand should be executed, I would copy it to end of file, then close the editor and run

tail -1 file.txt | bash

Some line of the file can be run by

sed -n <line_number> file.txt | bash

or

grep <pattern> file.txt | bash

But that does not quite answer my question. It is a kind of workaround.

Share:
6,830

Related videos on Youtube

Pilot6
Author by

Pilot6

Updated on September 18, 2022

Comments

  • Pilot6
    Pilot6 over 1 year

    There is a text file with bash commands.

    Is there a way to copy a line from that file using vi and paste it to console? It may be nano as well.

    It is console only server installation. No mouse to be clear.

    • Panther
      Panther almost 9 years
      Have you tried GPM ? cyberciti.biz/tips/…
    • Pilot6
      Pilot6 almost 9 years
      @bodhi.zazen Yes!!! That's what I looked for. If you convert it to answer, I'll accept it.
  • Pilot6
    Pilot6 almost 9 years
    The problem is that this buffer is only inside vi. If you exit vi, you can't copy it to console. The question was: is there any general buffer in console?
  • Admin
    Admin almost 9 years
    @Pilot6: I don't think that I could have put it in a better way, so I have added a quote to my answer. Hope it helps.
  • Pilot6
    Pilot6 almost 9 years
    Do you understand this text? I don't ;-) And what do X and Windows do there? In console there is no X at all.
  • Admin
    Admin almost 9 years
    @Pilot6: No... That's why I couldn't reword it! :D
  • Admin
    Admin almost 9 years
    @Pilot6: I just assumed that you would know how to use vi and vim better than me and would understand...
  • Admin
    Admin almost 9 years
    Isn't the whole point of the console that you can escape from using the horrible mouse and GUI, and replace all that by a nice CLI?
  • Panther
    Panther almost 9 years
    @ParanoidPanda - Not really, although some people use it for that. It is a basic, non graphical interface and useful on headless servers or as a fall back or rescue mode on systems where the graphical interface fails. Regardless, the question is how to obtain copy-paste functionality in the console which is obviously useful. GPM also provides additional functionality such as rebooting when the keyboard fails, see the man page.
  • Admin
    Admin almost 9 years
    @Pilot6: Ok, I have updated my question, does it help more now?
  • Pilot6
    Pilot6 almost 9 years
    It does not work out of the box. You need to start it with sudo /etc/init.d/gpm start.
  • Pilot6
    Pilot6 almost 9 years
    I will try that too.
  • Panther
    Panther almost 9 years
    Do you have to start it manually with every reboot ? If so I will update the answer.
  • A.B.
    A.B. almost 9 years
    Are you a vi user? =)
  • Admin
    Admin almost 9 years
    @A.B.: If you were talking to me, then yes sort of. I don't really use vi very much though, instead I use vim, which is just an extension to vi so I guess that I am still sort of using it...