2013-06-11
Editing remote files with Acme in Inferno OS
Inferno OS ships with a version of the acme text editor. I've tried to use acme on and off for a bit and it never stuck. I've always been a vim
and emacs
user. Recently I watched a video by Russ Cox called A Tour of Acme that motivated me to try it again.
The video is short and covers a lot of what makes Acme interesting. Most of it is related to the extensibility of the editor via shell commands dealing with piped input and output alongside the power of sam editing commands.
Some useful acme papers and tutorials are:
- A Tour of Acme
- Acme: A User Interface for Programmers
- Acme man page
- A Tutorial for the Sam Command Language
- The Text Editor Sam (for more on the command language)
For a recent task I had restored a backup of my Creatures Developer Resource website, recovering from a hard drive failure. I wanted to remove the Google ad code from each page of the website.
To do this from within Inferno and using Acme I first ensured I had the styx
service running on the remote web server and had the host file system bound so remote users could connect to it:
; bind '#U*' /mnt/host
; svc/styx
On my client Inferno machine I mounted this remote directory and ran acme
to edit the files on it.
% mount tcp!example.com!styx /n/host
% acme
The acme
editor starts and from here I can edit files on the remote server by accessing /n/host/mnt/host
. The first step was to load all HTML files in the webserver directory into acme
. This was done by executing the following command in an acme
window that had its location set to /n/host/mnt/host/var/www/example.com
(using mouse button 2 to execute):
Edit B <du -an|grep 'html?$'
From the Sam Reference it can be seen that B
is used to load files into the editor. In this case I load the output of the command du -an|grep 'html?$'
. The command du -an
provides a recursive list of all files in the current directory and subdirectories and the grep
limits it to those ending in htm
or html
. Once executed acme has loaded all the files we're interested in from the remote server.
The Google Ad snippet to be removed looks like:
<!-- Gooogle -->
...
<!-- Gooogle -->
The following sam
command will find this snippet and delete it:
,x<!-- Gooogle -->(.|\n)*<!-- Gooogle -->/ d
The ,
means the starting range is the entire file. The x
command executes another comand for each part of the range that matches the regular expression - in this case the entire Google Ad snippet. The command to execute for each match is d
which is to delete the region. To have acme
run this on all open HTML files I used:
Edit X/html?$/ ,x<!-- Gooogle -->(.|\n)*<!-- Gooogle -->/ d
The X
command iterates over all open windows with the name matching the regular expression and runs the following command, which I described above. This results in all the ad snippets from being removed from all the HTML files. To save them back to disk:
Edit X/html?$/ w
And to close the acme
windows:
Edit X/html?$/ D
The task is done, closing acme
and unmounting the remote file system:
% unmount /n/host
This task was pretty simple and could be done with a number of existing Unix tools but I wanted to give Acme a try with a real world task. It worked well editing remote files, and iterating towards the correct sam
commands to use was easy by interactively testing on single files and using the p
command to print ranges instead of deleting. Make sure authentication is set up before sharing remote files though!