Skip to content

Displaying and Interacting with processes using Proced

Jul 8 14
by mickey

If you’re a regular user of the commandline tools top or ps then I have good news for you: there’s an Emacs command that does that and it works on Windows too!

The command is M-x proced and it is modelled on M-x dired (but it is not built on top of dired) and it does shadow most of the common navigational aids: n and p move down and up and g refreshes the buffer, for instance.

It’s also feature rich; by combining the utility of ps with the sortable, columnized and interactive system of top you get the best of both worlds.

What I like about it is that it’s a buffer like any other in Emacs. So if you’re keeping your eye on a handful of processes you can use highlight-phrase to highlight the strings you want to visually separate from the rest. It is also, in true Emacs tradition, very customizable — but I’ll get to that a bit later.

Marking, Sorting, Filtering and Formatting Columns

By default you will be given the short formatting but you can change the number of displayed columns by typing F. You’ll be shown a prompt where you can select from short, medium, long and verbose.

You can also change the filtering — which defaults to all — by pressing f. Like the formatting key above you are given a list of common filter types. Furthermore you can filter by any of the visible columns by moving your point to it and pressing RET. It will incrementally filter so you can repeatedly filter as much as you like — this is a very handy feature. Refreshing the buffer with g does not reset it: you must change your filter with f.

Like dired you can mark processes with both d and m (they do the same thing) and unmark with backspace and u. You can mark and unmark all processes with M and U. And finally you can then filter the marked processes with o.

There are some specialty mark commands as well. The command t will invert your marks and C and P will mark the children or parents of the process point is on.

Finally you can sort by columns as well. All the sort commands are bound to M-x proced-sort-xxx and to the prefix menu s:

Command Description
s c Sort by CPU %
s m Sort by Memory %
s p Sort by PID
s s Sort by Start Time
s t Sort by CPU Time
s u Sort by User
s S Sort by other column

If you want to sort by a column not bound to a key you must use s S where you are then prompted for the name of a column to sort by.

Sending Signals and Interacting with processes

Unsurprisingly sending POSIX signals is fully supported by Emacs. This is done using the Elisp function signal-process. Because of that all signals are sent using the uid of the Emacs process proced is running in. Conceivably proced could be changed to call out to userland command kill (using an external command is supported) and, by combining it with TRAMP for sudo elevation it could kill processes owned by other users. Sadly that functionality does not currently exist but abusing the default-directory variable might work.

To send a signal mark the processes you want to signal, or alternatively put your point on the target process, and press k or x. You will be shown a list of target processes and you can optionally change the default suggestion of TERM to something else.

When the signal(s) have been sent you can review the log by pressing ?.

As of Emacs 24.3 you can also renice a command by pressing r.

Understanding & Customizing Proced’s Internals

Proced is very flexible and it also supports the customize interface. You can access this inferface by typing M-x customize-group proced RET.

Proced will use the Elisp function list-system-processes to retrieve all the known PIDs and then call process-attributes on each one of them to fetch the process details. The good news is that Proced does not parse the output of ps or /proc directly but relies on Emacs’s compatibility layer to make sense of everything: this also means it works rather well on Windows.

Proced can auto update the display and you can enable this for a Proced buffer by running the command proced-toggle-auto-update and it will update every proced-auto-update-interval which is by default 5 seconds.

To make Proced auto update by default you must add a hook:

You are free to customize other settings in this mode hook in much the same way you would other mode hook functions.

If you’re really serious about tweaking the way the columns are displayed you can try your hand at editing the alist variable proced-grammar-alist. It controls how sorting, justification and string formatting works for each of the process attributes returned by process-attributes.

You can also alter the default Proced filter by changing proced-filter from user to something else. Creating your own filter is also possible by adding to the proced-filter-alist; in fact this functionality lets you do rather complex things such as calling a custom function for every process attribute.

The default sort and sort order (descending or ascending) is governed by proced-sort and proced-descent respectively.

There are many more customizable options and the author of Proced, Roland Winkler, has done a great job ensuring they’re accessible from Emacs’s customize interface.

It goes without saying that Proced is a both feature rich and customizable. I think it’s a very capable replacement for most ps and top use cases, but like so features in Emacs almost nobody has heard of it.

Dired Shell Commands: The find & xargs replacement

Apr 10 14
by mickey

If you’re a Linux/BSD user you probably do most of your bulk operations on files with the command line tools find ... -exec or find ... | xargs — but there’s a much better way. Dired (M-x dired or C-x d), Emacs’s DIRectory EDitor, can do the same but in a very intuitive way. I’ve talked about Dired before: how to work with files spread out across many directories and editable dired buffers that will persist the changes made to filenames and permissions in the buffer.

Of all the hidden gems in Dired, this is one of my favourites: the ability to run arbitrary shell commands on marked files — and if it’s a file extension known to Dired, it will suggest a default action: untarring .tar.gz files; displaying .pdf files; and much more!

Installing Dired-X

The latter functionality is buried in dired-x, an add-on for Dired that… ships with Emacs but isn’t enabled by default.

To use dired-x you must load it first.

You can also just (require 'dired-x) somewhere in your init file.

Using Emacs’s Guess Shell Command functionality

To apply a shell command to marked files press !. If you have no marked files Dired will apply the shell command to the file or directory point is on. Dired will suggest a list of defaults (navigate the choices with M-n and M-p) and if Dired-x is loaded it will set a default action so you just press enter to apply it.

Dired-x ships with a fairly large repository of common operations on files and you can add your own by modifying the alist dired-guess-shell-alist-user.

Running Arbitrary Shell Commands

Another little-known feature of Dired’s shell command functionality is that you can write your own one-off commands but run each command per marked file or collect (and separated by whitespace, like in the shell) them all and pass them to a single instance of the command.

Some shell commands accept many files per command and others just one. You can use two wildcard operators (* and ?) in a Dired shell command and that will determine how Dired constructs the external shell command(s).

Let’s say I want to call the external script foo.py. My shell command can be constructed in two ways. And let’s assume I have two files marked in Dired: hello.txt and world.txt.

If the command foo.py only accepts a single file argument I can use ? as the substitution variable. So foo.py ? will give us the following expanded commands:

If I had used * in lieu of the ? substitution variable then the entire list of marked files would have been inserted with whitespaces separating each file, like so:

Note: You must separate the substitution variable with spaces or it won’t work right!

If you want your calling shell to expand the * as a globbing wildcard you must type *"" instead.

Finally, you can dictate whether the shell commands should be executed synchronously or asynchronously.

By default the commands are called synchronously unless you append &, ; or ;&. However, they are not all alike. If you are operating on multiple files (that is, you are using the ? substitution variable) then & will make the shell commands execute in parallel as well! If the command ends in ; or ;& then the commands are executed sequentially, one after another but still asynchronously.

Dired’s shell command functionality is particularly powerful if you want to apply the shell commands on files spread out across multiple directories as it eliminates the need for find in all but the most complex searches. My article on how to work with files spread out across many directories shows you how you can find files across directories and display them in a “merged” dired buffer. Combined with the Dired shell command I just talked about and you can replace most external find ... -exec command patterns. If you think find is painful to use simply use Dired’s extremely powerful file marking functionality and off you go.

My Emacs keybindings

Feb 28 14
by mickey

I figured I’d write a blog post about the keys I’ve bound but also rebound in Emacs. I think rebinding keys in Emacs — even though, in essence, the editor is built around the idea of customization — is a perilous thing to do if you are not careful: people do it without knowing why the key is bound to what it is. This is particularly true of the “core” bindings in Emacs. Rebinding keys is something you should do as a last resort: if you’re new to Emacs and your first impulse is to rebind everything — stop! Learn Emacs first and then decide.

Consider C-f. It moves forward by the character, but M-f moves forward by word, and C-M-f moves forward by an s-expression. See the similarities? The C- for character, M- for word and C-M- for s-exp is a recurring pattern in Emacs.

Having said that… there are keys I rebind and commands I explicitly bind to keys. Most of them are quality of life improvements: I want to make it easier to type things I do frequently.

New Binding Reason for Rebinding
M-o The command other-window is normally bound to C-x o but I find that way too cumbersome for what is such a frequent operation. M-o is normally bound to some rich text formatting nobody cares about.
C-<return> This I bind to a custom Helm command that calls up some of my more frequent things. This key is surprisingly unbound in most modes.
M-` This is taken from my article on fixing the mark commands. When pressed it’ll jump around in the mark ring. Very useful, but Ubuntu and Unity (in their wisdom) have decided that I am not allowed to rebind this key…. so I cannot use it any more.
C-x C-k I kill buffers all the time, and the idea of killing a buffer that is not active is just not part of my workflow at all. This key will kill the active buffer without any prompting whatsoever.
C-z I bind this to the command repeat which will repeat the last command you did. Read my article on repeating commands for more info.
C-= This I bind to CSSH’s cssh-term-remote-open. It prompts me for a remote host to SSH to using an M-x ansi-term session. It’s great.
C-x C-r By default this will run find-file-read-only, a command that finds a file but opens it as read only. Meh. That’s all I have to say about that. The few occasions I need to do this I just set it as read only. Instead I bind it to a custom command that, using IDO, gives me a list of recent files I’ve opened.
M-n / M-p I bind these to my smart scan next/previous commands. They’re part of my Smart Scan package.
S-C-<left/right/down/up> This calls shrink/enlarge-window-horizontally and enlarge/shrink-window respectively. I don’t often resize my windows any more (my high monitor resolution means I don’t have to) but it’s occasionally useful, and definitely more useful than the useless and impossibly-hard-to-type C-x ^, et al. they’re bound to by default.
F1 This runs M-x shell, my preferred one of the terminal/shell wrappers in Emacs. If you don’t know the difference, read Running shells in Emacs: an overview. It’s such a handy keybinding, too. Yes, you override the help, but it’s bound to C-h also.
F2 Runs M-x rgrep. Indispensable. I grep a lot and the key is very accessible. By default it’s bound to the 2-column commands but they’re also bound to C-x 6 ....
C-<f2> This runs my own command, multi-occur-in-this-mode. This will run M-x occur but against all buffers of the same major mode as the one point is in. Very useful. Searching buffers with occur mode will tell you all you want to know.
F6 This calls another custom command of mine, revert-this-buffer. It does exactly what the name implies: it reverts (reloads from file) the current buffer without asking any questions. It will notify you in the minibuffer area that it did it.
F10 This calls magit-status. That will open up Magit, a great Git client for Emacs. Simple, but I use it 100s of times a day.
C-x C-b This is simply bound to M-x ibuffer. Terrific feature; ibuffer’s great.

And that’s pretty much it. Not a whole lot, really. I do repurpose quite a few of the aforementioned keys as there is, to me, a lot of useless cruft I’d never use, but apart from that very few of them are remapped.

Still, there’s always room for improvement. If you have any suggestions or handy snippets — post below or e-mail me.