Log output for screen

The one thing that annoys me when I use screen is that I can’t scroll up after a long output has printed. To log outputs, add the following near the top (required!) of your screenrc file ($HOME/.screenrc):

# auto-log http://www.cmdln.org/2007/07/20/automatic-session-logging-and-monitoring-with-gnu-screen-for-the-paranoid/
## search for "STRING ESCAPES" in the manpage
# logfile /tmp/screen_%Y-%m-%d-%c:%s_%n.log
## not using above because every window gets its own time too.  rather use session name; need 4.01.00devel (GNU450e8f3) 2-May-06 and above
logfile /tmp/screen-%S-%n.log
deflog on

For example, my rc file now looks like:

escape ^Oo
# auto-log http://www.cmdln.org/2007/07/20/automatic-session-logging-and-monitoring-with-gnu-screen-for-the-paranoid/
## search for "STRING ESCAPES" in the manpage
# logfile /tmp/screen_%Y-%m-%d-%c:%s_%n.log
## not using above because every window gets its own time too.  rather use session name; need 4.01.00devel (GNU450e8f3) 2-May-06 and above
logfile /tmp/screen-%S-%n.log
deflog on
##escape \56\56 ##ascii octal http://www.robelle.com/library/smugbook/ascii.html http://www.ncsu.edu/it/mirror/ldp/LDP/LGNET/147/appaiah.html Use C-. for escape
## following for starting screen at 1 instead of 0
## http://www.linuxquestions.org/questions/linux-software-2/gnu-screen-start-window-numbering-at-1-keep-window-number-0-from-ever-being-used-772580/
bind c screen 1
bind 0 select 10
screen 1
select 1

When I originally had the log options at the bottom, logging did not work for me.

Finally, you can use the %S string escape to refer to the session name in screen 4.01.00devel (GNU450e8f3) 2-May-06 and after. It might be wise to install the git version of screen.

Compiling bleeding edge version of screen

sudo apt-get remove screen
git clone git://git.savannah.gnu.org/screen.git
cd screen
./mktar
tar xvf screen-*.tar.gz
cd screen*/
./autogen.sh
./configure
make
sudo make install

Byobu, an enhanced screen

I use screen on a daily basis. I recently discovered byobu, an enhanced add-on for screen that makes it even more customizable. A screencast can be found here. After reading some descriptions of byobu here and here, I re-discovered screen’s ability to split screens. I’ll probably begin to use this feature. Not sure if I’ll need to use byobu. I do like the row on the bottom describing the system resources.

escreen instead of elscreen for screen-like features in emacs

I’ve been using elscreen for screen-like features in emacs the last couple of years. However, I have a few complaints. Elscreen has issues when used with emacsclient -c: a new frame might not be created from the -c argument, which messes up my current screen in emacs, and the “Opening Server Files Always in a New Frame” tip on this did not resolve this issue perfectly with elscreen. Also, when used with emacs’ Desktop mode, I get an error when launching emacs with an Rnw file (Rweave, or Sweave) is in the file list; the launch actually does not load all files, and I have to exit and delete my desktop file (which I’ll lose the list of files).

I discovered escreen from this post a long time ago, and thought escreen was pretty lightweight and worked pretty well. However, I never switched to it because it didn’t have “tabs” for me to know which screen I was at. I didn’t realize the very same post had customizations that would show which screen you are at. I’ve copied his customizations, and added a few more of my own, which I’ll list here:

 <pre class="example">;; escreen is simpler than elscreen...but elscreen spawns new 0-9 set of screens for a new frame whereas escreen does not

;; it is better than elscreen because: elscreen has issues when using with emacsclient -c (does not always create new frame, messes up existing frame); in escreen, each screen has its own ring of recently visited files (good!) ;; http://blog.tapoueh.org/news.dim.html#%20Escreen%20integration ;; http://www.splode.com/~friedman/software/emacs-lisp/#ui (load “escreen”) (escreen-install) (setq escreen-prefix-char “\C-z”) ;; http://www.macs.hw.ac.uk/~hwloidl/cool-el.html (global-set-key escreen-prefix-char ‘escreen-prefix) ;; add C-\ l to list screens with emphase for current one (defun escreen-get-active-screen-numbers-with-emphasis () “what the name says” (interactive) (let ((escreens (escreen-get-active-screen-numbers)) (emphased “”))

(dolist (s escreens) (setq emphased (concat emphased (if (= escreen-current-screen-number s) (propertize (number-to-string s) ;;’face ‘custom-variable-tag) ” “) ‘face ‘info-title-3) ;;’face ‘font-lock-warning-face) ;;’face ‘secondary-selection) (number-to-string s)) ” “))) (message “escreen: active screens: %s” emphased)))

(global-set-key (kbd “C-\ l”) ‘escreen-get-active-screen-numbers-with-emphasis)

(defun escreen-goto-last-screen-dim () (interactive) (escreen-goto-last-screen) (escreen-get-active-screen-numbers-with-emphasis))

(defun escreen-goto-prev-screen-dim (&optional n) (interactive “p”) (escreen-goto-prev-screen n) (escreen-get-active-screen-numbers-with-emphasis))

(defun escreen-goto-next-screen-dim (&optional n) (interactive “p”) (escreen-goto-next-screen n) (escreen-get-active-screen-numbers-with-emphasis))

(define-key escreen-map escreen-prefix-char ‘escreen-goto-last-screen-dim)

(defun escreen-create-screen-dim () (interactive) (escreen-create-screen) (escreen-get-active-screen-numbers-with-emphasis))

(defun escreen-kill-screen-dim () (interactive) (escreen-kill-screen) (escreen-get-active-screen-numbers-with-emphasis))

(add-hook ‘escreen-goto-screen-hook ‘escreen-get-active-screen-numbers-with-emphasis)

(define-key escreen-map “c” ‘escreen-create-screen-dim) (define-key escreen-map “k” ‘escreen-kill-screen-dim)

;; (global-set-key (kbd “C-]”) ‘escreen-goto-next-screen) ;; (keyboard-translate ?\C-[ ?\H-[) ;; (global-set-key (kbd “H-[“) ‘escreen-goto-prev-screen) (global-set-key (kbd “C-]”) ‘escreen-goto-next-screen-dim) (keyboard-translate ?\C-[ ?\H-[) (global-set-key (kbd “H-[“) ‘escreen-goto-prev-screen-dim)

Whenever I create/kill/visit a screen, the minibuffer will always show the list of screens available, with emphasis on the current screen. I like escreen because it does not have the issues I listed before with elscreen. Also, in escreen, each screen has its own ring of recently visited files, so that if I move to a different screen and edit a buffer, move back to my original screen, kill the buffer, the recent buffer from the other screen will not be the default screen on this current screen.

I like escreen!

StumpWM

Since Canonical decided to ship Ubuntu with Unity as opposed to GNOME as the default desktop in their next release, I decided to take a look at StumpWM again.

Although I like keyboard driven apps like emacs, conkeror, and screen, GNOME with Tracker + deskbar-applet (similar to Mac OS X’s Spotlight) has been sufficient; I think any stable Linux desktop is sufficient for me since they’re all very configurable by default (keyboard shortcuts!). The need for a tiling window manager doesn’t apply to me since 1) I work on my laptop/netbook (10in-13in), so I usually have windows maximized; 2) I’m pretty fast with Alt-Tab; and 3) non-minimal window manangers have certain services all set up, such as sound/video control (with shortcuts), network manager, etc. Basically, having convenient applets, a quick way to move between windows, and a quick way to launch apps (think Mac OS X’s Spotlight) makes me happy.

Nonetheless, I toyed with StumpWM again but decided I’m not ready for it yet. Maybe I will be ready the next time when I’m using a bigger screen. I’ll outline my trial for documentation.

I followed this to set up StumpWM on Ubuntu. This screencast gives a good overview of the powers of StumpWM.

Compile

sudo apt-get install sbcl sbcl-doc ## common lisp compiler
sudo sbcl
## enter following in sbcl prompt
## select [SKIP-GPG-CHECK] when asked. Have to do this a few times
(require 'asdf)
(require 'asdf-install)
(asdf-install:install 'clx)
(asdf-install:install 'cl-ppcre)
(quit)
## more install
sudo apt-get install xorg-dev
git clone git://git.savannah.nongnu.org/stumpwm.git ## use latest version
cd stumpwm
autoconf
./configure
make
sudo make install
sudo make install stalonetray ## for docking applets from GNOME

Add StumpWM as an entry in GDM Sessions Menu

sudo emacs -q -nw /usr/share/xsessions/stumpwm.desktop
## add following
[Desktop Entry]
Encoding=UTF-8
Name=StumpWM
Comment=This session logs you into StumpWM (a minimalistic window manager)
Exec=/usr/local/bin/stumpwm
Icon=
Type=Application

Restart computer and choose StumpWM when logging in.

~/.stumpwmrc

;; -*- lisp -*-
(in-package :stumpwm)

;mode line
(stumpwm:set-fg-color "green")
(stumpwm:set-bg-color "black")

;startup apps
(stumpwm::run-shell-command "stalonetray")
(stumpwm::run-shell-command "nm-applet")
(stumpwm::run-shell-command "gnome-volume-control-applet")
(stumpwm::run-shell-command "gnome-power-manager")
(stumpwm::run-shell-command "system-config-printer-applet")
(stumpwm::run-shell-command "dropbox start -i")
(stumpwm::run-shell-command "gnome-power-manager")
;; (stumpwm::run-shell-command "")
;; (stumpwm::run-shell-command "")

;keybinding
(define-key *top-map* (kbd "M-Tab") "pull-hidden-next")
(define-key *top-map* (kbd "M-ISO_Left_Tab") "pull-hidden-previous") ;; shift
(define-key *top-map* (kbd "M-]") "pull-hidden-next")
(define-key *top-map* (kbd "M-[") "pull-hidden-previous")
(define-key *top-map* (kbd "s-Tab") "fnext")
(define-key *top-map* (kbd "s-]") "gnext")
(define-key *top-map* (kbd "s-[") "gprev")
(define-key *top-map* (kbd "s-b") "move-focus left")
(define-key *top-map* (kbd "s-n") "move-focus down")
(define-key *top-map* (kbd "s-p") "move-focus up")
(define-key *top-map* (kbd "s-f") "move-focus right")
(define-key *top-map* (kbd "M-F4") "kill")
(define-key *top-map* (kbd "s-s") "fullscreen")
(define-key *top-map* (kbd "M-F2") "exec")
(define-key *top-map* (kbd "s-SPC") "exec")


(define-key *root-map* (kbd "M-F") "fullscreen")

I want to try xmonad next when I have time. My verdict now is to stay with GNOME.

using R + ess-remote with screen in emacs

Dear list,

I brought up this issue before but a good solution never arised: being able to use screen on a remote server (so if something goes wrong on my side I can always resume that R session) inside of emacs in order to utilize ESS. The closest thing I found to a good work flow was to use ansi-term or multi-term and copying and pasting code in Emacs (ESS only worked nicely with shell-mode; reason at the end). Some would advise to use screen and open emacs inside of that screen, and voila, you have the luxuries of screen (attaching the session anywhere) and emacs+ESS (keybindings, etc). However, I prefer to use one main emacs session on my laptop/netbook (all my configurations are there), where I can have multiple projects and multiple R jobs opened at once.

I would like to share what I have working (for the time being), with the help of Michael Zeller in case others are interested.

  1. Place the following in .emacs:
  <pre class="src src-sh">;; used to send screen keybindings to shell<span style="color: #00ffff;"> in</span> emacs

(define-key shell-mode-map (kbd “C-l”) (lambda (seq) (interactive “k”) (process-send-string nil seq))) (define-key inferior-ess-mode-map (kbd “C-l”) (lambda (seq) (interactive “k”) (process-send-string nil seq)))

  1. In xterm (or the likes), ssh to the remote server and start screen. Detach it. (Need to do this first as starting the initial screen in emacs shell-mode becomes very ugly with the printing; resuming the same screen session also becomes messy in xterm)
  2. In emacs, M-x shell. Set:
  <pre class="src src-sh">$ <span style="color: #eedd82;">TERM</span>=xterm <span style="color: #ff4500;">## </span><span style="color: #ff4500;">give clearing capabilities to shell-mode</span>

  1. ssh to remote server. screen -r to resume the screen session. start R. M-x ess-remote.
  2. Send R code from R source files like before!
  3. To detach or do anything screen related, precede EACH keybinding with C-l. For example, C-a C-d to detach will now be C-l C-a C-l C-d. Yes this is cumbersome, but I don’t imagine screen keybinding to be used much at this stage since we are developing and debugging R code for say a simulation study =].

I would also like to note (for archival reasons) that ess-remote does not work with ansi-term and multi-term because of the inferior-ess-mode command, which stems from comint-mode and inferior-ess-send-input. If you remove this command in ess-remote, you don’t get an error but u can only send one line of code at a time from the R file.

Hope this helps someone out there.

Run screen in emacs with ansi-term (combine this with emacs + ess + remote R)

This is actually an update to this post, but since I discovered a few more things, I’ll write a new post. To run screen within a shell buffer in emacs, I tried M-x shell and fired up screen (ditto with M-x term). It gave me this error: Clear screen capability required. I found the solution to this here. To fix this issue, do M-x ansi-term (use /bin/bash when asked of course). screen now works inside emacs. Combine this with running a remote R session in emacs, and there you have it, the perfect work flow for developing and running computationally intensive R code! I can utilize screen to not have my R sessions interrupted, and I can utilize ESS to send code to an R session/buffer. I have to say, this WILL be the way I use R for any computationally-intensive project!

UPDATE

So screen doesn’t work in emacs after I ssh to a remote server inside ansi-term. I get the error: Cannot find terminfo entry for 'eterm-color'. To fix this, I put the following in my remote server’s .bashrc file (info from here.):

if [ "$TERM" = "eterm-color" ] ; then
 TERM="vt100"
fi

UPDATE AGAIN (better solution)

This page (Remote Term Type section) shows how to fix the e-term color issue. Do make sure you create the .terminfo folder if its not there:

$ scp /usr/share/emacs/22.1/etc/e/eterm-color username@remoteserver:~/.terminfo/e/eterm-color
$ scp /usr/share/emacs/22.1/etc/e/eterm-color.ti username@remoteserver:~/.terminfo/e/eterm-color.ti

UPDATE AGAIN 2

So copying files into .terminfo didn’t fix everything. I guess SunOS servers don’t look in my home directory for those files. I guess we can copy things into /usr/share/lib/terminfo/?/* (more information at man terminfo), but I don’t have access to this location in some of my servers. I will have to resort back to the old trick (changing TERM). This time, change it to xterm (this doesn’t give me funny characters in emacs ansi-term); found this at the bottom of this page. Put the following in the remote .bashrc file:

TEMP=`uname`
if [ $TEMP = "SunOS" ]; then
 if [ "$TERM" = "eterm-color" ] ; then
 TERM="xterm"
 fi
fi

I hope there aren’t any more issues. What the previous trick does is check if system is SunOS, and if so, use xterm. I got the unix command information from here. I got the uname command info from here.

FINAL UPDATE

To get eterm-color to work in SunOS, put the following in my .bashrc file:

##following to get eterm-color working in SunOS
TERMINFO=${HOME}/.terminfo
export TERMINFO

I guess I’ve been doing this, but I never exported TERMINFO. Didn’t know this was needed. Make sure the eterm-files are copied over (see top of post). Now everything should work, hopefully flawlessly. To summarize, copy eterm files into ~/.terminfo, and put the TERMINFO stuff in ~/.bashrc.

Now screen works in emacs. An issue that arised from this method is that when screen is run inside emacs, I can’t execute ess-remote anymore because I can’t press M-x. In ansi-term, C-x C-j : behave like emacs, cursor can go anywhere C-x C-k : behave like a terminal (default) This is documented here and here. Press C-x C-j and I can press M-x again. However, ess-remote still doesn’t work.

I guess when I use screen, I am forced to copy and paste code. If I really must use screen with ESS, do the regular M-x shell. After logging into the remote server, execute “TERM\=’vt100′” in the shell. Now, run screen -> R -> ess-remote. I can send code with keypresses now, but screen steals some of my emacs key bindings. To fix this, put the following,

escape \^Oo

in my remote ~/.screenrc file to switch the default command key from C-a to C-o so it doesn’t conflict with my emacs key bindings (documented here).

More information on ansi term (like remaping C-x to C-c) can be found here.

This was a long post. Summary:

  1. ansi-term in emacs behaves VERY much like a terminal. I can run vi, emacs, etc, inside of it. Emacs behavior is ‘term’ and ‘shell’.
  2. I can change things by editing the env variable, TERM.
  3. Change keybinding in the remote .screenrc file.

NEED TO DO: get ess-remote to work with ansi-term and screen in emacs!

UPDATE2: It seems the best way to do things so far is to use ansi-term -> ssh to remote server -> screen -> R, then go to line run (C-c C-j) and copy and paste code from there. To get screen commands to work (like detach, etc), need to go back to char run (C-c C-k). Remember, I now use C-o instead of C-a (defined in .screenrc); this only works on a regular terminal or in emacs with ansi-term, not using ‘shell’ in emacs with the hack I mentioned up there.