Skip to content

So I signed up for twitter…

Feb 24 11
by mickey

… and I thought I would never jump on that bandwagon, but here I am. I get a significant amount of my traffic from people twittering — so why not?

Follow me here:

http://twitter.com/mickeynp

Toggling between Python buffers

Feb 23 11
by mickey

I work with Python a lot, and as any swashbuckling Pythoneer knows, using the Python REPL is a big part of the day-to-day routine.

I often find myself switching from one python buffer to the shell (also known in Emacs lingo as the inferior python process) and back again. That means copious use of C-x b or winner-mode yet.. it just doesn’t feel right. Sure, I could use C-c C-z (in Emacs’ own Python mode) but to me that’s a different use case.

I wanted something easier, so I wrote this a while back to toggle from a Python buffer to the shell, and from the shell you can go back to the last buffer you toggled from. In effect, it works like a very localized “alt-tab”.

Now I can quickly toggle between buffer and shell with a keypress. I’ve set it up so it won’t split windows but will instead switch the active buffer — like when you use C-x b.

I bind the toggle command to F12 in all Python buffers and in the shell, so it’s easy to reach. Feel free to change the keybind if you like (read Mastering Key Bindings in Emacs to learn how.)

Note: I can’t promise it’ll work in the non-Emacs Python mode!

(require 'python)
 
(defvar python-last-buffer nil
  "Name of the Python buffer that last invoked `toggle-between-python-buffers'")
 
(make-variable-buffer-local 'python-last-buffer)
 
(defun toggle-between-python-buffers ()
  "Toggles between a `python-mode' buffer and its inferior Python process
 
When invoked from a `python-mode' buffer it will switch the
active buffer to its associated Python process. If the command is
invoked from a Python process, it will switch back to the `python-mode' buffer."
  (interactive)
  ;; check if `major-mode' is `python-mode' and if it is, we check if
  ;; the process referenced in `python-buffer' is running
  (if (and (eq major-mode 'python-mode)
           (processp (get-buffer-process python-buffer)))
      (progn
        ;; store a reference to the current *other* buffer; relying
        ;; on `other-buffer' alone wouldn't be wise as it would never work
        ;; if a user were to switch away from the inferior Python
        ;; process to a buffer that isn't our current one. 
        (switch-to-buffer python-buffer)
        (setq python-last-buffer (other-buffer)))
    ;; switch back to the last `python-mode' buffer, but only if it
    ;; still exists.
    (when (eq major-mode 'inferior-python-mode)
      (if (buffer-live-p python-last-buffer)
           (switch-to-buffer python-last-buffer)
        ;; buffer's dead; clear the variable.
        (setq python-last-buffer nil)))))
 
(define-key inferior-python-mode-map (kbd "<f12>") 'toggle-between-python-buffers)
(define-key python-mode-map (kbd "<f12>") 'toggle-between-python-buffers)

New python mode

Feb 17 11
by mickey

Seems there’s a new kid in town.

Fabián Ezequiel Gallina has announced a new Emacs mode to, I hope, merge with the existing GNU Emacs mode (referred to as python.el) and possibly merge with the other Emacs mode out there (referred to as python-mode.el) and bring balance to the force.

This is great news. I have been meaning to write an article covering All Things Python in Emacs and if I can do that and only cover one mode that’ll make my life a whole lot easier. Currently the Emacs-Python community is way too fragmented due to license issues with python-mode and python.el not keeping up with the times.

Mastering Key Bindings in Emacs

Feb 8 11
by mickey

Altering the key bindings in Emacs should not, on the face of it, be a difficult task. But there’s a reason why the Emacs manual has dedicated 30-odd pages to describing, in great detail, all the subtleties and nuances of how to bind keys.

To save you the time of reading all of that, I’ve written a guide that covers what you need to know to bind keys to even complex commands, and a set of templates you can use in your own code.

read more…

Find files faster with the recent files package

Jan 27 11
by mickey

I bet the majority of files you edit on a day-to-day basis are the same ones over, and over again. For that reason I recommend you use Emacs’s recentf package, it is a great — and very sophisticated, like all things Emacs — utility that keeps track of recently used files.

I supercharge recentf by adding Ido mode support (if you don’t know what Ido is, read Introduction to Ido Mode); and by overriding C-x C-r, bound to find-file-read-only, a useless feature I never use. Note: the Ido supercharging only works if you have ido-mode enabled in the first place!

Note that unlike find-file I’ve opted to display the entire filepath in Ido’s completion engine as I often find that a directory or remote host is the only disambiguator if there are multiple files with the same name. I’ve thought about filtering the list of recent files through the uniquify module for buffers but that’s for another time.

Here’s what you need to add to your .emacs:

(require 'recentf)
 
;; get rid of `find-file-read-only' and replace it with something
;; more useful.
(global-set-key (kbd "C-x C-r") 'ido-recentf-open)
 
;; enable recent files mode.
(recentf-mode t)
 
; 50 files ought to be enough.
(setq recentf-max-saved-items 50)
 
(defun ido-recentf-open ()
  "Use `ido-completing-read' to \\[find-file] a recent file"
  (interactive)
  (if (find-file (ido-completing-read "Find recent file: " recentf-list))
      (message "Opening file...")
    (message "Aborting")))

Make script files executable automatically

Jan 19 11
by mickey

You can force Emacs to make a file executable (respecting your umask settings) if Emacs considers it a script. To determine if it is a script, Emacs will look for the hash-bang notation in the file and treat it as a script if it finds it.

Add this to your .emacs and Emacs will then make the file executable if it is a script.

(add-hook 'after-save-hook
  'executable-make-buffer-file-executable-if-script-p)

Effective Editing I: Movement

Jan 14 11
by mickey

If you can master movement and editing in Emacs, you have effectively conquered two of the biggest productivity boosters available to you. Emacs has unrivaled movement and editing capabilities and aside from enabling Ido Mode, few other things in Emacs will, pound-for-pound, give you a bigger productivity boost.

read more…

Making deleted files go to the trash can

Dec 30 10
by mickey

In Emacs 23.1 support for your operating system’s trash can (or recycle bin, or whatever) was added. File deletions in Emacs now uses your system’s trash can and the deleted files will be put there instead. The feature must be enabled manually by adding this to your .emacs:

(setq delete-by-moving-to-trash t)

The delete to trash functionality will obviously behave differently depending on your operating system. On Windows the special function system-move-file-to-trash is defined because Windows exposes its own API for handling files sent to the recycle bin. On other operating systems that function will be nil, and the default behavior provided by move-file-to-trash is used instead.

In Emacs 23.2 new functionality was added to ensure Emacs conforms to the freedesktop.org specification used by all major, free desktop environments. The new variable is trash-directory and determines where Emacs will put the deleted files. If the variable is nil the freedesktop.org trash can default is used, otherwise the variable must contain a path string to where the files are to be put.

Fixing the mark commands in transient mark mode

Dec 22 10
by mickey

Most people, myself included, use transient-mark-mode (or just tmm), a great piece of kit that makes Emacs behave sanely by using visible regions like other editors. But tmm can get in the way of Emacs’s point and mark system, as the mark also serves as a handy beacon that you can set with C-SPC and jump to with C-u C-SPC.

Sadly, most of the point and mark commands generally go unnoticed by people who never used Emacs without tmm. That’s a shame, because mastering the mark commands will greatly speed up movement and editing.

In tmm the command C-SPC activates the region and lets you select text as you see fit. That means if you want to set the mark (and nothing else) you have to follow up the preceding command with another C-SPC (or C-g) to abort the active region “selection.” That’s too much typing for something that should be second nature.

I use the following snippet below to explicitly set the mark. I bind it to C-`, an unused key.

(defun push-mark-no-activate ()
  "Pushes `point' to `mark-ring' and does not activate the region
Equivalent to \\[set-mark-command] when \\[transient-mark-mode] is disabled"
  (interactive)
  (push-mark (point) t nil)
  (message "Pushed mark to ring"))
(global-set-key (kbd "C-`") 'push-mark-no-activate)

The C-u C-SPC command is trickier, as the functionality in tmm can be very useful for precision work not served by specialist commands like mark-defun or kill-sexp. To jump to the mark I replace the binding on M-` — an altogether useless command that opens up a terminal-friendly menu bar in the minibuffer — with a dedicated “jump to mark” command.

(defun jump-to-mark ()
  "Jumps to the local mark, respecting the `mark-ring' order.
This is the same as using \\[set-mark-command] with the prefix argument."
  (interactive)
  (set-mark-command 1))
(global-set-key (kbd "M-`") 'jump-to-mark)

The exchange-point-and-mark, bound to C-x C-x, will by default activate the region when it is invoked. You must use the prefix argument to suppress the activation, but I find that to be too cumbersome for day-to-day use so I disable it outright. The snippet below will do this, so if you don’t want that to happen don’t use the snippet below!

(defun exchange-point-and-mark-no-activate ()
  "Identical to \\[exchange-point-and-mark] but will not activate the region."
  (interactive)
  (exchange-point-and-mark)
  (deactivate-mark nil))
(define-key global-map [remap exchange-point-and-mark] 'exchange-point-and-mark-no-activate)

And there you have it. Mark commands without the interloping tmm to spoil the fun.

Mastering Eshell

Dec 13 10
by mickey

There are several shells for Emacs, but none can match the versatility and integration with Emacs like Eshell. Eshell is a shell written entirely in Emacs-Lisp, and it replicates most of the features and commands from GNU CoreUtils and the Bourne-like shells. So by re-writing common commands like ls and cp in Emacs-Lisp, Eshell will function identically on any environment Emacs itself runs on.

Unfortunately, there is a problem: Eshell is woefully underdocumented — a rare sight in GNU Emacs — so I’ve compiled this guide to help people make full use of what Eshell has to offer.
read more…