Emacs is an incredibly powerful text editor. It can be programmed to do
anything. It was written before the advent of GUI interfaces, so every important function can be accessed through a series of keystrokes. Many of the more popular functions appear in menus. If you spend the time to learn the small subset of keystrokes for basic functions, you will find that Emacs is very well laid out, intuitive, and very fast to use.
ESS (Emacs Speaks Statistics) is an add-on to Emacs to make Emacs a powerful session environment for R and other statistical systems.
Invoking Emacs
You can invoke Emacs from the KDE
Development
menu (you can create a button on your task bar to make this easy) or by issuing the command
emacs
or
emacs &
from the command prompt. If you want to open a file into an existing Emacs session, define the following alias in your
.bashrc
file:
alias Xmacs="emacsclient -n"
and set up your Emacs
.emacs
file so that the Emacs server is started the first time
emacs
is involved. This is done by adding the following lines to
.emacs
:
(setq gnuserv-frame (selected-frame))
(server-start)
Then at the command prompt you can type
Xmacs foo
to open the file
foo
into the already started Emacs session.
A better approach is to define a command that will start Emacs if no session is running, or to add a new buffer otherwise. The command is executed with
emx myfile
. Put the
emx
script file in
~/bin
.
#! /bin/bash
if emacsclient --eval nil >/dev/null 2>&1;
then
emacsclient $2 -n $1
else
emacs $1 &
fi
Note that with the
It's All Text!
plugin for
Firefox
you can define
emacsclient -n
as the editor the plugin should use.
Installing Emacs and ESS
- use command
apt-get install emacs24 auctex emacs-goodies-el ess
The
goodies
package includes many useful Emacs packages such as
tabbar
.
auctex
provides advanced LaTeX features.
To use
auctex
with ESS you need to install the polymode Emacs package:
cd /usr/share/emacs/site-lisp
sudo git clone https://github.com/vitoshka/polymode.git
To control which types of R syntax are highlighted, click on
Ess ... Font Lock
and select your options, then click on
Save to custom
which will update your
~/.emacs
.
To take full advantage of
auctex
,
reftex
,
polymode
, and
ess
add this to your
~/.emacs
file:
(load "auctex.el" nil t t)
(require 'tex-site)
; Make default LaTeX output pdf; see http://blogisticreflections.wordpress.com
(setq TeX-PDF-mode t)
(add-hook 'TeX-mode-hook
(lambda () (TeX-fold-mode 1))); Automatically activate TeX-fold-mode.
;;; Emacs polymode - allows auctex/reftex to work with .Rnw files
(setq load-path
(append '("/usr/share/emacs/site-lisp/polymode/" "/usr/share/emacs/site-lisp/polymode/modes")
load-path))
(require 'poly-R)
(require 'poly-markdown)
(require 'poly-noweb)
; Turn on RefTeX for AUCTeX, http://www.gnu.org/s/auctex/manual/reftex/reftex_5.html
(add-hook 'LaTeX-mode-hook 'turn-on-reftex)
; Make RefTeX interact with AUCTeX, http://www.gnu.org/s/auctex/manual/reftex/AUCTeX_002dRefTeX-Interface.html
(setq reftex-plug-into-AUCTeX t)
(setq reftex-file-extensions
'(("tex" ".Rnw" ".nw" ".tex")
("bib" ".bib")))
auctex
makes it easy to see a table of contents for a long
knitr
report, making it easy to navigation to LaTeX
\section{}
and
\subsection{}
text.
Screenshot of Emacs with ESS, AucTeX, RefTeX, Polymode
Emacs and LISP
As I pointed out earlier, Emacs can be programmed to do anything. This is because Emacs is written in Lisp. Lisp is a dynamically compiled language that uses Polish notation (aka prefix notation).
Basic Emacs Keystrokes
-
C-x C-f
: Find file.
-
C-x C-s
: Save buffer.
-
C-x C-w
: Write buffer to file.
-
C-x C-c
: Quit Emacs.
-
C-h
: Help system.
-
C-g
: command quit.
-
M-x
: Execute extended command.
-
C-s
: Search forward.
-
C-r
: Search backward.
Emacs Configuration
%C%
One example of an Emacs configuration file follows below. More importantly you will probably want to configure Emacs to use horizontal tabs for easily navigating multiple buffers. Do this by putting the following in your
~/.emacs
file:
(setq tabbar-buffer-groups-function (lambda () (list "All buffers")))
(setq tabbar-cycling-scope nil)
(setq tabbar-home-button (quote (("[Home]") "[x]")))
(setq tabbar-separator (quote (" ")))
(require 'tabbar)
(tabbar-mode)
Here is the other version of
.emacs
to pick and choose from.
(defun print-header-func ()
(concat (ps-time-stamp-locale-default) " : " (ps-header-dirpart)))
(setq lpr-command "gtklp")
(setq inferior-R-args "--vanilla")
(ps-extend-face '(default "black" nil nil) 'MERGE)
(mouse-wheel-mode)
(global-set-key [C-tab] 'cyclebuffer-forward)
(global-set-key [f4] 'goto-line)
(global-set-key [C-S-iso-lefttab] 'cyclebuffer-backward)
(global-set-key [(control x) (control k)] 'kill-this-buffer)
(add-to-list 'auto-mode-alist '("\\.rd\\'" . Rd-mode))
(add-hook 'Rd-mode-hook
'(lambda ()
(abbrev-mode 1)
(font-lock-mode 1)))
(setq lpr-command "gtklp")
The last command makes the Emacs
Print
commands (accessible from the
File
menu) use the
gtklp
app printing, which gives you many printing options.
To get nice color highlighting of matching {[()]}, do the following:
cd /tmp
wget -nd http://nschum.de/src/emacs/highlight-parentheses/highlight-parentheses.el
sudo cp -p /tmp/highlight-parentheses.el /usr/share/emacs22/site-lisp/ess/.
then put the following in your
.emacs
file:
(require 'highlight-parentheses)
(setq hl-paren-colors '("gold" "red" "blue" "green" "orange"))
(defun hpm-on ()
(highlight-parentheses-mode t))
(add-hook 'ess-mode-hook 'hpm-on)
(add-hook 'inferior-ess-mode-hook 'hpm-on)
(add-hook 'tex-mode-hook 'hpm-on)
(add-hook 'text-mode-hook 'hpm-on)
Click on the
Options
menu on Emacs and check
Paren Match Highlighting
.
ESS Edit Buffer Keystrokes
-
_
: smart underscore inserts a " <- "
, press _
again to get a _
.
-
C-c TAB
: object completion.
-
C-M-q
: indent expression.
-
C-M-a
: goto beginning of function.
-
C-M-e
: goto end of function.
-
C-M-q
: indent each line in the compound expression which follows point.
-
C-c C-l
: load a source file.
-
C-c C-z
: jump to ESS buffer.
-
C-`
: jump to location of syntax error.
-
C-c C-v
: get help for a R function.
ESS Evaluation Keystrokes.
-
C-c C-j
: evaluate line.
-
C-c C-n
: evaluate line and step.
-
C-c C-b
: evaluate buffer.
-
C-c C-f
: evaluate function.
-
C-c C-r
: evaluate region.
- For each evaluate command if you replace the last CTL with ALT the command will jump to the ESS buffer.
-
C-c M-f
: evaluate function and jump to ESS process buffer.
-
C-c M-b
: evaluate buffer and jump to ESS process buffer.
ESS Process Buffer Keystrokes.
- In general the process buffer's keystrokes are the edit buffer's keystrokes while holding CTL.
-
C-END
: go to end of buffer.
-
C-HOME
: go to begining of buffer.
-
C-UP
: previous command in history.
-
C-DOWN
: goto begining of word.
-
C-LEFT
: goto end of word.
-
C-RIGHT
: goto end of word.
-
C-c C-w
: backward kill word.
-
C-c C-u
: delete everything from prompt to point.
-
TAB
: object and filename completion.
Useful .emacs
definitions for running ESS with R
Add the following 3 lines if you want to have R use the directory from which you invoked
emacs
or
emacsclient
as the
working directory, you want R to open in a new window, leaving your source code window intact, if you want ?foo to open a help file
for function foo in a new frame, and if you don't want to restore or save an
.RData
object with all the objects created during your R session.
(setq ess-ask-for-ess-directory nil)
(setq inferior-ess-same-window nil)
(setq ess-help-own-frame 'one)
(setq inferior-R-args "--no-restore --no-save")
If you want ESS to ask you about the location of the working directory, remove the first
setq
command. Otherwise, this is a good way to use ESS with R:
cd myproject
emacs analyze.r & or emacsclient -n analyze.r &
# See source code for analyze.r in the sole Emacs bufer
Esc R # start R session in myproject directory
?functionname # open help file in a new Emacs frame