替换非活动缓冲区中的单词

Replacing word in inactive buffer

如果我打开了两个缓冲区(并排)并且我从一个 window 移动到另一个,我可以用活动 window?

中光标下的那个

_ 是光标

  _______________
 | foo   | _bar  |          
 |       |       |
 |       |       |
 |       |       |
 |_______|_______| 

是否有内部命令可以让我快速将 foo 替换为 bar

没有内部命令,但这是 Emacs:

(defun replace-word-other-window ()
  (interactive)
  (let ((sym (thing-at-point 'symbol))
        bnd)
    (other-window 1)
    (if (setq bnd (bounds-of-thing-at-point 'symbol))
        (progn
          (delete-region (car bnd) (cdr bnd))
          (insert sym))
      (message "no symbol at point in other window"))
    (other-window -1)))

更新:进阶版

(defun region-or-symbol-bounds ()
  (if (region-active-p)
      (cons (region-beginning)
            (region-end))
    (bounds-of-thing-at-point 'symbol)))

(defun replace-word-other-window ()
  (interactive)
  (let* ((bnd-1 (region-or-symbol-bounds))
         (str-1 (buffer-substring-no-properties
                 (car bnd-1)
                 (cdr bnd-1)))
         (bnd-2 (progn
                  (other-window 1)
                  (region-or-symbol-bounds))))
    (if bnd-2
        (progn
          (delete-region (car bnd-2) (cdr bnd-2))
          (insert str-1))
      (message "no region or symbol at point in other window"))
    (other-window -1)))