Tilde (backup file) cleanup January 26, 2006
Posted by Carthik in snippets, ubuntu.trackback
In response to my request for tips from users, a few have written in with their favourite tips. I will be posting them here, in the order received, one or more per day.
First, Clifton Snyder has a tip to share with everyone.
Several text editors these days – Emacs, jEdit, et al – leave behind pesky tilde-files after you’ve edited/saved them. This can lead to a cluttered mess of extraneous backup files in different directories. (.bashrc~, .bash_profile~, .fluxbox/menu~, etc.). Thus, one of the smallest – but most useful – snippets that I use is the following alias:
alias emacs-clean='find . -name "*~" -exec rm {} \; -or -name ".*~" -exec rm {} \;'
This will find all files in $PWD and below that have a tilde (~) appended to them and send them to The Great Bit-Bucket Beyond.
I have the following to add:
An alias is something you add to your ~/.bashrc file. Once you add that line to your .bashrc file, typing “emacs-clean” on the command line will execute the action.
The tilde files are automatic backup files of the last version created by the text editors. You can turn off this automatic creation by editing the preferences of the text editor you use.
If you don’t want an alias, and just want to run this once (or every once in a while), execute the following command:
$find . -name "*~" -exec rm {} \; -or -name ".*~" -exec rm {} \;
Careful. Emacs backup files have saved my skin on more than one occasion. If you don’t want (or think you need) them, don’t generate them in the first place.
is this blog using ajax
Rather than deleting your backups every once and a while, why not just change your editors behavior. I include the following in my .emacs file:
;; create a backup file directory
(defun make-backup-file-name (file)
(concat “~/.emacs.backups/” (file-name-nondirectory file) “~”))
So when I edit a file, the backup is created in the ~/.emacs.backups directory rather than the current directory.
I agree with Kyle. Here’s the vim equivalent:
set backupdir=~/backups ” Don’t store backups in the current directory
Scary running a wildcarded rm, but it worked perfectly. Many thanks.
(Also, thanks to the commenters. Putting it in a backup directory’s a great idea.)
so how can I disable tilde file creation in Vim?
:set nobackup
[…] found this trick at Ubuntu Blog where a guy called Kyle added a comment about an auto-cleanup script. It’s about editing your […]
Thanks Marc, your line works like a charm.
Another option that’s nice on Windows is to move the tilde to the beginning of the file name. That way you won’t accidentally pick the wrong file, and your file types (and associtions in Explorer) aren’t hosed. Plus, they should stick together when a directory is sorted alphabetically.
So instead of the infuriating “file.txt~”, I’d have “~file.txt”, which still goes to emacs when double-clicked.
That way my line looks like
(defun make-backup-file-name (file)
(concat “~” (file-name-nondirectory file) ))
I just tried this for the first time a few minutes ago, so take care.
With gedit, you can just go to Edit -> Preferences and unselect “Create a backup copy of files before saving”.
so how can I disable tilde file creation in Vim?
So when I edit a file, the backup is created in the ~/.emacs.backups directory rather than the current directory.
Does anyone know how I can assign a backup directory for jed? Or disable ~ file generation completely? I tried to mess around a bit with my .jedrc but couldnt get it to work.
It’s awesome to go to see this web site and reading the views of all friends
on the topic of this article, while I am also keen of getting know-how.