1

Windows 10, Emacs 25.1

I have 4 open buffers.

Here

enter image description here

To switch from buffer "file_1.txt" to buffer "file_2.txt" I use standard command windmove-right. It is bound to <M-right>. enter image description here

To switch from buffer "file_2.txt" to buffer "file_3.txt" I use standard command windmove-down. It is bound to <M-down>.

And so on. It's very comfortable. Nice.

But what if I need to SWAP buffers "file_1.txt" and "file_2.txt"?

The result must be like this:

enter image description here

To do this I use package ace-window and command ace-swap-window.

Here steps. 1. Start command ace-swap-window

enter image description here

  1. Select number of windows I want to swap - "3" enter image description here

  2. And as result the buffer "file_1.txt" and "file_2.txt" was swapped. enter image description here

But I think it's very slow.

Is it possible when I stay on buffer "file_1.txt" and press, e.g. <M-S-right> to swap buffer "file_1.txt" and "file_2.txt".

Or when I stay on buffer "file_2.txt" and press, e.g. <M-S-down> to swap buffer "file_2.txt" and "file_4.txt".

I think it will be very fast and convenient.

  • I know is not the answer you're looking for. But wouldn't be faster just to key bind Ace swap to M-p? , that way you would just have to do M-p 3. That's what I actually do. This way you don't have to move your hand to the cursor – Fabman Aug 20 '18 at 16:47
  • I think about this...but it slow. I need set like this github.com/lukhas/buffer-move – a_subscriber Aug 20 '18 at 19:17
  • No problem!. You could always create your own package to do what your looking for. I don't know if any existing package does exactly what you are looking for. – Fabman Aug 20 '18 at 19:20
  • A package already exists for this – InHarmsWay Aug 23 '18 at 11:54
  • @InHarmsWay This package is closed and not work. I get error Debugger entered--Lisp error: (void-variable closed) eval-buffer(#<buffer *load*> nil "d:/Programs/emacs/.emacs.d/elpa/buffer-move-20160615.1103/buffer-move.el" nil t) ; Reading at buffer position 7 – a_subscriber Aug 23 '18 at 12:06
1

This is quick and dirty solution. It uses windmove and ace-window -- as I am lazy to come up with my own window functions:

(require 'windmove)
(require 'ace-window)

(defun swap-windows (dir)
  (let ((other-window (windmove-find-other-window dir nil nil)))
    (cond ((null other-window)
           (user-error "No window %s from selected window" dir))
          ((and (window-minibuffer-p other-window)
                (not (minibuffer-window-active-p other-window)))
           (user-error "Minibuffer is inactive"))
          (t
           (aw-swap-window other-window)))))


(defun swap-windows-right ()
  (interactive)
  (swap-windows 'right))

(defun swap-windows-left ()
  (interactive)
  (swap-windows 'left))

(defun swap-windows-down ()
  (interactive)
  (swap-windows 'down))

(defun swap-windows-up ()
  (interactive)
  (swap-windows 'up))

Bind those functions (swap-windows-right/left/down/up) to whatever keys you'd like.

1

Here's another hand-rolled solution. It requires only ace-window, the rest of the functions are built-in

(defun swap-vertical ()
  (interactive)
  (if (window-in-direction 'above)
      (aw-swap-window (window-in-direction 'above))
    (aw-swap-window (window-in-direction 'below))))

(defun swap-horizontal ()
  (interactive)
  (if (window-in-direction 'right)
      (aw-swap-window (window-in-direction 'right))
    (aw-swap-window (window-in-direction 'left))))

I'm expecting that you will have at most two rows and two columnns of windows in a pane. Calling swap-horizontal will swap left or right, and swap-vertical will swap up or down. I find that for these kinds of actions I like to have the same key do the forward and reverse direction, so I can bounce back and forth. You could also make separate functions for each direction if you prefer. In that case, they would be even simpler:

(defun swap-right ()
  (interactive)
  (aw-swap-window (window-in-direction 'right)))

and so on for each direction.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.