chainsawriot

Home | About | Archive

Advent of emacs #11: How I do system administration in emacs

Posted on Dec 11, 2022 by Chung-hong Chan

I have talked about some basics about emacs in the last 10 days. From now on, I will be focus more on the specific usage of emacs. On around day 19, I will start to talk about my attitude towards emacs 1.

Today I will talk about two sysadmin tasks that are suitable to be done in emacs. There are many other tasks in the realm of sysadmin. For those I would rather do them in the shell, which I talked about on day 5.

Editing configuration files

On day 4, I talked about it is not a good idea to use emacs like vi(m). This is another example: Some configuration files can only be edited with sudo, e.g. /etc/environments. The direct response to this task would be:

sudo emacs /etc/environments

Or even

sudo su
emacs /etc/environments

For a long time, I really did it like that. But the problem with this approach is that I am launching emacs as root. And my root account doesn’t necessarily have any emacs configuration file. And therefore a vanilla white box is there for me. Nein, danke.

Inside my own emacs, I can actually use find-file (C-x C-f) to open /etc/environments. But I can’t save.

TRAMP

TRAMP is a built-in feature of emacs and many users would associate it with editing remote files. I don’t want to talk about editing remote files and actually TRAMP is much more powerful than just editing remote files. On a recent enough version of emacs, one can use the /method::/ syntax in find-file.

It can also be used for opening a file with sudo.

/sudo::/etc/environments

helm-find-files

I use Helm (see day 1) and the helm version of find-file, i.e. helm-find-files, can also open a file as root. This method is under-documented.

The method is to use M-x helm-find-files, pointing to a file you want to edit as root, then press C-c r to run helm-ff-run-find-file-as-root.

Process management

Another task that is suitable to do in emacs is process management. One can also do the same either with 1) the suite of ps and kill (or pkill), or 2) ncurses-based tools such as htop.

The built-in emacs process manager proced is also useful. For quick-and-dirty tasks such as “sorting processes by memory usage” or “send SIGTERM to a process”, I find it quite useful. It is one of the two “dired-like” tools that I regularly use. What is a “dired-like” tool? Well, a “dired-like” tool looks like the directory editing mode (dired) of emacs. Commands are largely assigned to one single button. If you need to know two of these single-button commands, know g for refresh (or revert-buffer) and q for quit. Usually, these tools also have a mechanism to select multiple items by marking one item with m. And also important to know how to unmark one item u and unmark all U.

OK. Firing up proced by M-x proced, I see something like that (by default it is opened as a split-window).

It is very similar to running ps. But the buffer is searchable by the usual C-s (isearch-forward), C-r (isearch-backward) or even regex search C-M-s. For example, I want to search for a process with eog (the image viewer)

I can kill this process by pressing “k” and send a TERM signal (“Terminate”) to it.

Unlike top or htop, the buffer is not constantly updated. One need to manually update it by pressing g. For senior citizens like me who read slowly, I like it. But if you don’t like it, you can toggle the auto-update mode by the command proced-toggle-auto-update.

Bonus thought: file management

I have a complicated relationship with dired. As I said on day 5, I prefer to do file management in the shell.

I have tried several times to make dired more useful, e.g. using Sunrise Commander. I think the fundamental problem is that I have a preconception that it is faster to do file management with the command line and not quite familiar with managing files in the 2-dimensional space with tools such as Norton Commander. For some tasks, emacs adds value by changing it from 1-D to 2-D, e.g. proced. But to me, file management isn’t one of these tasks. “But that can well be my prejudice.”™

However, image-dired is something I can appreciate. It is a nice interface to deal with a directory full of images with an iPhoto kind-of interface. In this case image-dired really adds a lot of value for being 2-D. Also it gives another reason to use the GUI emacs.

Tomorrow I will talk about another task that emacs adds a lot of value by changing it from 1-D to 2-D.


  1. I am not prot the philosopher and emacs user extraordinaire. But I will try. 


Powered by Jekyll and profdr theme