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-#`.
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.