September 18, 2008

Essential Emacs Knowledge

Filed under: blogging,emacs — jonEbird @ 9:33 pm

I am on a island at work and there is no one else to converse with other than Wilson. The island is called “Isle de Emacs” and Wilson is, I guess, psychoanalyze-pinhead. But occasionally I do get a visitor to the island and the conversation is more or less the same stuff. Okay, enough with the analogy, this short tutorial is about getting you acquainted with using the Emacs editor. The repeated conversation I was referring to was an introduction to Emacs and trying to educate my co-worker on both the essential pieces of knowledge and a bit of Emacs philosophy to help guide their learning process.

Essential Knowledge

- In case you don’t know, emacs is really just a lisp interpreter.
  Means it’s nearly infinitely extensible.
  P.S. Also, in case you don’t know, Lisp is a programming language. That means practically everything the editor is supporting, from moving the cusor word by word to saving and opening files, is actually being done via code written in Lisp.
- Files being opened and edited are referred to as buffers.
  To say you are “closing a buffer” means you are closing that file and will no longer have it open.
  Similarly, when we say “switch to buffer” means that you are changing the current screen from what you are viewing/editing and moving to the next file.
- In keyboard shortcut notation, (C-x f) means to hold the
<control> key then depress <x>, then releasing and then
pressing <f>.
  In general, a space means you should stop pressing any keys and then move onto the next key sequence.
- Emacs commands are often a two sequence routine.
  Open a file (C-x C-f), save (C-x C-s), quit the editor (C-x C-c).
  Psst. (C-x) is common starting sequence, particularly for the basic operations.
- What does the “M” character stand for in keyboard sequences?
  It is called the “Meta” key and to execute it, you can use the <Alt> key or you can alternatively hit <Esc> followed by the next key. If you use the <Esc> key, you do not depress both keys at the same time, whereas with the <Alt> you do hit both keys at the same time.
  So, to get the command prompt (technically executing execute-extended-command), the sequence is (M-x). With the <Esc> method, it’s hit Escape, then hit “x”, whereas with the <Alt> you would start by holding <Alt> and then depress “x”.
  I used to use the <Esc> key method, but forced myself to use the <Alt> method in order to keep my fingers on the home keys for speed.
- To set a keyboard shortcut, it is called a “binding”.
  The interactive emacs command to do this is called “global-set-key”.
- The equivalent to “man man” of learning shell skills is (C-h ?)
  There you will see various types of help. You can search the building commands, you can query what a keyboard sequence will execute and much more.
- Emacs can do nearly everything, but it is fairly esoteric in it’s approach to being your text editor.
  What this means is that it’s learning curve is pretty steep. If you try to climb that hill initially, you’ll probably give up.
  Try to just learn the essentials and don’t bother with anything else. Learn one thing at a time. Use the menu, use the mouse, use your arrow keys, the pgdn and pgup keys, etc. Try to use the editor just like you would use notepad, but understand that one of the goals of a advanced emacs user is to not need the menu, scroll bars, mouse, etc.
  I once attended a RedHat training course where the instructor called himself a “emacs guy” but watching him actually use the editor appeared to me like he just started using it last week. But hey, that is totally cool, and really, the correct approach for learning emacs.
- Why should I invest in a learning a editor with such a steep learning curve?
  Because it can do nearly anything and can be customized for everything. Without going into a full dissertation, this means that the effort you are investing into this editor will not be put to waste. And moreover, you will not invest efforts just to find that you’ve reached the extent of what the particular editor can do.
- When you use (M-x) to enter a command, if there is a keyboard shortcut, the mini-buffer will tell you what it is.
  Psst: Can also use (C-h b) to show a helping page about the keyboard bindings.
- You can Tab-complete nearly everywhere.
  First place you’ll notice is when opening a file, but you can use within the mini-buffer too. Can be used to complete emacs commands,
  variable names, etc.
- So, you’ve forgotten that command but you remember it’s about the keyboard?
  Search the commands, (C-h a) then just type “key” and ENTER to search for commands with “key” in the name.
- In case you get yourself stuck or just not sure what you just pressed, there are two common ways to quit out.
  (ESC ESC ESC) – Hiting Escape three times gets you out of what you’re doing. This actually works for closing out that split screen help window too.
  The other way to quit out of items is to use (C-g), but there are scenarios where ESC ESC ESC works and C-g doesn’t.
- Customization is done within your ~/.emacs file.
  You are technically executing emacs lisp here. Since you’re reading this, I’ll assume you’re a newbie and as such, just go and copy somebody else’s .emacs file. If you’re still a Emacs user after a year, perhaps you’ll finally feel adventurous enough to dive in there and generate your own customizations.
- Support for different types of files is done via different “modes”.
  Your current “mode” is displayed in the status bar, at the bottom of the editor, in parentheses.
  To change to text mode, for example, execute the command text-mode (M-x text-mode). In general, to enter any mode, it’s going to be the modename followed by “-mode”. Means, for Python, execute python-mode.
  A cheap way to see how many modes Emacs supports is to do a apropos search on “mode”. That is, (C-h a “mode”) to show each command with the word “mode” in it.
- How do I automatically execute something when entering a particular mode?
  Although not an essential piece of knowledge, to execute something when entering a mode to to “create a hook”. By convention, the hooks are stored by the variable matching the mode name and adding “-hook” to the end. So the hook for executing something when I enter “outline” mode is stored in the variable “outline-mode-hook”. I didn’t use that example by accident either. Here is my hook, used in my .emacs file:
(add-hook ‘outline-mode-hook ‘hide-body)
  Even without any Lisp knowledge, that should be fairly intuitive. It will automatically fold the body of each section within my outline document upon opening it.
  Psst: Try sticking a “-*- outline -*-” as the first line of your outline file while keeping the file extension .txt. Without that line, emacs will assume it’s plain text because of the .txt extension, but with that line, you are telling emacs to use outline-mode for this file. It’s great for keeping notes.
  I use it for keeping notes on every single work request ticket I receive at work. Such a file could become beastly in size but I don’t notice since it’s all rolled up when I re-open it.

Essential Habits.

- Keep your buffers open
  Learn to switch between buffers (C-x b TAB)
  I, myself, keep my emacs editor open at all times. I never close it. I keep buffers of my week’s work open for weeks on end. I’ll pretty much only close buffers when it becomes too cluttered when switching between buffers (C-x b TAB).
  Finally, I currently have my EDITOR variable set to “emacsclient” which means commands such as “crontab” connect to that single running emacs editor.
- Tab complete everything.
  Just like you should be doing in the shell, tab-complete commands, filenames, etc within emacs.
- Use the desktop-save feature.
  This tip suppliments the habit of keeping your buffers open. The desktop-save allows you to keep track of all buffers currently open and to reopen them upon restarting emacs. This is useful on laptops where you won’t be able to keep a copy of emacs always running.
- Pay attention to the keyboard shortcuts which are listed in the menu as you navigate to execute them.
  This goes along with the “learn one trick at a time” suggestion. Next time you get tired of executing that command from the menu, instead learn the keyboard shortcut for it.
  Psst: If you are curious what emacs lisp command is being executed in the menu, you can still use the keyboard help command (C-h k). People normally use that help command to tell them what command is being executed for the keyboard sequence but few realize that it works for menu items as well.

Essential Tricks

- (C-x ESC ESC) – Repeat complex sequence
- Want to assign a keyboard shortcut to something which you do alot?
  E.g. By default “goto-line” is not set to a shortcut. Let’s say you want to assign to (C-c g).
         Run (M-x global-set-key). It will prompt you what keyboard sequence to use and what to execute.
         Next run, (C-x ESC ESC) which is for repeat complex routine. At this point, the sequence is listed in emacs-lisp form in the mini-buffer.
         Run (C-x o) to get to the mini-buffer so you can copy that text (C-a C-space C-e M-w) and return to the normal buffer (C-x o).
- If operating in a corporate environment with a jump machine segregating your workstation and the rest of your environment, use TRAMP.
  Tramp allows you edit remote files over various protocols. I recommend using “ssh” and to that point, stick these lines into your .emacs file:

(setq tramp-default-method "ssh")

- Redefine how your backups are made.
  Emacs will automatically make backups of your files as you are editing them. By default it sticks a “~” character at the end of the file.
  Well, this is quite annoying for me since it will also keep file permissions. If I’m creating a script, now I can’t get a unique tab-completion in the shell by typing (./scr TAB) to expand to “./”. Instead it finds “” also. Move your backups to ~/.backups directory. Create that directory and then add these lines to your .emacs file:
(defun my-make-backup-file-name (file-name)
  “Create the non-numeric backup file name for `file-name’.n
   This customized version of this function is useful to keep all backups
   in one place, instead of all over the filesystem.”
  (require ‘dired)
  (message (concat “make-backup: ” file-name))
  (if (file-exists-p “~/.backups”)
      (concat (expand-file-name “~/.backups/”)
          (dired-replace-in-string “/” “|” file-name))
    (concat file-name “~”)))
(setq make-backup-file-name-function ‘my-make-backup-file-name)
- Use In fact, the last backup trick was taken from there.
  Just as I recommended above, don’t try to become an expert overnight. Learn the basics well and then pick up one trick at a time. I believe this can be a never ending process as you’re goal should be to become more and more proficient in your editor. So, go out and learn just one new trick and don’t come back until that new trick is ingrained into your finger’s memory.

Essential Memorization List

- (M-x) – Execute emacs command.
- (C-x u) – Undo.
- (M-x revert-buffer) – Oops, I just totally messed this file up and need to revert from the saved copy.
- (C-x o) – Move cursor to other split window pane.
  Nice for looking through the help buffer after it just split your screen.
- (C-x 1) – Keep current buffer and remove other split pane.
  I.e. Get that other window out of my way, please.
- (C-x 0) – Keep the _other_ buffer and remove this one.
  Just like the previous, but you’re keeping the other buffer as the sole window.
  Nice when you’re paging through an apropos section and are done. Use this to close the window without switching back first.
- (C-x C-f) – Open file
- (C-x C-s) – Save file
- (C-x C-c) – Exit

The goal of this short guide was to arm you with the necessary knowledge
in order to get you going with one of the most powerful editors around.
I didn’t necessarilly want to write a whole series of Emacs weblog entries. Instead, I just did a brain dump and I hope it wasn’t too overwhelming. Instead, I’d rather hope it helps
guide you to the island. We’ll play some volleyball, snorkel, hunt peacocks,
etc; you know, island stuff.

Fixing my Weblog

Filed under: blogging — jonEbird @ 8:40 pm

Some time ago I lost my harddrive. At that point, I was making infrequent backups to the laptop. Not very admin-like for someone who’s profession is a Systems Administrator. It could have been much worse, but still a pain none the less.

I had to solve various issues with my home setup. First the harddrive vulnerability. For that, I bought two 320GB harddrives and setup a /dev/md0 RAID0 mirror. After that, I installed a fresh copy of Fedora Core 9. I got WordPress back up but honestly my site was still limping along. I left it that way for too long and have finally cleaned things up tonight, culminating the work by upgrading to WordPress 2.6.2.

Now let’s get back to writing more weblog entries.