I often edit config files with:
sudo emacs /etc/whatever
The problem with that is I would end up with ~ backup files littering my filesystem and also emacs is run as root so it gets root’s config file. I’d rather use emacs with my config.
The solution, thanks to emacs-fu is twofold:
First set emacs’s backup directory, which tells it to save backup files in a
specific directory rather in the same dir as the file you edited. Put something like this in your emacs init file:
(setq
backup-by-copying t ; don't clobber symlinks
backup-directory-alist
'(("." . "~/.saves")) ; don't litter my fs tree
delete-old-versions t
kept-new-versions 6
kept-old-versions 2
version-control t) ; use versioned backups
Then you add this to your .bashrc:
# see http://emacs-fu.blogspot.com.au/2011/12/system-administration-with-emacs.html
# edit file with root privs
function E() {
filename=$1
without_beg_slash="${1##/}"
if [[ $without_beg_slash == $1 ]];then
filename="${PWD%//}/$1"
fi
emacsclient -c -a emacs "/sudo:root@localhost:$filename"
}
Now just edit files with: E someconfigfile
and it loads it in your emacs, asks your pw to edit and off you go.
Leave a Reply