domingo, 16 de mayo de 2021

Current file name on emacs's shell-command

When I came from vim to emacs, I wanted to get my ":!perl %" command in emacs. I found that by default, there was no symbol bound to current-file-name in the shell-command minibuffer.

One of my first emacs hacks was this snippet (git blame says I added it in 2013)


(defun shell-execute ()
(interactive)
(let ((file-buffer (buffer-file-name))
(command (read-shell-command "Shell command: " nil nil
(let ((filename
(cond
(buffer-file-name)
((eq major-mode 'dired-mode)
(dired-get-filename nil t)))))
(and filename (file-relative-name filename))))))
(shell-command (replace-regexp-in-string "%" file-buffer command))))
(global-set-key (kbd "M-!") 'shell-execute)
view raw .emacs.el hosted with ❤ by GitHub

The other day, in some reddit thread, I found shell-command+, which does this and a bit more. Super cool that it also uses '%', inheriting the vi tradition. And I can retire now my old code.


Speaking of '%', did you know that typing '%' in your firefox address bar will narrow the completions to currently open tabs?  A-MA-ZING. More cool shortcuts here.

No hay comentarios: