Question: 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

Answer: 
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-#`.
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 %`