DEV Community

Rajasegar Chandran
Rajasegar Chandran

Posted on

Vim-style repeatable key bindings for navigating windows in Emacs

Table of Contents

  1. Vim-style repeatable key bindings for navigating windows in Emacs
    1. Navigating windows in Emacs
    2. windmove
    3. repeat-mode
    4. Key bindings
    5. References

Vim-style repeatable key bindings for navigating windows in Emacs

In this post we are going to see how to create easily accessible
key bindings for jumping around windows inside Emacs in a directional manner. We are also going to make this key bindings repeatable to make the navigation awesome, along with Vim-style navigation using h,j,k,l keys

Navigating windows in Emacs

Before I came up with this workflow, I have been using C-x o to switch to windows in Emacs. The biggest problem with this for me is to complete the window cycle to switch to the desired window which is the immediate left window from my current one. I stumbled across the windmove commands and start using them. Finally I came up with an intuitive key binding in Vim style to use the home row keys like h,j,k,l

windmove

Windmove is a library built into GnuEmacs starting with version 21.
It lets you move point from window to window using Shift and the arrow keys. This is easier to type than β€˜C-x o’ and, for some users, may be more intuitive. To activate all these keybindings, add the following to your InitFile:


    (when (fboundp 'windmove-default-keybindings)
      (windmove-default-keybindings))
Enter fullscreen mode Exit fullscreen mode

You also might need to enable windmove-wrap-around setting if you wish to enable wrapping around window navigation.

    (setq windmove-wrap-around t)
Enter fullscreen mode Exit fullscreen mode

This setting will control whether movement off the edge of the frame wraps around. If this variable is set to t, moving left from the leftmost window in a frame will find the rightmost one, and similarly for the other directions.

repeat-mode

repeat-mode is a global minor mode.
When Repeat mode is enabled, certain commands bound to multi-key
sequences can be repeated by typing a single key, after typing the
full key sequence once. The commands which can be repeated like that are those whose symbol has the property β€˜repeat-map’ which specifies a keymap of single keys for repeating.

You can check out the current repeat maps enabled in your Emacs with M-x describe-repeat-maps

Please ensure you have enabled repeat-mode in your Emacs config by adding:

    (repeat-mode 1)
Enter fullscreen mode Exit fullscreen mode

Key bindings

Now let's take a look at mapping the windmove commands to simple
keybindings starting with the C-c w prefix:

    (global-set-key (kbd "C-c w h") 'windmove-left)
    (global-set-key (kbd "C-c w j") 'windmove-down)
    (global-set-key (kbd "C-c w k") 'windmove-up)
    (global-set-key (kbd "C-c w l") 'windmove-right)
Enter fullscreen mode Exit fullscreen mode

In order to make the key bindings repeatable we have to define a new repeat-map using the defvar-keymap function with the repeat property set to true.

    (defvar-keymap windmove-repeat-map
    :repeat t
    "h" #'windmove-left
    "j" #'windmove-down
    "k" #'windmove-up
    "l" #'windmove-right)
Enter fullscreen mode Exit fullscreen mode

Hope you enjoyed the post and the new key bindings are helpful for you to navigate your windows in Emacs. Please let me know your thoughts and feedback in the comments section

References

Top comments (0)