Domanda

In vi [m] c'è il comando ! che mi permette di reindirizzare il testo tramite un comando shell - come ordinamento o rientro - e riportare il testo filtrato nel buffer. Esiste un equivalente in emacs?

È stato utile?

Soluzione

È possibile selezionare una regione e digitare `C-u M- | comando RET ', e sostituisce la regione con l'output del comando nello stesso buffer a causa dell'argomento del prefisso interattivo di shell-command-on-region.

Altri suggerimenti

L'ho scritto qualche anno fa, potrebbe aiutarti:

(defun generalized-shell-command (command arg)
  "Unifies `shell-command' and `shell-command-on-region'. If no region is
selected, run a shell command just like M-x shell-command (M-!).  If
no region is selected and an argument is a passed, run a shell command
and place its output after the mark as in C-u M-x `shell-command' (C-u
M-!).  If a region is selected pass the text of that region to the
shell and replace the text in that region with the output of the shell
command as in C-u M-x `shell-command-on-region' (C-u M-|). If a region
is selected AND an argument is passed (via C-u) send output to another
buffer instead of replacing the text in region."
  (interactive (list (read-from-minibuffer "Shell command: " nil nil nil 'shell-command-history)
                     current-prefix-arg))
  (let ((p (if mark-active (region-beginning) 0))
        (m (if mark-active (region-end) 0)))
    (if (= p m)
        ;; No active region
        (if (eq arg nil)
            (shell-command command)
          (shell-command command t))
      ;; Active region
      (if (eq arg nil)
          (shell-command-on-region p m command t t)
        (shell-command-on-region p m command)))))

Ho trovato questa funzione molto utile. Se lo trovi utile, ti suggerisco di associarlo a qualche tasto funzione per praticità, personalmente uso F3 :

(global-set-key [f3] 'generalized-shell-command)

Modifica tardiva : per quanto apprezzi i voti positivi, la risposta di Jurta è la strada da percorrere. E l'hack di Greg è più ordinato del mio.

Lascerò il resto qui perché potrebbe valere la pena, ma ...


Mx shell-command-on-region , che sembra essere associato a M-| da di default.


Vedo che questo non fa esattamente ciò che Rohit ha chiesto. L'uso di Ch f shell-command-on-region rivela che il comportamento desiderato è disponibile nella versione non interattiva del comando (impostando l'argomento sostituisci su non-zero ). Dovremmo essere in grado di scrivere un wrapper per farlo.

Prova (caricalo in * scratch * ed esegui M-x eval-buffer , se funziona, copialo nel tuo file .emacs):

(defun shell-command-on-region-replace (start end command)
  "Run shell-command-on-region interactivly replacing the region in place"
  (interactive (let (string) 
         (unless (mark)
           (error "The mark is not set now, so there is no region"))
         ;; Do this before calling region-beginning
         ;; and region-end, in case subprocess output
         ;; relocates them while we are in the minibuffer.
         ;; call-interactively recognizes region-beginning and
         ;; region-end specially, leaving them in the history.
         (setq string (read-from-minibuffer "Shell command on region: "
                            nil nil nil
                            'shell-command-history))
         (list (region-beginning) (region-end)
               string)))
  (shell-command-on-region start end command t t)
  )

E nota come ho detto nei commenti che questa non è una cosa molto emacsy da fare. Ma penso che funzioni.


Per tutti i lettori che non sanno come selezionare una regione:

  1. Sposta il " punto " (posizione corrente del cursore) a un'estremità della regione e usa C-space per attivare il segno " "
  2. Sposta il punto sull'altra estremità della regione
  3. Fatto, invoca il comando
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top