Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine base name of Emacs buffer file name?

Tags:

emacs

Assume I edit in Emacs a file t.txt in my home directory. Then ESC-: buffer-file-name produces /Home/fcihh/t.txt How can I extract the file name part t.txt from that?

According to this link I thought I should be able to write ESC-: (file-name-base buffer-file-name). But this produces the error

Lisp error: (void-function file-name-base)

I am using GNU Emacs 23.3.1 on Ubuntu 12.04.

like image 777
Håkon Hægland Avatar asked Oct 26 '25 10:10

Håkon Hægland


1 Answers

Unfortunately file-name-base has been introduced in Emacs 24. Here is its definition, from lisp/files.el:

(defun file-name-base (&optional filename)
  "Return the base name of the FILENAME: no directory, no extension.
FILENAME defaults to `buffer-file-name'."
  (file-name-sans-extension
   (file-name-nondirectory (or filename (buffer-file-name)))))

Both file-name-sans-extension and file-name-nondirectory should be available in Emacs 23.

If you want the base name with extension you only need file-name-nondirectory.

like image 93
giordano Avatar answered Oct 28 '25 23:10

giordano



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!