简体   繁体   English

如何使基于文件名的emacs自动命名缓冲区?

[英]How to make emacs auto-name buffer based on filename?

I often load many 00readme.txt files into emacs, and the default buffer name "00readme.txt" isn't very helpful. 我经常将许多00readme.txt文件加载到emacs中,默认的缓冲区名称“ 00readme.txt”不是很有帮助。

I use rename-buffer manually to rename them "project1", "project2", etc. 我手动使用重命名缓冲区将它们重命名为“ project1”,“ project2”等。

How do I tell emacs: "when loading /foo/bar/00readme.txt, automatically name the buffer project1, not 00readme.txt"? 我如何告诉emacs:“在加载/foo/bar/00readme.txt时,自动将缓冲区命名为project1,而不是00readme.txt”?

您可能还需要查看emacs附带的uniquify库,当打开具有相同名称的文件时,该库可以将部分目录名称添加到缓冲区名称中。

;; toss this into your .emacs file and fiddle with it till you get what you want
(defun my-buffer-renamer() 
(interactive) 
(let ()  ; <--  local vars in here

  (message "bufer name is %s" (current-buffer))
  (rename-buffer "something else") ; make sure to make unique names

))

(add-hook 'text-mode-hook 'my-buffer-renamer) ; only do this once

Emacs supports a plethora of 'hook' functions, callbacks that get executed when a particular action occurs. Emacs支持大量的“ hook”函数,这些钩子是在发生特定操作时执行的回调。 Here, we add a function to gets invoked when a text file get's loaded. 在这里,我们为加载文本文件时添加了要调用的函数。

Here's a nice configuration for uniquify , which is the standard Emacs way to solve your problem: 这是uniquify的一个不错的配置,这是解决问题的标准Emacs方法:

(require 'uniquify)
(setq uniquify-buffer-name-style 'reverse)
      uniquify-separator " • "
      uniquify-after-kill-buffer-p t
      uniquify-ignore-buffers-re "^\\*"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM