Emacs-эквивалент следующей команды vi - PullRequest
0 голосов
/ 06 марта 2011

Я ищу эквивалент следующей команды vi
:!nl%
это запускает команду nl для открытого в данный момент файла
Что такое emacs-способ определения имени открытого файла?
MX shell-commnad nl

Я не могу найти определение текущего значения открытия / буфера и замены.Thx / Махеш

Ответы [ 3 ]

2 голосов
/ 06 марта 2011

РЕДАКТИРОВАТЬ: неверно истолковать ваш вопрос как желание применить это изменение к файлу, над которым вы работаете.Если вы просто хотите запустить команду оболочки для буфера, вы можете использовать shell-command-on-region, который обычно привязан к M-|.

Если вы просто пытаетесь получить определенный номер строки, M-x goto-line работает.Я связываю это с C-x C-l, помещая (define-key global-map "\C-x\C-l" 'goto-line) в мой ~/.emacs.


Попробуйте это (в вашем ~/.emacs файле):

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

Вызовите еговыбрав регион, над которым вы хотите работать, а затем выполните M-#.

0 голосов
/ 06 марта 2011

Я использую это:

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

Тогда вы можете сделать М-! нл%

0 голосов
/ 06 марта 2011

Если вы всегда хотите, чтобы имя файла буфера вставлялось для команды оболочки, вы можете воспользоваться этим советом:

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

Как только вы добавите приведенный выше код, M-x shell-команда всегда будет начинаться с имени файла буфера, поэтому вы можете использовать его в команде.

...