Posted by: rakitha | March 23, 2011

How to Work and Edit Files on a Remote Host – screen & TRAMP

This is very important when you work remotely by logging in to a host. I usually logged into the host through a ssh connection and opened emacs editor to work on. Problem is if the ssh connection looses you will lost all your opened emacs editors + what ever you had on the remote host.

I found two solutions to this.

1. Use a virtual terminal, screen instead of regular terminal or xterm.

2. Use TRAMP to connect and open files on a local emacs editor.

Virtual Terminal, screen

Basically screen is available by default in any Linux distributions. I’m working on both Ubuntu and Red Hat Enterprise Edition distributions and they both had screen installed by default. If you want to install screen, you can find compiled or source code for the screen for most of Linux distributions. Simple use either,

yum install screen

or

apt-get install screen

When you are ready with screen installed in the host, you can configure the screen to make it little user-friendly. Therefore create the file

~/.screenrc

and then copy following entries to this file,

#To have a status bar at the bottom of the screen
hardstatus alwayslastline
hardstatus string '%{= kG}[ %{G}%H %{g}][%= %{=kw}%?%-Lw%?%{r}(%{W}%n*%f%t%?(%u)%?%{r})%{w}%?%+Lw%?%?%= %{g}][%{B}%Y-%m-%d %{W}%c %{g}]'#To enable screen terminal scrolling
# source http://www.cmdln.org/2007/07/31/fix-terminal-scrolling-when-inside-gnu-screen/
termcapinfo xterm|xterms|xs|rxvt ti@:te@
# Default screens
screen -t shell1	0
screen -t shell2	1
screen -t server	2	ssh user@host

The last 3 lines will create three default virtual terminals when you run the screen command. The very last line the terminal will make a ssh connection.

Now you have the virtual terminal setup and ready. Now what you have to get used to is, when you logged in to your remote host execute the screen in the terminal ,

screen

This will create virtual terminals you have defined in the configuration file .screenrc. You will see all the terminal available in the status bar.
Now in the screen terminals, all the commands start with ctrl+a (C-a), then followed with the single or double key. I will go through several important command in screen and you can find really useful web entries for more information.
To switch between terminals,

C-a space-bar
C-a num

where the num is the numerical number of the virtual terminal. You can see this in the screen status bar.

To list all available terminals,

C-a w

To list all the commands,

C-a ?

To detach from the screen back to the terminal where it was called,

C-a d

To exit from virtual terminals belonging to the current screen session,
1. The standard command is,

C-a \ or C-a C-\

2. in Red hat distributions I had to use,

C-a :quit

Which is not given in the standard command help.

You can kill single virtual terminal by using the command,

C-a K

Then the most important part is you can detach your screen session and log out of the remote host. Then when you need to work again just log in to the remote host and type,

screen -ls

This will show list of screen sessions available. To go back to your working screen session use the command,

screen -r [session]

where the [session] is listed when you typed screen -ls

Enjoy the world of virtual terminal with screen

Edit Files on a Remote Host using emacs+TRAMP

Two of the most frustrating issue when working in a remote host through a ssh connection is if you lose the network connection, you will lose all your work and if you are working in a crappy network, then opening a GUI based emacs terminal would be a pain. If you configure the TRAMP in the emacs, both of these pressing networking issues would not matter anymore. emacs editor is a local object running in you local computer. The tramp will keep a local copy of the remote file and it will only access the remote copy when you are saving it. So no more worries. Here is how to configure this,

First copy following lines to the ~/.emacs file,

(require 'tramp)
(setq tramp-default-method "scp")

Then in a terminal open the remote file using,

emacs /user@host.com:/[abstract file directory]/file-name

The emacs through tramp will connect to the remote host and get a local copy to edit. When you save you may need to enter password repeatedly, which could be a hassle. But you could go around this issue if you use ssh-agent.

I use Ubuntu 10.10 which already had TRAMP installed. So all I had to do was add above set of line to the emacs configuration file. But if you need to install and configure TRAMP from the scratch, please follow the instructions in TRAMP User Manual.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: