Emacs, PHP and the tilde

Working at the commandline, my favorite editor is Emacs. When installing a new OS, you are ‘forced’ to use VI at the start, but Emacs gets installed ASAP. One of Emacs’ standard behaviours, is creating a backup file automatically. But, when editing this file, this behaviour might be unwanted: in general, .php-files get served as php, but .php~ -files get served as text! This means that when you store your database connection in config.php and you change it once, you have a config.php~ wihth username and password exposed to the world…
Since I, also, tend to forget cleaning up the backup files, I wanted to get rid of this problem. I found a couple solutions:

  • Tell Apache to serve .php~ files as php
  • Tell Apache to deny access to .php~ files
  • Change the default location for the backup files
  • Tell Emacs to stop making backup files

Tell Apache to serve .php~ files as php

This is done quite easily. In the file ‘mime.types’, locate the line containing php and make it look like this:
application/x-httpd-php phtml pht php php~
Restart Apache and you’re done.

Tell Apache to deny access to .php~ files

Done as easy as the first rule: open your httpd.conf and locate the part about the AccessFilename. Tell Apache to stop serving all files that end with a tilde by adding this:
< Files ~ "~$" >
Order allow,deny
Deny from all
< /Files >

This causes Apache to generate a 403: Permission Denied to all files end with a tilde. Restart Apache and your done.

Change the default location for the backup files

I found this trick at Ubuntu Blog where a guy called Kyle added a comment about an auto-cleanup script. It’s about editing your .emacs file: the file with your emacs preferences. The file should be in your home directory. If not, you can just create it. Add the following lines to the file:
;; create a backup file directory
(defun make-backup-file-name (file)
(concat “~/.emacs.backups/” (file-name-nondirectory file) “~”))

Now, when a file is edited, a backup is created in ~/.emacs.backups/. Backups are still present, which can be quite convenient.

Tell Emacs to stop making backup files

This is the one I was looking for. Found it via Google at Rensselaer. Edit your .emacs file and add the following:
(setq make-backup-files nil)
I wonder how long it will take me to regret this sinces typo’s killing now… But, my .php~ files won’t show anywhere at all anymore. At a single-user environment, this can be the way to go. Since I work in a multi-user environment, my favorite solution won’t be sufficient. That’s why I also use the first (two) methods.

Comments, thoughts, cents, best practices are welcome!

Add to your favorites:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Reddit
  • StumbleUpon

This entry was posted on Friday, July 20th, 2007 at 22:17 and is filed under Blog, Commandline, Emacs, Tech, Unix general. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

4 Responses to “Emacs, PHP and the tilde”

xurizaemon May 4th, 2009 at 22:46

your code as posted above doesn’t quite work right – the <Files> container needs to not have spaces at the end of each tag

<Files ~ “~$”>
Order allow,deny
Deny from all
</Files>

xurizaemon May 4th, 2009 at 22:46

but, thanks for the post ;)

xurizaemon May 4th, 2009 at 22:47

…. oh, it smart-quoted my post above. thanks, wordpress. you make my day, every day that you try to be helpful …

Jaap May 5th, 2009 at 08:32

Hi xurizaemon ,

Thanks for your comments. You’re right, the spaces shouldn’t be there. When I wrote the post, Wordpress wouldn’t convert the code without spaces, because the code was interpreted as HTML.

So I agree: thanks wordpress…

Leave a Reply