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

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.

2

4 Answers

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

or the man page

4

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!

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 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 <>" \ --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.

9

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.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like