Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Emacs shortcut to switch from a horizontal split to a vertical split in one move?

Tags:

emacs

I often find myself switching from a horizontal view of two windows in emacs to a vertical view. This requires me to first do C-x 1 and then C-x 3 and then C-x o followed by C-x b <RET> to switch to the other buffer or something like that. I would like to only have to type C-x | (analogous to how, in Ediff, you hit | to toggle the split view).

I found this in the emacs wiki site: http://www.emacswiki.org/emacs/ToggleWindowSplit

But how do I map that to the key combo I want? Or whether there is a simpler way to do it (taking less .emacs space).

like image 580
Palace Chan Avatar asked Feb 14 '13 17:02

Palace Chan


People also ask

How do I split a window vertically in Emacs?

You can split a window horizontally or vertically by clicking C-Mouse-2 in the mode line or the scroll bar.

What is split vertical?

A vertical splitting suggests a separation of sectors that lie side by side. There is a psychoanalytic tradition that suggests that the first splitting (horizontal) represents repression, and the second (vertical) can be considered as a representation of denial.

What is horizontal split?

Splits content that is displayed horizontally in a panel into two or more sections.


1 Answers

Making it easier for other people that also happened to be looking for the script (in this link), already modified with the other answer's keybinding:

(defun toggle-window-split ()   (interactive)   (if (= (count-windows) 2)       (let* ((this-win-buffer (window-buffer))          (next-win-buffer (window-buffer (next-window)))          (this-win-edges (window-edges (selected-window)))          (next-win-edges (window-edges (next-window)))          (this-win-2nd (not (and (<= (car this-win-edges)                      (car next-win-edges))                      (<= (cadr this-win-edges)                      (cadr next-win-edges)))))          (splitter           (if (= (car this-win-edges)              (car (window-edges (next-window))))           'split-window-horizontally         'split-window-vertically)))     (delete-other-windows)     (let ((first-win (selected-window)))       (funcall splitter)       (if this-win-2nd (other-window 1))       (set-window-buffer (selected-window) this-win-buffer)       (set-window-buffer (next-window) next-win-buffer)       (select-window first-win)       (if this-win-2nd (other-window 1))))))  (global-set-key (kbd "C-x |") 'toggle-window-split) 
like image 55
marcelocra Avatar answered Sep 27 '22 22:09

marcelocra