My funky ZSH prompt
Here’s the config for my ZSH prompt:
local blue_op="%{$fg[blue]%}[%{$reset_color%}"
local blue_cp="%{$fg[blue]%}]%{$reset_color%}”
local path_p=”${blue_op}%~${blue_cp}”
local user_host=”${blue_op}%n@%m${blue_cp}”
local ret_status=”${blue_op}%?${blue_cp}”
local hist_no=”${blue_op}%h${blue_cp}”
local smiley=”%(?,%{$fg[green]%}:%)%{$reset_color%},%{$fg[red]%}:(%{$reset_color%})”
PROMPT=”╭─${path_p}─${user_host}─${ret_status}─${hist_no}
╰─${blue_op}${smiley}${blue_cp} %# “
local cur_cmd=”${blue_op}%_${blue_cp}”
PROMPT2=”${cur_cmd}> “
It looks like this. 
It has two lines. The first displays the path of the current working directory, then the user and hostname, then the return value of the last executed command, and the last item is the command’s number in the history.
The second line displays a green smiley, if the last command worked well, or a red smiley, if it failed.
I took some inspirations from the prompt of strcat, see http://www.echox.de/blog/archives/74-ZSH-Prompt.html.
Tags: prompt, shell, ZSH
Today I wrote an extension for emacs that lets you view pdf, ps or dvi documents in emacs. It’s named doc-view.el and I posted it to the emacs-sources mailing list (here it is). But I recommend that you get the latest version by cloning my Git repository.
A description of doc-view.el and how to get it can be found on my homepage.
UPDATE: doc-view is official part of Emacs and the default viewer for DVI and PDF files.
Directory local variables
You probably know file local variables. If not read (info “(emacs)File Variables”). They’re quite handy if you work on projects that use different coding conventions, e.g. some indent with tabs and others don’t. The problem with this approach is that it requires you to modify the files.
To escape this restriction I came up with this:
(defvar th-dir-local-variables-alist
'(("~/repos/gnus/" . ((indent-tabs-mode . t)
(tab-width . 8))))
"An alist with (PATH . LIST) pairs. PATH is a path and LIST is
a list of variables to set locally for files below that path. It
has elements of the form (VAR . VAL) where VAR is a symbol and
VAL is its value.")
(defun th-set-dir-local-variables ()
"Locally set the variables defined in
`th-dir-local-variables-alist' for the current buffer."
(interactive)
(let ((file (buffer-file-name (current-buffer))))
(when file
(dolist (pair th-dir-local-variables-alist)
(when (string-match (concat "^" (regexp-quote (expand-file-name (car pair))))
file)
(dolist (var (cdr pair))
(if (local-variable-if-set-p (car var))
(set (car var) (cdr var))
(set (make-local-variable (car var)) (cdr var)))))))))
(add-hook 'find-file-hook
'th-set-dir-local-variables)
Yesterday I posted a command that executes emacs commands by giving an abbreviation. Today I’ve built a complete global minor mode around it. Here’s the file’s description:
This file includes the command `exec-abbrev-cmd’ which lets you execute a
command by giving it in an abbreviated form, where the abbreviation takes
the first character of each word in the command name. For example “g” is an
abbreviation for the command `gnus’, “eb” is an abbreviation for
`emms-browser’ and “omm” is an abbreviation for `outline-minor-mode’. Of
course it is possible, that an abbreviation matches several commands,
e.g. “g” matches not only `gnus’ but `grep’, `gdb’ and some more. In such
cases you will be queried, which command to use.
To have this functionality quickly accessible you might want to bind it to
some key. That’s what I use:
(add-to-list 'load-path "~/elisp") ;; Where is exec-abbrev-cmd.el?
(require 'exec-abbrev-cmd) ;; Load it.
(global-set-key (kbd "C-x x") 'exec-abbrev-cmd)
Now you’ll say, “Wow, what a nice feature!”, but it’s even getting better.
Let’s say you often invoke `customize-face’ with `C-x x cf RET’ and then
choosing from the completion list between `copy-file’ and `customize-face’.
Always `copy-file’ is selected first, because it’s lexicographically before
`customize-face’. As a solution to this problem there’s
`exec-abbrev-cmd-mode’, a global minor mode that does bookkeeping how often
you invoke a command with `exec-abbrev-cmd’, so that the list of commands
you have to choose from is sorted by the frequency of command invokation.
After a while in most cases `C-x x <abbrev> RET RET’ will do what you want.
If you want to enable this feature put this in your ~/.emacs:
(exec-abbrev-cmd-mode 1)
Have fun!
You can get exec-abbrev-cmd.el from my elisp darcs repository.
UPDATE: I moved all my projects to Git repositories. Have a look at my homepage for more infos on exec-abbrev-cmd.el.
Today I looked at my ~/.emacs file and I saw that I defined a lot of aliases for commands I frequently use. Most of the time the aliases are words that are built by taking each first char of a word in the command, e.g. nsn for newsticker-show-news, eb for emms-browser or omm for outline-minor-mode. But what should I do when I want to create an alias for ediff-buffers since eb is already used?
My solution was to write a function that calls a command by its abbreviation.
(defun th-execute-abbreviated-command (prefixarg)
"Queries for a command abbreviation like \"mbm\" and calculates
a list of all commands of the form \"m[^-]*-b[^-]*-m[^-]*$\”. If
this list has only one item, this command will be called
interactively. If there a more choices, the user will be queried
which one to call.
The PREFIXARG is passed on to the invoked command.”
(interactive “P”)
(let* ((abbrev (read-from-minibuffer “Command Abbrev: “))
(regexp (let ((char-list (append abbrev nil)) ;; string => list of chars
(str “^”))
(dolist (c char-list)
(setq str (concat str (list c) “[^-]*-”)))
(concat (substring str 0 (1- (length str))) “$”)))
(commands (remove-if-not (lambda (string) (string-match regexp string))
(let (c)
(mapatoms (lambda (a)
(if (commandp a)
(push (symbol-name a) c))))
c))))
(cond ((> (length commands) 1)
(call-interactively (intern
(if ido-mode
(ido-completing-read “Command: ” commands)
(completing-read “Command: ” commands)))
t))
((= (length commands) 1)
(call-interactively (intern (car commands)) t))
(t (message “No such command.”)))))
To make it quickly accessible I’ve bound it to C-x x.
(global-set-key (kbd "C-x x") 'th-execute-abbreviated-command)
UPDATE:
Alex Schröder pointed me to the builtin partial-completion-mode which allows nearly the same. It requires that you type the separator (- or _), too, so you need a few keystrokes more when you know the commands name. On the other hand it completes not only function names, but also files, e.g. f_b.c will expand to foo_bar.c.
UPDATE2:
The version above uses ido-completing-read when ido-mode is enabled.