[SOLVED] Preserve colors when copy/pasting from terminal
Linux - DesktopThis forum is for the discussion of all Linux Software used in a desktop context.
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
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?
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.
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?
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.
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?
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:
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.
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.
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.