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

时间:2015-01-04 21:32:39

标签: emacs

如果我打开两个缓冲区(并排)并从一个窗口移动到另一个窗口,是否可以将第一个(现在不活动)窗口中先前选择的单词替换为活动窗口中光标下的单词?

_是光标

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

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

1 个答案:

答案 0 :(得分:2)

没有内部命令,但这是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)))