One extremely cool (but little known) new feature in Emacs 23 was the addition of proced.
proced basically allows you to run top inside Emacs and monitor/control processes via it. Here’s
how it looks in action:
Normally you’d use M-x proced to start the command, but as mentioned
earlier I find it extremely useful and therefore I bind it to C-x p
(inspired by dired’s C-x d):
See proced-mode(C-h f proced-mode) for a description of features available in Proced buffers.
Some of you might have noticed that the screenshot in the post is
taken under GNOME, which is kind of strange considering I’m an OSX
user. Unfortunately proced does not work on OSX(but perhaps surprisingly it works on Windows).
The C-x p keybinding works out-of-the box on Prelude.
Few weeks ago I showed you a
handy way to run term-mode. You
might have noticed that it makes sense for many commands to be run in
a similar manner. Here’s a quick example - I often like to jump
between an Emacs Lisp source buffer and an ielm (interactive Emacs
Lisp shell - M-x ielm) buffer to try out stuff. I could reuse the
code I showed you for ansi-term to create a similar command called visit-ielm:
(defunvisit-ielm()"Create or visit a `ielm' buffer."(interactive)(if(not(get-buffer"*ielm*"))(progn(split-window-sensibly(selected-window))(other-window1)(ielm))(switch-to-buffer-other-window"*ielm*")))
You might want to bind this to C-c C-z (a-la SLIME):
I don’t know about you, but I hate code repetition. Having that in
mind we can factor out the duplication like this:
(defunstart-or-switch-to(functionbuffer-name)"Invoke FUNCTION if there is no buffer with BUFFER-NAME.Otherwise switch to the buffer named BUFFER-NAME. Don't clobberthe current buffer."(if(not(get-bufferbuffer-name))(progn(split-window-sensibly(selected-window))(other-window1)(funcallfunction))(switch-to-buffer-other-windowbuffer-name)))(defunvisit-term-buffer()"Create or visit a terminal buffer."(interactive)(start-or-switch-to(lambda()(ansi-term(getenv"SHELL")))"*ansi-term*"))(defunvisit-ielm()"Switch to default `ielm' buffer.Start `ielm' if it's not already running."(interactive)(prelude-start-or-switch-to'ielm"*ielm*"))
Much better! We can now use start-or-switch-to to build any number of similar commands!
start-or-switch-to and visit-ielm are available in
Prelude(but with a prelude-
P.S. If you’d like some nice SLIME-like code navigation command in
emacs-lisp-mode you might check out
SLIME allows very convenient navigation to the symbol at point (using
M-.), and the ability to pop back to previous marks (using M-,).
This plugin provides similar navigation for Emacs Lisp, supporting
navigation to the definitions of variables, functions, libraries and
Additionally, elisp-slime-nav provides a way to describe the symbol at
point, whatever its type. As with slime-describe-symbol, this
functionality is bound both to C-c C-d d and C-c C-d C-d by default.
It’s pretty useful if you hack Emacs Lisp yourselves. If you don’t
hack Emacs Lisp (yet) you probably can do without it.
One of my bigger problems as an Emacs user is that there only so
many good keybindings available. At some point you’re out of good
options, but there are still plenty of commands you’d rather be able
to execute faster than you’d be able to do with M-x command-name.
key-chord-mode to the
rescue! It’s a pretty handy minor mode which allows you to bind
commands to keys that should be pressed together or consecutively(with
little time between the keystrokes - by default 0.3 seconds). This
gives you some pretty good opportunities to increase your
productivity. Assuming you’ve installed the mode somehow, you can start
playing with it:
The examples above illustrate global key-chords. Major mode specific
key chords are also available.
One should be careful about the key-chord he chooses - it should
definitely be something you’re unlikely to type normally. This also
means that if you’re writing in Emacs in several languages with common
letters, key chords that make sense in say English, might not make
sense in French (for instance). Luckily for me - I write almost
exclusively in English or some programming language (and Bulgarian and
English are nothing alike).
One other note - key chords don’t play very well with
evil-mode(Emacs’s vim emulation layer) for obvious reasons. vim is
kind of key-chord-mode’s principle inspiration. If you’re an evil-mode user you
probably don’t need key-chord-mode anyways.
Prelude enables key-chord-mode
out of the box and makes some creative use of it with jj and JJ
(which I’d advise you try out if you didn’t know about them).
Jumping between the current buffer and the one you were in before is a
pretty common task. It’s also one you can solve in a multitude of
way - the most popular being simply the use of switch-to-buffer and
ido-switch-to-buffer. Both commands (which are generally bound to
C-x b) would suggest to select the previous buffer when you invoke
them. While this was good enough for me for many years, recently
Luke Randall suggested me a neat
(defunswitch-to-previous-buffer()"Switch to previously open buffer.Repeated invocations toggle between the two most recently open buffers."(interactive)(switch-to-buffer(other-buffer(current-buffer)1)))
When you pair this command with a handy
it becomes a great performance booster:
Anybody knows that you can run a shell inside Emacs with M-x
shell. Few people know you can have Emacs run some additional
configuration for the shell after it’s started.
Emacs sends the new shell the contents of the file
~/.emacs_shellname as input, if it exists, where shellname is the
name of the name if your shell - bash, zsh, etc. For example, if you
use bash, the file sent to it is ~/.emacs_bash. If this file is not
found, Emacs tries with ~/.emacs.d/init_shellname.sh.
One popular application of such custom configuration is to have a
simpler shell prompt in Emacs, compared to the one you usually employ,
since shell-mode doesn’t deal well with fancy prompts.
You can find out more about running interactive shells in Emacs
or by typing C-h r m Interactive Shell RET in your favorite text
One common hurdle Emacs users experience when hacking in Emacs is that
many programming languages use CamelCase for class names, method
names, identifiers, etc, but Emacs (unlike most IDEs) doesn’t treat
CamelCase words the same special way it treats lisp-case and snake_case
words. Let me illustrate this with an example.
If you invoke kill-word(M-d) when your cursor is before
lisp-case this would kill just lisp and leave you with -case. If
you do the same with CamelCase the entire “word” CamelCase will be
killed. Same goes for navigation-by-word commands like forward-word,
backward-word, etc - they are aware of the subwords that comprise a
lisp-case or a snake_case word, but are oblivious to
CamelCase. So, what can we do? The answer is surprisingly simple -
just use the built-in minor mode subword-mode (formerly known as
;; enable just in ruby-mode(add-hook'ruby-mode-hook'subword-mode);; enable for all programming modes(add-hook'prog-mode-hook'subword-mode)
At this point you can try again kill-word before CamelCase. Only
Camel is killed now. Sweet!
Prelude enables subword-mode
out of the box for programming modes that make use of CamelCase.
One area where Emacs traditionally falls short by default is editing
files that require root permissions. Since most Emacs users just use
a single Emacs frame they never leave, they have the problem of having
started it with their current user’s privileges(and it’s unlikely this
user is root). In this post I’ll describe two ways to alleviate that
particular problem (note that emacsclient users have other options
to pick from as well - one is mentioned at the very end of the post).
Here’s Option A - a simple command that allows you to reopen the
currently visited file with root permissions(obtained via tramp and
sudo) and prompts you for a file name if you’re not currently
visiting a file or supply a prefix argument:
(defunsudo-edit(&optionalarg)"Edit currently visited file as root.With a prefix ARG prompt for a file to visit.Will also prompt for a file to visit if currentbuffer is not visiting a file."(interactive"P")(if(orarg(notbuffer-file-name))(find-file(concat"/sudo:root@localhost:"(ido-read-file-name"Find file(as root): ")))(find-alternate-file(concat"/sudo:root@localhost:"buffer-file-name))))
I came across a similar command a few years back and it’s been pretty
useful to me ever since. If you like it, I’d suggest binding it to C-x
C-r (kind of like find-file’s binding C-x C-f).
Lately I’ve decided that such a command is a bit of an overhead, since
we can check the file permissions automatically anyways. While I’m not
quite fond of advising commands (debugging advised commands is no
fun) this was an excellent opportunity to exploit them (for great
(defadviceido-find-file(afterfind-file-sudoactivate)"Find file as root if necessary."(unless(andbuffer-file-name(file-writable-pbuffer-file-name))(find-alternate-file(concat"/sudo:root@localhost:"buffer-file-name))))
This advises ido-find-file(you might want to advise find-file
instead if you’re not using ido) to reopen the selected file as
root(you’ll be prompted for your sudo password) if you don’t have
write permissions for it. Extremely cool!
The first command has been part of
Prelude since forever. As of late,
the ido-find-file advice is also present there and it
generally voids the need for sudo-edit.
emacsclient users have it easy. They just need the following shell
alias(I generally alias e to emacsclient -t):
alias E="SUDO_EDITOR=\"emacsclient -t -a emacs\" sudoedit"
This should(will) finally save you from reaching for vim in the terminal.
The Emacs Lisp interpreter’s integration with Emacs is pervasive. One
can evaluate Emacs Lisp just about everywhere in so many different
ways. One way I find particularly handy(especially for short snippets of
code) is evaluating Emacs Lisp directly in the minibuffer. This is
done with M-:(bound to the command eval-expression).
Upon typing this magic keycombo you’ll be presented with the prompt
Eval: in the minibuffer and there you can enter any Lisp
expression. Hitting Return will result in the expression’s
There is one minor snafu with this, though - I’m addicted to
Paredit(for editing Lisp code at
least) and Paredit is not automatically enabled in the
minibuffer. Let’s change that:
(defunconditionally-enable-paredit-mode()"Enable `paredit-mode' in the minibuffer, during `eval-expression'."(if(eqthis-command'eval-expression)(paredit-mode1)))(add-hook'minibuffer-setup-hook'conditionally-enable-paredit-mode)
Evaluate this snippet and try M-: again. If you’re like me - the
results will please you.
It should come as no surprise that
Prelude enables Paredit by
default for eval-expression.
It most text editors if you select a piece of text and press any
key(that’s not bound to some command) the selection would be replaced
by the character bound to the key(or deleted if you press
Backspace). In Emacs things look differently - by default typed
text is just inserted at point, regardless of any selection.
Of course, there is a way to change this:
Now when transient-mark-mode is also enabled(it is enabled by
default since Emacs 23) any typed text replaces the selection if the
selection is active.
Personally I find this behavior more useful than the default
one. That’s why it should be no surprise that delete-selection-mode
is enabled by default in
Let’s continue our exploration of the fascinating topic of
killing lines. Apart from
killing lines from the cursor to the end of the line(kill-line) and
killing lines from the cursor back to the beginning of the
line(kill-line with a 0 prefix argument) you can also kill entire
lines with a single command - kill-whole-line.
The command is bound to C-S-Backspace by default and kills the line
including its newline. With a prefix argument, it will kill that many
lines starting from the current line. If the argument is negative it
will kill that many lines backward plus the preceding newline. If the
argument is 0, it will kill the current line but exclude the
trailing newline. We can improve the command a bit by making it
indentation aware(like we did for backward kill line in the previous post):
(defunsmart-kill-whole-line(&optionalarg)"A simple wrapper around `kill-whole-line' that respects indentation."(interactive"P")(kill-whole-linearg)(back-to-indentation))