Vim-style repeatable key bindings for navigating windows in Emacs
Tags: productivity, emacs, vim
Published: 2024-06-06 05:51:00


# Table of Contents

1.  [Vim-style repeatable key bindings for navigating windows in Emacs](#orge85934e)
    1.  [Navigating windows in Emacs](#org9ab4f54)
    2.  [windmove](#orge3556e7)
    3.  [repeat-mode](#org0e39f4f)
    4.  [Key bindings](#org0158bd7)
    5.  [References](#orgcf3d17f)


<a id="orge85934e"></a>

# 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 


<a id="org9ab4f54"></a>

## 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


<a id="orge3556e7"></a>

## 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:

```elisp
    
    (when (fboundp 'windmove-default-keybindings)
      (windmove-default-keybindings))
```

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

```elisp
    (setq windmove-wrap-around t)
```

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.


<a id="org0e39f4f"></a>

## 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:

```elisp
    (repeat-mode 1)
```

<a id="org0158bd7"></a>

## Key bindings

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

```elisp    
    (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)
```

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.

```elisp
    (defvar-keymap windmove-repeat-map
    :repeat t
    "h" #'windmove-left
    "j" #'windmove-down
    "k" #'windmove-up
    "l" #'windmove-right)
```
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


<a id="orgcf3d17f"></a>

## References

-   <https://www.emacswiki.org/emacs/WindMove>
-   <https://karthinks.com/software/emacs-window-management-almanac/>
-   <https://karthinks.com/software/it-bears-repeating/>