|
| 1 | +--- |
| 2 | +title: "Bind Ctrl + hjkl to arrow keys on a Mac Machine" |
| 3 | +description: "This is how you can bind Ctrl + hjkl as arrow keys on a mac machine" |
| 4 | +date: 2024-07-31T13:55:20-05:00 |
| 5 | +draft: false |
| 6 | +tags: ["Vim", "Neovim", "Lua", "Linux"] |
| 7 | +categories: ["Development"] |
| 8 | +# weight: 4 |
| 9 | +# lastmod: 2020-01-01T16:45:40+08:00 |
| 10 | +# author: "Dillon" |
| 11 | +# authorLink: "https://shivas.blog" |
| 12 | +# images: [] |
| 13 | +# resources: |
| 14 | +# - name: "featured-image" |
| 15 | +# src: "featured-image.png" |
| 16 | +# lightgallery: true |
| 17 | +--- |
| 18 | + |
| 19 | +### What this post covers? |
| 20 | +So instead of reaching to arrow keys, you can just use h (as left arrow), j (as down arrow) k (as up arrow) and l (as right arrow) by hoding ctrl key. |
| 21 | + |
| 22 | +### Before we start |
| 23 | +First things first, we need to install a free tool called Hammerspoon. This tool will allow us to configure the key bindings. |
| 24 | + |
| 25 | +## Keyboard Settings on Mac |
| 26 | +Navigate to System Settings > Keyboard > Keyboard Shortcut > Modifier Keys (left pane). Select your keyboard, and for the Caps Lock key, choose Control from the drop-down menu. |
| 27 | + |
| 28 | +This will make the Caps Lock key function as the Control key (you can always change it back to Caps Lock if needed). Since I rarely use the Caps Lock function, I repurposed this key. You can select any other key from the drop-down that suits you better. |
| 29 | + |
| 30 | +## Hammerspoon Config |
| 31 | +If don't exist already, creat a new file at the following location ~/.hammerspoon/init.lua and enter the following code |
| 32 | +``` |
| 33 | +mkdir -p ~/.hammerspoon |
| 34 | +touch ~/.hammerspoon/init.lua |
| 35 | +``` |
| 36 | + |
| 37 | +```lua |
| 38 | +-- Boolean to track if mappings are enabled: |
| 39 | +local mappingsEnabled = false |
| 40 | + |
| 41 | +-- Function to bind `hjkl` to arrow keys with Control as a modifier |
| 42 | +function mapControlToArrows() |
| 43 | + local controlModifier = {"ctrl"} |
| 44 | + local controlShiftModifier = {"ctrl", "shift"} |
| 45 | + local repeatDelay = 0.1 |
| 46 | + local repeatInterval = 0.05 |
| 47 | + |
| 48 | + -- Bind Control + h to left arrow |
| 49 | + hs.hotkey.bind(controlModifier, "h", function() |
| 50 | + hs.eventtap.keyStroke({}, "left") |
| 51 | + end, nil, function() |
| 52 | + hs.eventtap.keyStroke({}, "left") |
| 53 | + end) |
| 54 | + |
| 55 | + -- Bind Control + j to down arrow |
| 56 | + hs.hotkey.bind(controlModifier, "j", function() |
| 57 | + hs.eventtap.keyStroke({}, "down") |
| 58 | + end, nil, function() |
| 59 | + hs.eventtap.keyStroke({}, "down") |
| 60 | + end) |
| 61 | + |
| 62 | + -- Bind Control + k to up arrow |
| 63 | + hs.hotkey.bind(controlModifier, "k", function() |
| 64 | + hs.eventtap.keyStroke({}, "up") |
| 65 | + end, nil, function() |
| 66 | + hs.eventtap.keyStroke({}, "up") |
| 67 | + end) |
| 68 | + |
| 69 | + -- Bind Control + l to right arrow |
| 70 | + hs.hotkey.bind(controlModifier, "l", function() |
| 71 | + hs.eventtap.keyStroke({}, "right") |
| 72 | + end, nil, function() |
| 73 | + hs.eventtap.keyStroke({}, "right") |
| 74 | + end) |
| 75 | + |
| 76 | + -- Bind Control + Shift + h to Shift + left arrow |
| 77 | + hs.hotkey.bind(controlShiftModifier, "h", function() |
| 78 | + hs.eventtap.keyStroke({"shift"}, "left") |
| 79 | + end, nil, function() |
| 80 | + hs.eventtap.keyStroke({"shift"}, "left") |
| 81 | + end) |
| 82 | + |
| 83 | + -- Bind Control + Shift + j to Shift + down arrow |
| 84 | + hs.hotkey.bind(controlShiftModifier, "j", function() |
| 85 | + hs.eventtap.keyStroke({"shift"}, "down") |
| 86 | + end, nil, function() |
| 87 | + hs.eventtap.keyStroke({"shift"}, "down") |
| 88 | + end) |
| 89 | + |
| 90 | + -- Bind Control + Shift + k to Shift + up arrow |
| 91 | + hs.hotkey.bind(controlShiftModifier, "k", function() |
| 92 | + hs.eventtap.keyStroke({"shift"}, "up") |
| 93 | + end, nil, function() |
| 94 | + hs.eventtap.keyStroke({"shift"}, "up") |
| 95 | + end) |
| 96 | + |
| 97 | + -- Bind Control + Shift + l to Shift + right arrow |
| 98 | + hs.hotkey.bind(controlShiftModifier, "l", function() |
| 99 | + hs.eventtap.keyStroke({"shift"}, "right") |
| 100 | + end, nil, function() |
| 101 | + hs.eventtap.keyStroke({"shift"}, "right") |
| 102 | + end) |
| 103 | +end |
| 104 | + |
| 105 | +-- Function to unbind `hjkl` from arrow keys |
| 106 | +function unmapControlFromArrows() |
| 107 | + hs.hotkey.disableAll() |
| 108 | +end |
| 109 | + |
| 110 | +-- Function to toggle mappings |
| 111 | +function toggleMappings() |
| 112 | + if mappingsEnabled then |
| 113 | + unmapControlFromArrows() |
| 114 | + hs.alert.show("Control + hjkl as arrows disabled") |
| 115 | + else |
| 116 | + mapControlToArrows() |
| 117 | + hs.alert.show("Control + hjkl as arrows enabled") |
| 118 | + end |
| 119 | + mappingsEnabled = not mappingsEnabled |
| 120 | +end |
| 121 | + |
| 122 | +-- Bind the toggle function to a hotkey (e.g., Ctrl+Shift+F1) |
| 123 | +hs.hotkey.bind({"ctrl", "shift"}, "F1", toggleMappings) |
| 124 | + |
| 125 | +-- Initial setup to enable mappings |
| 126 | +toggleMappings() |
| 127 | +``` |
| 128 | + |
| 129 | +Save the file and open the Hammerspoon application on your system to reload the configuration. Alternatively, you can reload the config by clicking the Hammerspoon icon in the taskbar and selecting "Reload Config." |
| 130 | + |
| 131 | +This setup allows you to use the hjkl keys as arrow keys when holding the Control key, helping you code faster and stay more productive. |
0 commit comments