iLemming
iLemming

Reputation: 36166

Replacing word in inactive buffer

If I have two buffers open (side-by-side) and I move from one window to another, can I replace previously selected word in the first (now inactive) window with the one that is under cursor in active window?

_ is cursor

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

is there an internal command that can quickly let me replace foo with bar?

Upvotes: 1

Views: 42

Answers (1)

abo-abo
abo-abo

Reputation: 20342

No internal commands, but this is 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)))

update: advanced version

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

Upvotes: 2

Related Questions