d11wtq
d11wtq

Reputation: 35318

Emacs move around split windows in a specified direction?

In Terminal Emacs (no mouse), I'm using split windows to work with multiple buffers at the same time. I'm finding moving between the split windows much more painful than how I do it in Vim. Reading the documentation it looks like I'm doing it correctly (C-x o), but that just cycles around the windows in a clockwise direction. If I move to an adjacent window to make a quick edit, I need to hit C-x o a few times before I'm back where I was. Sometimes I accidentally press it too many times in a hurry and have to cycle all the way back through the windows again.

Far from install yet-another-external-package, is there any way I can just either move directly to a window (by, say, a number), or at least cycle around the windows in the opposite direction?

In Vim C-w C-w is like C-x o in Emacs, but there's also C-w ARROW to move in a specified direction... something like that? :)

Upvotes: 24

Views: 17585

Answers (8)

Jorge Moraleda
Jorge Moraleda

Reputation: 381

You can also use give a numeric argument to C-x o to jump ahead (or backwards for negative numbers) multiple windows.

For example, to select the previous window, press Alt type -1, then release Alt and type C-x o.

References:

Upvotes: 0

phils
phils

Reputation: 73345

Add this to your init file:

(windmove-default-keybindings)

Then you can use SHIFT+arrow to move to the next adjacent window in the specified direction.

You can specify a different modifier if you prefer, by passing an argument (defaults to 'shift).

Or just bind whatever you like to these functions:

  • windmove-up
  • windmove-down
  • windmove-left
  • windmove-right

You can also add FrameMove to the mix, to make this work transparently across multiple frames.

For numbered window navigation, there's switch-window.el.

Upvotes: 33

jameshfisher
jameshfisher

Reputation: 36579

Add this to your init file (e.g. ~/.emacs):

(windmove-default-keybindings)

Then do SHIFT+arrow to move to the window in that direction.

Upvotes: 5

csantosb
csantosb

Reputation: 306

For the sake of completion, there is window-numbering and ace-window too

Upvotes: 1

mcandre
mcandre

Reputation: 24662

Use window-jump, e.g.:

;; C-x <direction> to switch windows
(use-package window-jump
             :bind (("C-x <up>" . window-jump-up)
                    ("C-x <down>" . window-jump-down)
                    ("C-x <left>" . window-jump-left)
                    ("C-x <right>" . window-jump-right)))

For help with use-package, see https://github.com/jwiegley/use-package/blob/master/README.md.

Upvotes: 1

Burton Samograd
Burton Samograd

Reputation: 3638

I wrote an elisp module a while back to expand on windmove to make it a bit more useful: http://samograd.ca/stumpwm.el. You can bind stumpwm-move-window-[left/right/up/down] to whatever keys you want and the windows will move in the correct direction, even into another another frame (tested with 2 frames). There's also an stumpwm-interactive-resize-window for handy interactive window resizing using C-n/C-p/C-f/C-b with Return to end resizing.

Upvotes: -1

snowape
snowape

Reputation: 1404

You can give a prefix argument to C-x o like this C-u -1 C-x o. This way you can go any number of windows forward or backward. Personally I think it's easier to create a special key for moving back one window. I have this in my .emacs:

(defun other-window-backward ()
  "Goto previous window"
  (interactive)
  (other-window -1))
(global-set-key (kbd "\C-x p") 'other-window-backward)

Upvotes: 3

Lindydancer
Lindydancer

Reputation: 26144

I use the following to navigate to the next (same as C-x o), previous, first, and last window:

(defun my-previous-window ()
  "Previous window"
  (interactive)
  (other-window -1))
(global-set-key "\C-xp" 'my-previous-window)

(global-set-key "\C-xn" 'other-window)

(defun my-select-first-window ()
  (interactive)
  (select-window (frame-first-window)))

(defun my-select-last-window ()
  (interactive)
  (select-window (previous-window (frame-first-window))))

(global-set-key "\C-x<" 'my-select-first-window)
(global-set-key "\C-x>" 'my-select-last-window)

Upvotes: 1

Related Questions