Pergunta

No vi [m] há a ! comando que me deixa texto tubulação através de um comando shell - como espécie ou travessão - e obter a volta texto filtrado para o buffer. Existe um equivalente no emacs?

Foi útil?

Solução

Você pode selecionar uma região e digite `C-u M- | comando RET', e que substitui a região com a saída de comando no mesmo tampão devido ao argumento prefixo interactivo de concha-comando-em-região.

Outras dicas

Eu escrevi este há alguns anos atrás, ele pode ajudá-lo:

(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)))))

Eu encontrei esta função para ser muito útil. Se você achar que é útil, bem, eu sugiro que o vincule a algum tecla de função por conveniência, pessoalmente, eu uso F3:

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

editar Tarde : Tanto quanto eu aprecio os upvotes, a resposta de Jurta é o caminho a percorrer. E corte de Greg é mais puro do que o meu.

Vou deixar o resto deste aqui porque pode valer alguma coisa, mas ...


M-x shell-command-on-region , que parece ser ligado a M-| por padrão.


Eu vejo que isso não fazer exatamente o que Rohit pediu. Usando C-h f shell-command-on-region revela que o comportamento desejado está disponível na versão não-interativo do comando (definindo o replace argumento para não-nil). Nós deve ser capaz de escrever um wrapper para fazer isso.

Tente este (carregá-lo em *scratch* e M-x eval-buffer prazo, se funcionar, copie-o para seu arquivo .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 que eu digo nos comentários que isso não é uma coisa muito emacsy fazer. Mas eu acho que funciona.


Para todos os leitores que não sabem como selecionar uma região:

  1. Mova o "point" (posição atual do cursor) para uma extremidade da região, e da utilização C-space para ativar a "marca"
  2. Mova o ponto para o outro extremo da região
  3. O seu feito, chamar o comando
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top