Android and Linux

Thursday, May 27, 2010

Copy/paste between Linux and N1

Using my post about copy/paste in the Android terminal, it becomes easy to share clipboard contents between your phone and Linux.

First, you need a way to access the Linux clipboard from a terminal. I use either xclip or xsel. Both are old programs which always seem to work differently for each application, so this example uses both.

You need to set up ssh keys to login to your home computer from the phone. The general layout for using keys in a script is something like: ssh USER@IP -i KEYFILE. You can also briefly login to a computer and run a command over ssh by using: ssh USER@IP -i KEYFILE 'command' and that's what we'll be doing.

In order to get the computers clipboard contents, we'll use the command "xsel -p." Since xclip and xsel are utilities meant to interact with the X display, you may also need to export the display then unset the display in order to get a clean logout with ssh. For simplicity, I made a script called paste2n1 which can be ran with the ssh command.

#! /bin/sh
export DISPLAY=:0.0
xsel -p
unset DISPLAY


Now I can run this command to display the computer's clipboard on my phone:
ssh USER@IP -i KEYFILE 'paste2n1'

By piping it to the copy script I posted previously and running this command from gscript, I can copy the computer's clipboard contents to the phone with one tap of an icon on the phone.

ssh USER@IP -i KEYFILE 'paste2n1' | copy

To paste the phone's clipboard contents to the computer, I'll use xclip with this script named n1_to_comp on the computer:
#! /bin/sh
in=$(cat /dev/stdin) # or "in=$(cat -)"
export DISPLAY=:0.0
echo "$in" | xclip
unset DISPLAY


ssh supports most commands through pipes so you can send any text to the computer with the command: ssh USER@IP -i KEYFILE 'n1_to_comp'

You could use that as a flexible all-around paste command/script to paste any output to the comptuer such as: cat textfile | ssh USER@IP -i KEYFILE 'n1_to_comp'

Or use my previous command I called "clip" for pasting the phone's clipboard in the terminal for a one touch clipboard-to-clipboard transfer using gscript: clip | ssh USER@IP -i KEYFILE 'n1_to_comp'

These are just examples and instead of piping to different scripts, you could write a simple one-liner for each operation. I may write a single script with options to handle both operations if I get bored.

Followers