It’s a simple thing to ask. You want emacs to nicely indent your code. Emacs comes with two perl editing modes: perl-mode and cperl-mode. For many years, I have used cperl-mode happily. However, $dayjob prefers the way perltidy arranges code and I have come to agree.

If you would like to get perltidy to format your code in emacs, you will need to do the following:

  • install emacs25
  • install Perl::Tidy
  • install tramp
  • install perltidy.el

This is a lot more work than it should be, but this is what I needed to do to get this working under Debian 9.4. Maybe your mileage will vary.

Installing emacs25

This is pretty easy to do on debian:

  $ sudo apt-get install emacs25-nox

If you want the X version of emacs, leave off -nox from the package name.

Installing Perl::Tidy

This too is pretty straight forward for perlers:

  $ sudo cpan install Perl::Tidy

There are no XS parts of Perl::Tidy so this usually goes fast. Note that if you are using something like Perlbrew or plenv, you probably still want to globably install Perl::Tidy rather than make this a per-project module.

Perltidy’s default styling can be overridden by a local ~/.perltidyrc. Here’s mine:

# -*-conf-unix-*-
# This is how $dayjob thinks of the world and I agree
# --outdent-labels --outdent-label-characters=2
# --break-after-all-operators
# exception: "or" and "and" read better at the start of the line
-wbb="or and ->"
# ToDo: Currently we break after these operators:
--want-break-after='. , && || **= += *= &= <<= &&= -= /= |= >>= ||= %= ^= x= .='
--want-break-before='or and -> ? : << >> -> // % + - * / x != == >= <= =~ < > | &'
--closing-side-comment-prefix="# end"
-cscl="sub : BEGIN END"
# Allow up to two consecutive blank lines.
-vt=2   # Maximal vertical tightness
-cti=0  # No extra indentation for closing brackets
--paren-tightness=2   # High parenthesis tightness
-bt=1   # Medium brace tightness
-sbt=1  # Medium square bracket tightness
-bbt=1  # Medium block brace tightness

Installing tramp

Tramp is a dependency of the perltidy emacs package. Unfortunately, it does not appear to come with the Debian emacs25 package, nor did I find a debian package for this. This leaves us with a from-source code install.

You will need autoconf:

  $ sudo apt-get install autoconf

Clone the git repo into a convenient directory (e.g. ~/src/emacs):

   $ git clone git://

And then build it:

  $ cd tramp && autoconf && make && make check

There were a few unit test failures, but I ignored them.

Now let emacs know about this package by adding the following lines to ~/.emacs:

  ;; tramp stuff 
  (add-to-list 'load-path "~/src/emacs/tramp/lisp/")
  (add-to-list 'Info-directory-list "~/src/emacs/tramp/info/")
  (require 'tramp)

Be sure to get the paths right in those add-to-list directives.

Installing perltidy.el

If you made it this far, you’re a linux/unix pro! The last step is easy.

  $ mkdir -p ~/.emacs.d/lisp ; wget -O ~/.emacs/lisp/perltidy.el

This adds the perltidy lisp bindings to your .emacs.d directory of local packages. Change the paths to reflect your organization scheme.

If you have not yet done so, tell emacs about your local emacs packages by adding the following to ~/.emacs:

  (add-to-list 'load-path "~/.emacs.d/lisp/")

FINAL STEP: Tell emacs about the perltidy package by editing ~/.emacs

;; perltidy
(require 'perltidy)

Mostly, I use M-x perltidy-dwim on a buffer to indent it, but here’s how to bind that operation to the F12 key:

(global-set-key (kbd "") 'perltidy-dwim)

I hope you guessed that this line is added to your ~/.emacs file after the require line.

I hope this makes your life of indenting perl code in emacs all the more pleasant.