LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Desktop
User Name
Password
Linux - Desktop This forum is for the discussion of all Linux Software used in a desktop context.

Notices


Reply
  Search this Thread
Old 05-04-2012, 02:09 AM   #1
it_tyyppi
LQ Newbie
 
Registered: May 2012
Posts: 7

Rep: Reputation: Disabled
Preserve colors when copy/pasting from terminal


Hi

I want to copy colored output (ansi colors?) from terminal to clipboard. I frequently access MikroTik routers via ssh and want to paste the output from the router to LibreOffice for documentation purposes. I think that preserving the colors highly increases the readability of the configurations.

The terminals I've tried so far do show the colors but copy only plain text to clipboard.

In Windows I can use PuTTY/MinTTY to get RTF? formatted text to the clipboard and paste it to LibreOffice with the colors in place. I guess MinTTY does some kind of ANSI-color to RTF conversion on the fly?

Is there a way for doing this in Linux?

Thank you
it_tyyppi
 
Old 05-04-2012, 02:18 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,792

Rep: Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304
I think the clipboard of the X windows system cannot handle colors, attributes, just plain text. From the other hand you can use printscreen or some similar app to take a screenshot.
 
Old 05-04-2012, 02:27 AM   #3
it_tyyppi
LQ Newbie
 
Registered: May 2012
Posts: 7

Original Poster
Rep: Reputation: Disabled
PrintScreen is not an option, I would rather use uncolored text so I can copy paste the configs when I need to use them again.

Would it be possible e.g. to use screen to capture the color formatted output to a file and somehow convert that to something LibreOffice understands?

-it_tyyppi
 
Old 05-04-2012, 02:35 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,792

Rep: Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304
maybe this helps:
http://stackoverflow.com/questions/5...s-without-usin
 
Old 05-04-2012, 03:43 AM   #5
it_tyyppi
LQ Newbie
 
Registered: May 2012
Posts: 7

Original Poster
Rep: Reputation: Disabled
The solutions in pan64's link above would probably work fine for source code but my problem is different.

I want to capture (not re-create) the exact coloring created by MikroTik's RouterOS from the ssh/telnet session. I've tested some RouterOS syntax coloring schemes but they are not complete. It is probably impossible to color router config syntax perfectly due to the unorganized nature of the syntax compared to normal source code.

AFAIK the colors shown in the terminal are ANSI codes embedded in the plain text content. I'm sure there's a way to somehow capture those color codes so that they could be used to create documentation with colored router config syntax.
 
Old 05-04-2012, 03:50 AM   #6
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,792

Rep: Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304
Yes, you can try to catch those color codes, not from the screen but by redirecting stdout.
Maybe this perl module can help you:
http://stackoverflow.com/questions/2...-codes-to-html
 
1 members found this post helpful.
Old 05-04-2012, 04:17 AM   #7
it_tyyppi
LQ Newbie
 
Registered: May 2012
Posts: 7

Original Poster
Rep: Reputation: Disabled
The perl module certainly seems to do the trick. Thanks for the link pan64.
However the ease of use of this method is far from the usability in Windows (where copying from MinTTY to LibreOffice via clipboard preserves colors).

I can copy/paste colored text from Firefox to LibreOffice in Ubuntu 12.04. This would suggest that in Linux/Gnome/X.org some kind of support for copying colored text via the clipboard exists.

So the problem is that there is no terminal for Linux that can change the ANSI codes to something that the clipboard understands?
 
Old 05-04-2012, 04:26 AM   #8
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,792

Rep: Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304
it depends on your X server, terminal I think and the GUI. Here you can find some other tricks, maybe one of them is suitable for you:
http://www.debian-administration.org/articles/565
 
Old 05-04-2012, 05:10 AM   #9
it_tyyppi
LQ Newbie
 
Registered: May 2012
Posts: 7

Original Poster
Rep: Reputation: Disabled
Using xclip or xsel as mentioned in http://www.debian-administration.org/articles/565 would also require piping stdout so it is no simpler than the solutions already mentioned earlier. It is not possible to use the mouse to select the area that should be copied to clipboard.

Using these tools I could probably pipe/redirect the whole session to a HTML file, open it in Firefox and copy/paste what I need from Firefox to LibreOffice:
Perl module: http://search.cpan.org/~nuffin/HTML-...ML/FromANSI.pm
Binary tool: http://ziz.delphigl.com/tool_aha.php

How would one go about piping/redirecting ssh output to two places? Meaning that the output goes both to the terminal window (stdout) and to a file or the app converting the ANSI to HTML?

I guess oldskool guru types don't need syntax highlighting (or GUIs for that). Still. I find it hard to believe that simply selecting colored stuff from the terminal window using a mouse and pasting it somewhere preserving the colors is impossible in Linux. Is it so that the terminals output only plaintext to clipboard or that clipboard (or LibreOffice) doesn't understand the ANSI codes?
 
Old 05-04-2012, 06:22 AM   #10
it_tyyppi
LQ Newbie
 
Registered: May 2012
Posts: 7

Original Poster
Rep: Reputation: Disabled
I figured it out myself but still thinking that there has to be a simpler way to do this (e.g. terminal that supports colored text with Linux clipboard, clipboard tweaks etc.).

To redirect stdout to multiple places I can use the tee command.
The aha (Ansi HTML Adapter) command was available in Ubuntu's default repository.

So here's what I did:
#install aha
sudo apt-get install aha

#access server with ssh, redirect output to both terminal and aha
#aha creates outfile.html that includes the syntax highlighting
ssh username@server | tee >(aha > outfile.html)
#--run router commands here
#--e.g. "export compact" in RouterOS outputs current configuration
#--quit ssh session

#open outfile.html in Firefox
firefox outfile.html &

I can now copy the colored output from firefox to LibreOffice writer via Ubuntu 12.04 clipboard.

Last edited by it_tyyppi; 05-04-2012 at 06:31 AM.
 
Old 05-04-2012, 06:24 AM   #11
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,792

Rep: Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304
Redirecting output to two places will go with tee. You can use pipe, file, stdout, what you want.

Syntax highlight is useful also for an oldschool guru. Maybe they do not like GUIs, or they prefer vi, but they surely like to see the code in colors.
 
Old 05-04-2012, 06:25 AM   #12
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,792

Rep: Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304
Happy with solution ... mark as SOLVED
(located in the "thread tools")
 
Old 05-04-2012, 06:40 AM   #13
it_tyyppi
LQ Newbie
 
Registered: May 2012
Posts: 7

Original Poster
Rep: Reputation: Disabled
Although I can live with this approach, I think this is more of a workaround than a solution. If this would be the solution then "Linux - Desktop" would be the wrong forum for this thread as this is a command line solution for a desktop problem.

I'm sure that it is somehow possible to select text from terminal using the mouse, copy the selection and paste it somewhere preserving the colors. This is possible in Windows using MinTTY/PuTTY.

Last edited by it_tyyppi; 05-04-2012 at 07:03 AM.
 
Old 05-04-2012, 07:07 AM   #14
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,792

Rep: Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304
I totally agree with you. This is not a nice solution, but a working one. I think it is really hard because it depends on the terminal, depends on the shell running inside, depends on the session manager and the x server also (not to speak about the other softwares, like firefox). It works on windows based apps because they have much better clipboard support and all of them have the same built-in solution.
 
Old 05-04-2012, 07:21 AM   #15
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,792

Rep: Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304
Just a tip, maybe this can simplify it:
https://addons.mozilla.org/en-US/firefox/addon/firessh/
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Can cpio copy symlinks and preserve the modification time? catkin Linux - Newbie 5 08-17-2009 10:17 AM
Copy and pasting between terminals enortham Linux - Software 4 12-27-2005 09:45 AM
copy pasting from X app to vim Furlinastis Linux - General 3 12-05-2005 09:05 PM
copy pasting in emacs wogga Linux - Software 7 06-16-2004 03:12 PM
Questions on copy+pasting, gdesklets and more! Tyir Linux - General 2 03-23-2004 02:52 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Desktop

All times are GMT -5. The time now is 01:21 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration