Friday, 10 June 2011

Lost line breaks when copying from gedit to Evernote on Ubuntu - solution

I love Evernote. I work with Ubuntu so I have to use web based Evernote. The problem is that when I want to copy paste my txt files as notes in evernote I always lose the line breaks. I was very annoyed about that.

My solution is to use perl script fix-clipb.pl that works this way:
1. Select the text in gedit and copy it to your clipboard with ctrl+c
2. Run the script. You can create a launcher on your Gnome top panel.
3. Paste it in your Evernote note with ctrl+v.

How to configure it:
1. Install xclip first. Go to Ubuntu Software Centre and search for xclip, install it.
2. Create fix-clipb.pl file and save it somewhere:

#!/usr/bin/perl -w

@clipboardInput=`xclip -o -selection clipboard`;
$tmp_file = "/var/tmp/xclip.txt";

open OUT_FILE, ">$tmp_file"  or die("Error: cannot open file for write /var/tmp/xclip.txt\n");

foreach (@clipboardInput) {
    chomp;
    $_ =~ s/\t/    /g;
    print OUT_FILE "$_\n\r";
}
close (OUT_FILE);

system("xclip -selection clipboard -i $tmp_file");
system("rm $tmp_file");

3. Make the script runnable: chmod +x fix-clipb.pl
4. Add custom launcher to the script in your Gnome top panel.

Details what it does:
1. Gets your clipboard content with xclip.
2. Creates a temp file with line endings converted to Windows standard: \n\r (this is what Evernote treates as a line break). Additionally it replaces all tabs with 4 spaces as I noticed Evernote doesn't like tabs...
3. Exports temp file to your clipboard
4. Deletes the temp file.