Skip to content

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

License

Notifications You must be signed in to change notification settings

jamescherti/bufferfile.el

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

Build Status MELPA MELPA Stable License

Introduction

This package provides helper functions to delete and rename buffer files:

  • bufferfile-rename: Renames the file visited by the current buffer 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, 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: Copies the file visited by the current buffer to a new file.

The functions above also ensures that any modified buffers are saved prior to executing operations like renaming, deleting, or copying.

(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.)

Table of Contents

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
  :ensure t)

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.)

Customizations

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)

Usage

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-2025 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: An Emacs package that provides a minor mode that enables code folding and outlining based on indentation levels for various indentation-based text files, such as YAML, Python, and other indented text files.
  • vim-tab-bar.el: Make the Emacs tab-bar Look Like Vim’s Tab Bar.
  • easysession.el: Easysession is lightweight Emacs session manager that can persist and restore file editing buffers, indirect buffers/clones, Dired buffers, the tab-bar, and the Emacs frames (with or without the Emacs frames size, width, and height).
  • elispcomp: A command line tool that allows compiling Elisp code directly from the terminal or from a shell script. It facilitates the generation of optimized .elc (byte-compiled) and .eln (native-compiled) files.
  • tomorrow-night-deepblue-theme.el: The Tomorrow Night Deepblue Emacs theme is a beautiful deep blue variant of the Tomorrow Night theme, which is renowned for its elegant color palette that is pleasing to the eyes. It features a deep blue background color that creates a calming atmosphere. The theme is also a great choice for those who miss the blue themes that were trendy a few years ago.
  • Ultyas: A command-line tool designed to simplify the process of converting code snippets from UltiSnips to YASnippet format.
  • dir-config.el: Automatically find and evaluate .dir-config.el Elisp files to configure directory-specific settings.
  • flymake-bashate.el: A package that provides a Flymake backend for the bashate Bash script style checker.
  • flymake-ansible-lint.el: An Emacs package that offers a Flymake backend for ansible-lint.
  • inhibit-mouse.el: A package that disables mouse input in Emacs, offering a simpler and faster alternative to the disable-mouse package.
  • quick-sdcv.el: This package enables Emacs to function as an offline dictionary by using the sdcv command-line tool directly within Emacs.
  • enhanced-evil-paredit.el: An Emacs package that prevents parenthesis imbalance when using evil-mode with paredit. It intercepts evil-mode commands such as delete, change, and paste, blocking their execution if they would break the parenthetical structure.