3

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
  • Have you tried GPM ? cyberciti.biz/tips/…
    – Panther
    Jul 24 '15 at 3:43
  • @bodhi.zazen Yes!!! That's what I looked for. If you convert it to answer, I'll accept it.
    – Pilot6
    Jul 24 '15 at 9:21
4

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

4
  • 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?
    – user364819
    Jul 24 '15 at 13:32
  • @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.
    – Panther
    Jul 24 '15 at 13:41
  • It does not work out of the box. You need to start it with sudo /etc/init.d/gpm start.
    – Pilot6
    Jul 24 '15 at 14:08
  • Do you have to start it manually with every reboot ? If so I will update the answer.
    – Panther
    Jul 24 '15 at 14:25
3

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!
2

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 <youremail@domain.com>" \
    --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
  • 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?
    – Pilot6
    Jul 23 '15 at 22:09
  • @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.
    – user364819
    Jul 23 '15 at 22:22
  • Do you understand this text? I don't ;-) And what do X and Windows do there? In console there is no X at all.
    – Pilot6
    Jul 23 '15 at 22:25
  • @Pilot6: No... That's why I couldn't reword it! :D
    – user364819
    Jul 24 '15 at 13:30
  • @Pilot6: I just assumed that you would know how to use vi and vim better than me and would understand...
    – user364819
    Jul 24 '15 at 13:31
1

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

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.