crazyfox
crazyfox

Reputation: 165

How can I remap my OSX keyboard without using third-party programs?

I currently am using Seil to remap caps-lock to escape on OSX 10.11. Whenever I check htop just to see what is running when the system isn't doing much of anything (programs open but not doing much processing), Seil is always up there and fluctuating in its cpu and memory use. It probably isn't a big deal, but it feels crazy to have to always run this thing considering I only use vim for occasional flat text files. I don't want to have to start Seil every time I use vim. Is my best option to just disable caps lock and physically attach a bridge up to the escape key? Vim comes standard on OSX and is unusable without remapping escape to something, so I would think this would come up instead of relying on someone else to make an app for it.

Upvotes: 2

Views: 1915

Answers (1)

zarak
zarak

Reputation: 3003

Remap Caps Lock to Control

You can remap the caps lock key to the control key in the keyboard settings (click on the Modifier Keys button).

enter image description here

In vim, an alias for esc is control-[, which is much more accessible than reaching for the esc key.

Remap Caps Lock to Escape in Vim

Navigate to the Library/Preferences/ByHost directory under the home folder

cd ~/Library/Preferences/ByHost

Identify the hidden property list file

ls -a | grep Global

Assign it to a shell variable for easy referencing later

pfile=$(ls -a | grep Global)

Make sure the newly created variable pfile has the right value. It should look something like this: .GlobalPreferences.7D7C488E-0E83-5562-B58B-011C540890F3.plist

echo $pfile

Make a backup just in case something goes horribly wrong

cp $pfile{,.bak}

Copy the pfile to the home directory and convert it to xml

cp $pfile ~/keys-binary.plist
plutil -convert xml1 -o ~/keys-xml.plist ~/keys-binary.plist

Open the file and search for the text HIDKeyboardModifierMappingSrc and HIDKeyboardModifierMappingDst. Set the value in between the integer tags under HIDKeyboardModifierMappingDst to 5 and under HIDKeyboardModifierMappingSrc to 0, as follows.

 <key>com.apple.keyboard.modifiermapping.1452-588-0</key>
 <array>
     <dict>
         <key>HIDKeyboardModifierMappingDst</key>
         <integer>5</integer>
         <key>HIDKeyboardModifierMappingSrc</key>
         <integer>0</integer>
     </dict>
 </array>

Save, then convert back to a binary file

plutil -convert binary1 -o ~/keys-binary.plist ~/keys-xml.plist

Finally, copy this file back to the ByHost directory, replacing the original file.

You'll need to log out and back in before the changes take effect. Once you have logged back in, add the following line to your .vimrc:

noremap  <Esc>Op <Esc>
noremap! <Esc>Op <Esc>

This is going to work if you open vim in the terminal. If you are using gvim, try out Solution 2 by eelco which I referenced to write this post with a couple of different mappings to make it work in the terminal.

Note also that, for this to work, the Allow VT100 application keypad mode option in your Terminal Preferences must be checked.

Upvotes: 2

Related Questions