0

I am looking for equivalent of following vi command
:! nl %
this runs nl command on currently open file
What is emacs way to detect name of open file ?
M-X shell-commnad nl

I am not able find determine value of current open/buffer and substitute. Thx/Mahesh

JPReddy
  • 63,233
  • 16
  • 64
  • 93
forvaidya
  • 3,041
  • 3
  • 26
  • 33

3 Answers3

2

EDIT: Misread your question as wanting to apply that change to the file you're working on. If you just want to run a shell command against a buffer, you can use shell-command-on-region, which is usually bound to M-|.

If you're just trying to get to a particular line number, M-x goto-line works. I bind that to C-x C-l by putting (define-key global-map "\C-x\C-l" 'goto-line) in my ~/.emacs.


Try this (in your ~/.emacs file):

;;; Run a shell command on all text between the mark and the point and
;;; replace with the output.

(defun shell-command-in-region (start end command &optional flag interactive)
  "Execute shell-command-on-region and replace the region with the output
of the shell command."
  (interactive (list (region-beginning) (region-end)
                     (read-from-minibuffer "Shell command in region: "
                                           nil nil nil 'shell-command-history)
                     current-prefix-arg
                     (prefix-numeric-value current-prefix-arg)))

  (shell-command-on-region (point) (mark) command t)
)

(define-key esc-map "#" 'shell-command-in-region)

Invoke it by selecting a region you want to operate on and then doing M-#.

Blrfl
  • 6,817
  • 1
  • 25
  • 25
  • Although your edit does work, the question is how to programatically provide the filename corresponding to the buffer as an argument to a shell command. – jamessan Mar 06 '11 at 14:28
  • That's here: http://stackoverflow.com/questions/455345. Be aware that there is no guarantee that the file on the disk matches what's in the buffer unless you make it a point to `save-buffer` beforehand. (Same as in `vi`.) – Blrfl Mar 06 '11 at 14:32
0

If you always want the buffer's file name to be inserted for the shell command, you can use this advice:

(defadvice read-shell-command (before read-shell-command-with-filename activate)
  "force the initial contents to contain the buffer's filename"
  (if (and (null (ad-get-arg 1))
       buffer-file-name)
  (ad-set-arg 1 buffer-file-name)))

Once you've added the above code, M-x shell-command will always start with the buffer's file name, so you can use it in the command.

Trey Jackson
  • 73,529
  • 11
  • 197
  • 229
0

I use this:

(defun my-shell-command-on-current-file (command &optional output-buffer error-buffer)
  "Run a shell command on the current file (or marked dired files).
In the shell command, the file(s) will be substituted wherever a '%' is."
  (interactive (list (read-from-minibuffer "Shell command: "
                                           nil nil nil 'shell-command-history)
                     current-prefix-arg
                     shell-command-default-error-buffer))
  (cond ((buffer-file-name)
         (setq command (replace-regexp-in-string "%" (buffer-file-name) command nil t)))
        ((and (equal major-mode 'dired-mode) (save-excursion (dired-move-to-filename)))
         (setq command (replace-regexp-in-string "%" (mapconcat 'identity (dired-get-marked-files) " ") command nil t))))
  (shell-command command output-buffer error-buffer))

(global-set-key (kbd "M-!") 'my-shell-command-on-current-file)

Then you can do M-! nl %

scottfrazer
  • 17,079
  • 4
  • 51
  • 49