Emacs Misc Tips

Advertise Here

,

This page is a collection of short emacs tips.

Avoid lambda When Defining A Hook

When you define a hook, it is best avoid using lambda in its function. Instead, define the function of what you want to do, then then add that function name to the hook. Let me explain.

Here's a hook definition, written in the way most people would write it:

;; modify nxml-mode's shortcut keys
(add-hook 'nxml-mode-hook
 (lambda ()
 (define-key nxml-mode-map (kbd "<f8>") 'browse-url-of-buffer)
 (define-key nxml-mode-map (kbd "<tab>") 'html-next-content)
 (define-key nxml-mode-map (kbd "S-<tab>") 'html-previous-content)))

However, it is better done like this:

(defun xah-xml-mode-keys ()
  "Modify keymaps used by xml-mode."
  (interactive)
 (define-key nxml-mode-map (kbd "<f8>") 'browse-url-of-buffer)
 (define-key nxml-mode-map (kbd "<tab>") 'html-next-content)
 (define-key nxml-mode-map (kbd "S-<tab>") 'html-previous-content)
  )

(add-hook 'nxml-mode-hook 'xah-xml-mode-keys)

There are minor advantages in doing it this way.

When a hook is defined by a function symbols, you can now remove some of your hook stuff easily by calling “remove-hook”.

Also, suppose you are using command-frequency-mode to know what commands you use the most. If you have lambda, the data will become unreadable with bunch of lambda code, instead of command names.

Of course, lambda in hook is so convenient, typically just used once in your emacs init file. Usually, you don't need to manipulate it with remove-hook or manipulate it by other code. But if you are using hooks in your elisp code in a package, it's best to define functions instead of using lambda in hooks. That way, the hook can be programmatically manipulated, and is often done by emacs.

Show Current File In Dired

If you have a file open and you need to go to the directory the file is in, you can just call “dired-jump” 【Ctrl+x Ctrl+j】, and it'll take you to the dir and place your cursor on the file.

You could just use find-file 【Ctrl+x Ctrl+f】 then press Enter, because that opens the current dir and the current dir is usually the dir the file is in. If the dir has a hundred files, then you have to do a search command to locate the file. The “dired-jump” comes to the rescue. (You want the cursor on the file name because you might need to copy or rename the file, etc.)

In the past, i actually defined my own command for this:

; DON'T USE THIS COMMAND. use dired-jump instead.
(defun show-file-in-dired ()
  "Show the current file in dired.
DON'T USE THIS COMMAND. This command was written before i knew dired-jump."
  (interactive)
  (let (fName dName)
    (setq fName buffer-file-name)
    (setq dName (file-name-directory fName) )
    (dired dName)
    (search-forward (file-name-nondirectory fName))
    )
  )

Uncolor Current Region

Sometimes, i want to take out all the syntax coloring of some text. Never knew how to do that … but today i discovered font-lock-unfontify-region. Unfortunately, it's a function not command, so you cannot call it with 【Alt+x】. So, i make one:

(defun unfontify-region (begin end)
  "Unfontify text selection.
See also: `font-lock-fontify-block', `font-lock-fontify-buffer'."
  (interactive "r")
  (font-lock-unfontify-region begin end)
  )
blog comments powered by Disqus