End Point

News

Welcome to End Point's blog

Ongoing observations by End Point people.

Update Your (Gnu) Screen Config on the Fly

An Indispensable Tool

I use Screen constantly in my work at End Point. It is an indispensable tool that I would not want to operate without. It's so handy to resume where I left off after I've detached or when my connection drops unexpectedly. This is likely preaching to the choir but if you are not already using Screen and/or tmux, start now.

The Scenario

I often find myself in the following situation:

  1. SSH into a server
  2. Fire up a new Screen session
  3. Create several windows for editing files, tailing logs etc
  4. Realize the default Screen configuration is inadequate or does not exist.
  5. Facepalm \O/

While my needs are fairly minimal, I do like to bump up the scrollback buffer and display the list of windows in the status line.

Screen example

There are a couple of options at this point. I could put up with the default / non-existent configuration or create a config file and manually re-create the session and all of the windows to pick up the configuration changes. Neither of these options was desirable. I wanted to be able to update the configuration and have all of the existing windows pick up the changes. After asking around a little I ended up taking a look at the manual and discovered the `source` command.

Use the source (command)

The source command can be used to load or reload a Screen configuration file. It can be invoked from inside a Screen session like so:

C-a :source /absolute/path/to/config_file

It is important to note that you must use the absolute path to the config file. There are exceptions which can be found in the Screen man pages but I found it easier to just use the absolute path. Once the source command has been issued, the configuration will be applied to all existing windows! This was exactly what I was looking for. Armed with this information I copied my local .screenrc to the system clipboard, created a new config file on server and applied it to my session using the `source` command.

Works with tmux too

I like to use tmux as well and was happy to find it had a similar feature. The source-file command (`source` is aliased as well) is invoked in the exactly the same way:

C-prefix :source /absolute/path/to/config_file

After issuing the source-file command, all of the windows and panes in the current session will pick up the configuration changes.

Changing the Default Directory

Another related issue I often run into is wishing I had started my Screen or tmux session in a different directory. By default, when you start a Screen or tmux session, all new windows (and panes) will be opened from the same directory where Screen or tmux was invoked. However, this directory can be changed for existing sessions.

For Screen, the chdir command can be used:

C-a :chdir /some/new/directory

In tmux, the default-path command can be used:

C-prefix :default-path /some/new/directory

After issuing the chdir or default-path commands, all new windows and panes will be opened from the specified directory.

I hope this has been helpful — feel free add your own Screen and tmux tips in the comments!


7 comments:

Jon Jensen said...

I'll have to remember the ability to source configs in all windows!

One correction: In tmux the default-path if not otherwise configured comes from the current working directory of the window you're in when you spawn a new window! When you first start it that seems the same as screen but as you cd around your filesystem and open new windows you can see the difference. (This assumes your OS, tmux package, or something else didn't change the default.)

From the tmux man page section on default-path:

If empty (the default), the working directory is determined from the process running in the active pane, from the command line environment or from the working directory where the session was created. If path is "$HOME" or "~", the value of the HOME environment variable is used. If path is ".", the working directory when tmux was started is used.

Greg Davidson said...

Thanks Jon, that's good to note. It could be my terminal emulator (iTerm2) but new windows and panes open in the directory tmux was started in. Unless I use `:set-option default-path /some/path` to change that.

Jon Jensen said...

Are you sure? What version of tmux? (Try `tmux -V`.) I'm running 1.6. Is anything about default-path shown when you run:

:show-options -s

Based on my experience and the docs for version 1.6, you should be able to open tmux, `cd /tmp`, then ^B c to start a new window and find it's started in /tmp. Does that not work for you?

I don't want to belabor the point, but it'd be nice to get to the bottom of any differences out there!

Greg Davidson said...

I have tmux 1.6 and there is nothing about the default-path when I view `:show-options -s`. I cd'ed into /tmp and from there created a window and a pane. Both were using the directory I started tmux in (not /tmp)!

Not sure why but that's been my experience.

Jon Jensen said...

Greg, does your tmux manpage not spell out the functionality like mine? "If empty (the default), the working directory is determined from the process running in the active pane" is pretty clear, and yours is not behaving that way, so if it's documented that way it seems like a bug or undocumented patch in the Mac OS X build of tmux.

Greg Davidson said...

Jon, I upgraded to tmux 1.7 today and it behaves the way you and the manpage describe.

Jon Jensen said...

Strange, but good to hear at least that new version is consistent with the docs!