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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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) |
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:
Publicar un comentario