help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: emacs equivalent of vi %


From: Xah
Subject: Re: emacs equivalent of vi %
Date: Wed, 13 Aug 2008 08:14:25 -0700 (PDT)
User-agent: G2/1.0

In dired, you can type “!” to run a command on a file.
On Aug 12, 11:52 pm, "Rustom Mody" <address@hidden> wrote:
> In vi one can do, for example,
> :!gcc %
> and the current file gets compiled -- ingeneral :
> :!someCommand options % otherStuff
> and the filename gets interpolated.
> Is there any way to get the '%' behavior in emacs?
> [Note: I know about compile; I am asking a more general question about
> getting the filename]

as far as i know there's no similar mechanism. Here's related things i
know:

This code will run the current file:

(defun run-current-file ()
  "Execute or compile the current file.
For example, if the current buffer is the file x.pl,
then it'll call “perl x.pl” in a shell.
The file can be php, perl, python, bash, java.
File suffix is used to determine what program to run."
(interactive)
  (let (ext-map file-name file-ext prog-name cmd-str)
; get the file name
; get the program name
; run it
    (setq ext-map
          '(
            ("php" . "php")
            ("pl" . "perl")
            ("py" . "python")
            ("sh" . "bash")
            ("java" . "javac")
            )
          )
    (setq file-name (buffer-file-name))
    (setq file-ext (file-name-extension file-name))
    (setq prog-name (cdr (assoc file-ext ext-map)))
    (setq cmd-str (concat prog-name " " file-name))
    (shell-command cmd-str)))

For detail, see:
http://xahlee.org/emacs/elisp_run_current_file.html

You can easily modify it so that it takes a string, replace % with
filename, and run it as a shell command, like vi's %.

What you need to do is query users by a prompt, then take the string
and do replacement on it. Then call shell command on it as above.
Here's a tutorial on these:
 http://xahlee.org/emacs/elisp_idioms.html


"Rustom Mody" wrote:
> Roughly I know what to do:
>
> Bind the character '%' to
> (file-name-nondirectory (buffer-file-name))
> in the keymap that is in effect when shell-command ( M-! )  is running.
>
> Im only not sure which keymap (and/or how to find it)

if you implement it as i suggested above, then you don't need to
override any keymaps.

For what's worth, the minibuffer's map is minibuffer-local-map, and
the local keymap used by shell is comint-mode-map. For some detail,
see:
 http://xahlee.org/emacs/ergonomic_emacs_keybinding_minibuffer.html

  Xah
∑ http://xahlee.org/

reply via email to

[Prev in Thread] Current Thread [Next in Thread]