tags:

views:

366

answers:

3

Hello,

I am using emacs I find that sometimes I have 2 files separated into 2 windows.

For example: I open 1 file using C-x C-f file1.c RET

and I split the fame into two windows: C-x 3

I then open another file C-x C-f file2.c RET

So I have 2 files:

window 1 (left) file1.c

window 2 (right) file2.c

I am wondering if there is any key combination to swap the files over? Normally I like to work on the left window when I have 2 window. I know I can easily do C-x oto move the cursor to the right window.

However, I am just wondering if I can swap the files so that file2.c is in the left window and file1.c is in the right window?

Many thanks for any suggestions,

+1  A: 

I would contrive to open up file #2 in the desired location, i.e. after you hit c-x 3, move the cursor with c-x o before navigating to the second file.

khedron
+2  A: 

I'm not aware of any built-in function doing this.

However, it does not seem too difficult to whip up some elisp for doing it. Devil is in the details though.

(defun swap-buffers-in-windows ()
  "Put the buffer from the selected window in next window, and vice versa"
  (interactive)
  (let* ((this (selected-window))
     (other (next-window))
     (this-buffer (window-buffer this))
     (other-buffer (window-buffer other)))
    (set-window-buffer other this-buffer)
    (set-window-buffer this other-buffer)
    )
  )

Notably, this may not be doing what you desire with respect to where the caret ends up. However, you'd first have to say what you want :p

Bahbar
I copied and pasted this code, and it doesn't seem to do anything.
Kinopiko
Oh, sorry, it does do something, it swaps the top and bottom parts of the Emacs window. I was expecting it to swap the frames.
Kinopiko
ok. You're confusing me. you mentioned C-x 3. This is to create 2 emacs windows, not 2 emacs frames. Are you using frames or windows ? What do you call windows and what do you call frames?
Bahbar
also, you did not talk about top and bottom parts. Do you have more than 2 buffers showing at once ?
Bahbar
I'm not the person who asked the question, I'm just an interloper. I had never used C-x 3 before I saw this question, but as you say it splits the Emacs window, rather than creating a new frame.
Kinopiko
doh. Sorry. I should have noticed
Bahbar
Well, your answer seems to be correct! For some reason I am chronically incapable of understanding Lisp despite my efforts.
Kinopiko
Sorry, I guess I was wrong. I guess I should have been using Windows instead of frames.
robUK
+4  A: 

I use buffer-move for this. Now if you are working on the buffer on the left side, calling 'buf-move-right' will swap it with the one on the right. I guess this is what you want.

Raja
This solution is perfect, and the comments are very clear - just make sure you read them :)
Martin Clarke