Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Emacs: replace file extension

Tags:

regex

emacs

elisp

Given a file name I want to replace the file extension to ".org"

I decided to use replace-regexp-in-string function:

(replace-regexp-in-string "\\(.*\\)\..*$" "\\1.org" "file.conserv.module")

Output:

"file.conserv.modul.org"

I supposed, that it will return "file.conserv.org" like in Perl program:

my $str = 'file.conserv.module';
$str =~ s/(.*)\..*$/$1.org/;
print $str;

How can I get "file.conserv.org"?

like image 917
user4035 Avatar asked Jul 16 '26 11:07

user4035


1 Answers

This can do what you want without any regular expressions:

(concat (file-name-sans-extension "file.conserv.module") ".org")

If you want to remove the path too:

(concat (file-name-base "file.conserv.module") ".org")
like image 51
pmr Avatar answered Jul 18 '26 02:07

pmr



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!