SkillAgentSearch skills...

Bufferfile.el

Emacs 'bufferfile': Rename, Delete, or Copy Files and Associated Buffers (e.g., clones/indirect buffers)

Install / Use

/learn @jamescherti/Bufferfile.el
About this skill

Quality Score

0/100

Supported Platforms

Universal

README

bufferfile.el - Rename, Delete, or Copy Files and Associated Buffers (e.g., clones/indirect buffers)

Build Status MELPA MELPA Stable License

This bufferfile.el package provides helper functions to delete, rename, or copy buffer files:

  • bufferfile-rename: Renames the file visited by the current buffer, ensures that the destination directory exists, and updates the buffer name for all associated buffers, including clones/indirect buffers. It also ensures that buffer-local features referencing the file, such as Eglot, Dired buffers, or the recentf list, are correctly updated to reflect the new file name.
  • bufferfile-delete: Delete the file associated with a buffer and kill all buffers visiting the file, including clones/indirect buffers.
  • bufferfile-copy: Ensures that the destination directory exists and copies the file visited by the current buffer to a new file.

If this enhances your workflow, please show your support by ⭐ starring bufferfile.el on GitHub to help more Emacs users discover its benefits.

The bufferfile package overcomes limitations in Emacs' built-in functions:

  • Emacs built-in renaming: While indirect buffers continue to reference the correct file path, their buffer names can become outdated.
  • Emacs built-in deleting: Indirect buffers are not automatically removed when the base buffer or another indirect buffer is deleted.

The bufferfile package resolves these issues by updating buffer names when a file is renamed and removing all related buffers, including indirect ones, when a file is deleted.

(To make bufferfile use version control when renaming or deleting files, you can set the variable bufferfile-use-vc to t. This ensures that file operations within bufferfile interact with the version control system, preserving history and tracking changes properly.)

<!-- markdown-toc start - Don't edit this section. Run M-x markdown-toc-refresh-toc -->

Table of Contents

<!-- markdown-toc end -->

Installation from MELPA

To install bufferfile from MELPA:

  1. If you haven't already done so, add MELPA repository to your Emacs configuration.

  2. Add the following code to your Emacs init file to install bufferfile from MELPA:

(use-package bufferfile
  :commands (bufferfile-copy
             bufferfile-rename
             bufferfile-delete)
  :custom
  ;; If non-nil, display messages during file renaming operations
  (bufferfile-verbose nil)

  ;; If non-nil, enable using version control (VC) when available
  (bufferfile-use-vc nil)

  ;; Specifies the action taken after deleting a file and killing its buffer.
  (bufferfile-delete-switch-to 'parent-directory))

Usage

  • To rename the current buffer's file and associated buffers, run: M-x bufferfile-rename (You will be prompted to enter a new name. The file will be renamed on disk, and the buffer—along with any associated buffers such as indirect buffers—will begin visiting the new file, with their buffer names updated accordingly.)

  • To delete the current buffer's file and associated buffers, run: M-x bufferfile-delete (You will be asked to confirm the deletion. If confirmed, the file will be removed from disk, and all associated buffers, including indirect buffers, will be killed.)

  • To copy the current buffer's file, run: M-x bufferfile-copy

Customizations

How to make Dired use bufferfile to rename files

By default, Dired's rename operation (R) updates the file on disk but may not correctly update any buffers visiting the file, especially for renamed files with indirect (clone) buffers.

To address this, you can override Dired's rename keybinding (R) to use bufferfile-dired-do-rename, which uses bufferfile rename functions. This ensures that all associated buffers, including indirect ones, are properly updated after the rename operation:

;; Override Dired's rename behavior to use bufferfile rename functions,
;; ensuring buffers visiting the renamed file are updated accordingly.
(with-eval-after-load 'dired
  (define-key dired-mode-map (kbd "R") #'bufferfile-dired-do-rename))

Making bufferfile use version control (VC), such as Git, when renaming or deleting files?

To make bufferfile use version control (VC) when renaming or deleting files, you can set the variable bufferfile-use-vc to t. This ensures that file operations within bufferfile interact with the version control system, preserving history and tracking changes properly.

(setq bufferfile-use-vc t)

Hook functions

The bufferfile package provides customizable hook variables that allow users to execute functions before and after renaming or deleting files. These hooks can be used to integrate additional logic, such as logging, or updating dependent buffers.

Hooks for Renaming Files

  • bufferfile-pre-rename-functions A list of functions executed before renaming a file. Each function receives three arguments:

    • previous-path: The original file path.
    • new-path: The new file path.
    • list-buffers: The list of buffers associated with the file.
  • bufferfile-post-rename-functions A list of functions executed after a file has been renamed. Each function receives the same three arguments as bufferfile-pre-rename-functions.

Hooks for Deleting Files

  • bufferfile-pre-delete-functions A list of functions executed before a file is deleted. Each function receives two arguments:

    • path: The file path to be deleted.
    • list-buffers: The list of buffers associated with the file.
  • bufferfile-post-delete-functions A list of functions executed after a file has been deleted. Each function receives the same two arguments as bufferfile-pre-delete-functions.

Frequently asked questions

What is the difference between bufferfile and the built-in Emacs rename and delete functions?

Here are the limitations of Emacs' built-in functions:

  • Renaming: Indirect buffers point to the correct file path, but their buffer names become outdated.
  • Deleting: Indirect buffers are not removed when the base buffer or another indirect buffer is deleted.

The bufferfile package addresses these issues by ensuring that buffer names are updated when renaming a file and that all buffers, including indirect buffers, are deleted when a file is removed.

These limitations of the built-in functions motivated the development of bufferfile, which improves support for renaming and deleting buffers, including indirect buffers.

Author and License

The bufferfile Emacs package has been written by James Cherti and is distributed under terms of the GNU General Public License version 3, or, at your choice, any later version.

Copyright (C) 2024-2026 James Cherti

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program.

Testimonials

  • tinkerorb: I can't even remember for how long I have wanted something that solves this (but I also have not lifted a single finger of my own to solve it for myself). Thank you for this package!

Links

Other Emacs packages by the same author:

  • minimal-emacs.d: This repository hosts a minimal Emacs configuration designed to serve as a foundation for your vanilla Emacs setup and provide a solid base for an enhanced Emacs experience.
  • compile-angel.el: Speed up Emacs! This package guarantees that all .el files are both byte-compiled and native-compiled, which significantly speeds up Emacs.
  • [outline-indent.el](https://github.com/
View on GitHub
GitHub Stars20
CategoryDevelopment
Updated2d ago
Forks1

Languages

Emacs Lisp

Security Score

95/100

Audited on Apr 2, 2026

No findings