How do I make Caps Lock work like Esc in Mac OS X?
Edit: As described in this answer, newer versions of MacOS now have native support for rebinding Caps Lock to Escape. Thus it is no longer necessary to install third-party software to achieve this.
Here's my attempt at a comprehensive, visual walk-through answer (with links) of how to achieve this using Seil (formerly known as PCKeyboardHack).
First, go into the System Preferences, choose Keyboard, then the Keyboard Tab (first tab), and click Modifier Keys:
In the popup dialog set Caps Lock Key to No Action:
2) Now, click here to download Seil and install it:
3) After the installation you will have a new Application installed ( Mountain Lion and newer ) and if you are on an older OS you may have to check for a new System Preferences pane:
4) Check the box that says "Change Caps Lock" and enter "53" as the code for the escape key:
And you're done! If it doesn't work immediately, you may need to restart your machine.
Impressed? Want More Control?
You may also want to check out KeyRemap4MacBook which is actually the flagship keyboard remapping tool from pqrs.org - it's also free.
If you like these tools you can make a donation. I have no affiliation with them but I've been using these tools for a long time and have to say the guys over there have been doing an excellent job maintaining these, adding features and fixing bugs.
Here's a screenshot to show a few of the (hundreds of) pre-selectable options:
PQRS also has a great utility called NoEjectDelay that you can use in combination with KeyRemap4MacBook for reprogramming the Eject key. After a little tweaking I have mine set to toggle the AirPort Wifi.
These utilities offer unlimited flexibility when remapping the Mac keyboard. Have fun!
Since macOS 10.12.1 it is possible to remap Caps Lock to Esc natively (Apple > System Settings… > Keyboard > Keyboard Shortcuts > Modifier Keys in macOS 13, or, before, System Preferences -> Keyboard -> Modifier Keys).
I wasn't happy with any of the answers here, and went looking for a command-line solution.
In macOS Sierra 10.12, Apple introduced a new way for users to remap keys.
No need to fiddle around with system GUIs
No special privileges are required
Completely customisable
No need to install any 3rd-party crap like PCKeyboardHack / Seil / Karabiner / KeyRemap4MacBook / DoubleCommand / NoEjectDelay
If that sounds good to you, take a look at hidutil.
For example, to remap caps-lock to escape, refer to the key table and find that caps-lock has usage code 0x39 and escape has usage code 0x29. Put these codes or'd with the hex value 0x700000000 in the source and dest like this:
hidutil property --set '{"UserKeyMapping":[{"HIDKeyboardModifierMappingSrc":0x700000039,"HIDKeyboardModifierMappingDst":0x700000029}]}'
You may add other mappings in the same command. Personally, I like to remap caps-lock to backspace, and remap backspace to delete:
hidutil property --set '{"UserKeyMapping":[{"HIDKeyboardModifierMappingSrc":0x700000039,"HIDKeyboardModifierMappingDst":0x70000002A}, {"HIDKeyboardModifierMappingSrc":0x70000002A,"HIDKeyboardModifierMappingDst":0x70000004C}]}'
To see the current mapping:
hidutil property --get "UserKeyMapping"
Your changes will be lost at system reboot. If you want them to persist, configure them in a launch agent. Here's mine:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<!-- Place in ~/Library/LaunchAgents/ -->
<!-- launchctl load com.ldaws.CapslockBackspace.plist -->
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.ldaws.CapslockEsc</string>
<key>ProgramArguments</key>
<array>
<string>/usr/bin/hidutil</string>
<string>property</string>
<string>--set</string>
<string>{"UserKeyMapping":[{"HIDKeyboardModifierMappingSrc":0x700000039,"HIDKeyboardModifierMappingDst":0x70000002A},{"HIDKeyboardModifierMappingSrc":0x70000002A,"HIDKeyboardModifierMappingDst":0x70000004C}]}</string>
</array>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>
I've placed this content into a file located at ~/Library/LaunchAgents/com.ldaws.CapslockBackspace.plist and then executed:
launchctl load com.ldaws.CapslockBackspace.plist
The only thing I know how to do is to map Caps Lock to Control, or Option, or Command. This can be done via the Keyboard & Mouse pane of System Preferences. Click on "Modifier Keys" on the bottom left and you'll be able to remap Caps Lock, Control, Option, and Command, to any of those.
#Craig:
This suggests that Caps Lock can be used as a normal -- that is, non-toggle -- key. On my MacBook, since I have re-mapped Caps Lock to Control, the Caps Lock light never lights up. It simply acts like the Control key.
It is now much easier to map the Caps Lock key to Esc with macOS Sierra.
Open System Preferences → Keyboard.
Click the Modifier Keys button in the bottom right-hand corner.
Click the drop down box next to the hardware key that you’d like to remap, and select Escape.
Click OK and close System Preferences.
https://9to5mac.com/2016/10/25/remap-escape-key-action-macbook-pro-macos-sierra-10-12-1-modifier-keys/
It's possible.
Solution 1
From an arcticle on TrueAffection.net.
Download PCKeyboardHack and install it.
Go to PCKeyboardHack in System Preferences.
Enable ‘Change Caps Lock’ and set the keycode to 53.
Solution 2
This solution doesn't involve patching the keyboard driver, but gives you a Vim specific solution.
OS X supports mapping the Caps Lock key to a whole bunch of keys, but you have to do it 'by hand', editting .plist files. The process is described in this article. As addendum to that hint I suggest you first set Caps-Lock to None in the System Preferences, then you only need to change one value in the .plist file. Also, you can of course use the Property List Editor instead of going through the XML conversion steps.
The trick is to map the Caps Lock key to the Help key (code 6), which isn't on most keyboards. But if it is, it will be treated as the insert key, which you probably don't use anyway, since you ask about remapping your Caps Lock to prevent stretching your hands ;)
You can then map the Help and the Insert key to Esc in vim.
map <Help> <Esc>
map! <Help> <Esc>
map <Insert> <Esc>
map! <Insert> <Esc>
This will work for gvim (Vim.app). I didn't get it to work with vim in the Terminal and I haven't tested it with MacVim.
So, it's rather a complicated, half-baked solution or installing a third-party piece of hackery. Your pick ;)
Edit: Just noticed solution 3, if you're using MacVim you can use Ctrl, Option and Command as Esc. With the System Preferences it's trivial to map Caps Lock to one of those keys.
Seil isn't yet available on macOS Sierra (10.12 beta). As such, I've been using Keyboard Maestro with these settings:
Credit to this github comment: https://github.com/tekezo/Seil/issues/68#issuecomment-230131664
Having tried several of these solutions, I have some notes:
DoubleCommand will not allow you to swap esc and caps-lock.
PCKeyboardHack will allow you to map capslock to escape, but does not have the capability to map escape to capslock. Recent versions will allow you to perform a complete swap by editing both keys.
This may or may not be sufficient for your needs (I know it is for mine).
In case you don't want to install a third-party app and you really only care about vim inside iTerm, the following works:
Remap CapsLock to Help as described here.
Short version: use plutil or similar to edit ~/Library/Preferences/ByHost/.GlobalPreferences*.plist, it should look similar to this:
<key>HIDKeyboardModifierMappingDst</key>
<integer>6</integer>
<key>HIDKeyboardModifierMappingSrc</key>
<integer>0</integer>
Restart! A simple log-out and log-in did not work for me.
In iTerm, add a new key mapping for Help: send hex code 0x1b, which corresponds to Escape.
I know this is not exactly what was asked for, but I assume the intent of many people looking for a solution like this is actually this more specialized variant.
In order to actually swap the escape key with the caps lock key (not just map one to the other) using both PCKeyboardHack and KeyRemap4MacBook, you have to follow the instructions in this thread, mapping the caps lock key to a keycode not used by the keyboard but accounted for by KeyRemap4MacBook (eg. 110). Then, in PCKeyboardHack, select the appropriate option that maps that keycode to escape (in the case of 110, it's "Application Key to Escape"). Here's what your KeyRemap4MacBook preferences should look like (provided you've selected the "show enabled only" checkbox).
I originally attempted to post this information as an edit to cwd's excellent answer, but it was rejected. I encourage anyone who wants to go the route that I describe to first read his/her response.
Open up Keyboard preferences and click modifier keys... you can change the caps lock key to control, option, escape, or command.
Seil doesn't work on macOS Sierra yet, so I'm using Karabiner Elements, download from https://pqrs.org/latest/karabiner-elements-latest.dmg.
Either use the GUI or put the following into ~/.karabiner.d/configuration/karabiner.json:
{
"profiles" : [
{
"name" : "Default profile",
"selected" : true,
"simple_modifications" : {
"caps_lock" : "escape"
}
}
]
}
You can also use DoubleCommand to remap this, and other keys.
IIRC, it will map Caps Lock to Esc.
Karabiner-Elements
A powerful and stable keyboard customizer for macOS. (freeware)
https://pqrs.org/osx/karabiner/index.html
Worked for me for Mojave to change caps-lock to backspace
With the latest Ventura update, System Settings changed.
You will find it on
System Settings > Keyboard > Keyboard Shortcuts > Modifier Keys
Related
It is my first time to use emacs -nw. But I have problem at the first when I type M-x. In the minibuffer, it shows "Mark set" instead of M-x
I don't know why it happens. How can I deal with it?
PS: My os is Macosx and I connect remote by using ssh and then I type emacs -nw but I cannot use M-x
Update:08/30/2015
When I see the options under M-x customize-group ns, it seems that Alt is actually used as Meta? Why it didn’t works.
14Hide Ns Alternate Modifier: [Value Menu] meta |
15 [ State ]: STANDARD. |
16 This variable describes the behavior of the alternate or option key. More |
37Hide Ns Right Alternate Modifier: [Value Menu] Use the value of ns-alternate-modifier |
38 [ State ]: STANDARD. |
39 This variable describes the behavior of the right alternate or option key. More |
When using Emacs in the terminal (switch -nw), you might not have a Meta key, depending on your platform etc. In particular, the Alt keyboard key might not function as the Meta key. You might find some information (e.g. here, or by googling) about how to get it working as Meta for your context (platform etc.), but you might not.
You can always use ESC (the Escape key) followed by a key to act the same as the Meta modifier applied to that key. For example, instead of M-x, you can use ESC x.
(You press and release ESC, and then do the same for x. ESC is not a modifier key -- you do not hold it pressed while hitting x.)
Your problem is that OSX is stealing the key. There are a few things which will make your life easier
Install emacs using homebrew. The version which comes with OSX is old
Use iterm2 instead of the default term.app - much better and feature rich
In iterm2, you can set a key override for either the left or right option as +ESC, which will make it all work.
This will all work on a local machine, but you may run into additional problems if your connecting to a remote machine and running emacs there. It is a similar problem, your key settings may no be getting stolen/dropped by the ssh session - all depends on your terminal, your ssh config and the remote system.
Note that with emacs, you often don't need to run remote. Emacs comes with a built-in system called Tramp, which is very powerful. Using tramp, you can open remote directories and edit the files as if they were local files. You run emacs locally and tramp uses ssh to open the remote files, read the contents into the local emacs buffer and then write the changes back tot he remote file. Helps to have ssh keys setup so you don't have to worry about passwords (but you will need an ssh-agent to make this work easily).
In general, when you find emads does not respond to a key press, especailly if you try C-h k and nothing happens when you hit the key bindings you can't get to work, it is almost certain that something in-between you and emacs is stealing the keys - this is often the window manager or OS, but it can also be due to terminal emulators etc. In over 20 years of using emacs, I've never had emacs 'do nothing' when you hit a key combination - if emacs recieves the key input, it will respond with something, even if that something is to tell you there is no key binding for the keys you pressed. If emacs does absolutely nothing, you know the message just isn't get through!
Open Terminal Preferences
Select Profiles
Keyboard
Check the box for "Use Option as Meta Key"
GNU Emacs on Mac OS X, by default, uses the control key as CTRL, and the command key as META. This drives me crazy, because my MacBook Pro contains only a single control key on the left, while I'm used to having a control key on both sides of the keyboard.
With my GNU Emacs installation, I was able to modify the variables mac-control-modifier, mac-command-modifier, and mac-option-modifier so that command acts as CTRL and option acts as META.
How can I accomplish the same thing in IntelliJ IDEA? It's easy to select "Emacs" as the keymap, but I don't see any way make a similar remapping of CTRL and META. I know I could remap these keys globally in the OS, but I don't want to do that. I also know I could go in to each and every action and individually modify them, but that seems like a poor solution.
It's may be not the easiest solution but you can open the resources.jar (/Applications/IntelliJIdea.app/lib/resources.jar on MacOS) file, locate the Keymap_Emacs.xml (in idea directory) copy and modify it.
And then you can create a new keymap in IntelliJ for updating it with the new content (~/Library/Preferences/IntelliJIdea90/keymaps on MacOS).
That way you can change all your keymap without changing each action individually.
I saw the vim wiki tips and it says that in order to remap Esc to CAPS LOCK you have to edit the following windows code:
REGEDIT4
[HKEY_CURRENT_USER\Keyboard Layout]
"Scancode Map"=hex:00,00,00,00,00,00,00,00,02,00,00,00,01,00,3a,00,00,00,00,00
Is it possible to remap Esc to CAPS LOCK by only adding or modifying lines in the _vimrc?
I recommend that you use AutoHotkey for this.
You can do a per-application hotkey change:
SetTitleMatchMode,2
#IfWinActive,VIM
CAPSLOCK::ESC
return
#IfWinActive
CAPSLOCK::CTRL
return
This script, for example sets caps to escape in vim, and control everywhere else.
There's no vim key identifier for the capslock key, so you have to do it with a scan code.
It would be like trying to map the ctrl key or the shift key to something - those keys don't do anything by themselves, they are key modifiers, so they can't be trapped the same way.
The mapping has to be done at the OS level instead of vim, so the complication comes from the OS. For example, on my Ubuntu machine I have the following in ~/.Xmodmap:
! Esc on caps lock
remove Lock = Caps_Lock
keysym Caps_Lock = Escape
However, I don't think the REGEDIT script is so bad. It's kind of ugly, but it's only two lines!
Also you can try this: SharpKeys.
I swaped CapLock and Esc with it.
You could also use Ctrl-C to escape (Available at least in vim). Which is what I normally use. I commonly swap control and capslock, though, and have done so through registry settings in windows and keyboard conf in ubuntu.
Edit: as #mMontu kindly notes, Ctrl-C is more brutal than Esc: As stated in this appropriate question,
vim isn't going to bother checking if you just wrote part of an
abbreviation, and it isn't going to run the fancy auto commands your
plugins have set up for every time you leave insert mode
I hadn't noticed because my vimming is not so advanced yet.
Another alternative that worked well for me was to use the instructions found in
this website
Essentially: Install Microsoft's Keyboard Layout Creator, then edit its source code to allow remapping of CAPS LOCK and create your own custom layout.
Remap CAPSLOCK to ESC and CTRL on Windows
A very nice tool that I've already described in this answer gives you the possibility to remap CAPSLOCK to both
to ESC (when pressed alone) and
to CTRL (when pressed with other keys)
Just use this little piece of open source software by ililim.
You do not need Admin privileges for this and ESC and CTRL are still working as expected.
I use it to enjoy convenient CTRL + anything presses without hurting my pinky and to toggle modes in Vim that I use via ssh in MobaXterm. For installation just follow this description.
I am using the Emacs keybinding on Komodo IDE where Komodo uses the command key for "Meta" by default. So you press Command-X when you normally press Alt+X for Emacs on Linux.
I want to use the actual Alt key .. so when I tried to assign Alt+K, Komodo informs that "This key combination is not available" (see screenshot below)
Komodo Preferences Dialog screenshot http://dl.getdropbox.com/u/87045/permalinks/komodo-alt-key.png
How do I use the Alt key in Komodo? Is it possible?
On the Mac the Option/Alt key is used to quickly enter non-ASCII characters.
Komodo provides a few bindings involving control keys, like Alt+Return to
edit a file's properties, but it stays away from the Alt/Option key for
the most part.
I tried binding [Go to file...] to Cmd+Alt+K. The preferences dialog
accepted it (although it said it was first bound to Cmd+¬, and later said
it was bound to Cmd+Alt+Û, both of which gave me the feeling that
the binding would fail to work. There have been Mozilla bugs dealing
with inconsistencies in keybindings on OS X, and we're seeing that here.
Komodo will also let you create keybindings using combinations of
the Command, Control, and Shift keys -- I would stick with those,
and leave the Alt key for entering non-ASCII characters.
One can workaround this behavior using ControllerMate or a similar tool:
Create a new Driver Configuration for your normal keyboard
In that configuration, swap the Option and Command keys
Disable this new configuration
Create a new ControllerMate page
Add an Application Block to that page which only triggers when Komodo IDE is running in the foreground
Add a Properties Block connected to the Application Block which enables your Driver
Configuration.
Thus, when Komodo IDE is running in the foreground, ControllerMate swaps
Command and Option, giving you the Meta behavior you get everywhere else in OS X.
In bash shell with emacs key-binding, you can use key combination like M-f, M-b to move one word forward or backward on the shell prompt respectively. Usually, the meta key is mapped to Alt key on Windows and Linux. However, in iTerm, I could not find a way to map this meta key to either Option or Command key on my MacBook Pro.
It seems that in OS X, the meta key is by default mapped to ESC key. So you can use ESC-f, ESC-b on iTerm. However, ESC key is apparently not practical to use. In addition, iTerm does have option that allow you to modifier mapping for the meta key (Bookmarks > Profiles > Keyboard Profiles > Global > Option Key as...), this setting does not seem to work at all.
Therefore, if anyone know what is the solution to this problem, please let me know.
I have upgraded to the latest release, 0.9.6.1012, and this behavior is still persist.
Edit: Some clarification to my question. The key-binding I'm talking about is for bash shell, not in emacs. It just happens that, by default, bash shell also use the same key-binding as emacs.
Open "Preferences" (⌘,) -> Profiles -> Keys -> General tab.
Set Left Option key: to act as Esc+.
Works for me in Emacs, though I'm not sure if it will have any other undesirable effects.
Go to Bookmarks > Manage Profiles. Then select Keyboard Profiles > Global and choose Option Key as Meta. This works for me in version 0.9.6.1201.
In Menu bar Iterm2 => Preferences => Profiles choose the keys tab and select
left Option key act as +Esc
worked for me as well in irssi.
For iTerm2 users this answer just partially solves the problem.
Cmd+., Profiles, Keys and choose Left option Key acts as: +Esc.
In addition to that, you have to go to Preferences (Cmd+,) ➔ Profiles ➔ Keys and in the Key Mappings find these Key Combination's: and . Double click on each mapping and change their Action and Esc+ to the following:
Go to your iTerm preferences select "profiles" then "key" and change your presets in "Natural Text Editing"
It should work immediately after.
It works with iTerm version 3.3.12
Going all zombie resurrection here, but for sake of posterity --
This post got it working as you desire for me, for M-b and M-f, while keeping 'Option Key as Meta' set so you don't sacrifice a modifier (I have vim mappings using meta, personally).
In summary, in your Global Keyboard Profile, add two new mappings with settings:
Key as hex code, value of 62 for b, 66 for f (man ascii)
Modifier as Option
Action as send escape sequence, value b or f
The only thing I still miss from Terminal.app is Opt-Backspace to delete by word. There are other workarounds for that, but I'm just trying to train myself to use Ctrl-w.
It's easier in iTerm2. Go to preferences->bookmark->keyboard and select Option sends +ESC. In iTerm2 you can configure the left and right option keys separately, so remember to change both if that's what you want. Link for iTerm2: http://sites.google.com/site/iterm2home/
Go to Bookmarks > Manage Profiles. Then select Keyboard Profiles > Global and choose Option Key as +ESC.
Works in iTerm Build 0.9.5.0611 & Build 0.9.6.20090415
I found some solution on stackoverflow also, just go to keyboard profile and in "Global" change "Option key as" +ESC
that works for me perfectly =) btw some iterm patch exists also look here: http://www.emacswiki.org/emacs/EmacsForMacOS
I don't know about iTerm, but you can do this in Terminal. Instructions can be found here:
Emacs on Mac OS X Leopard key bindings
Here is what worked for me: Bookmarks > Manage Profiles > Keyboard Profiles > Global/xterm > Option Key as +Esc. With this, I can use Option+Enter in Midnight Commander and the currently selected file/directory name shows up in the command string, as it should. Option Key as Meta did not work for me. My build of iTerm is 0.10.
I was struggling with Meta + Left or Meta + Right. The context was for keystroke in Emacs utilized in an iTerm2 session.
I tried Esc+ and Meta for the left Option key with no luck. I tried with and with out the hex mappings. No luck.
But then I tried "Report modifiers using CSI u". (Documentation here). That allowed me to use the meta + Left and meta + right keys in Emacs, which is the behavior I have on my Linux machine.
The common answers of setting the options key to ESC+, didn't work for me out of the box. At some point I realized that it doesn't work only when opening the floating iterm terminal (using F12). The trick to fix it was setting the options key to ESC+ also for the "Hotkey Window" profile.
This tiny note in the "Apps can change this" mouseover made all the difference for me:
After trying all the options above for what seemed like hours, it was as simple as setting the "Right Option key" to act as Esc+ (though I was pressing Left).
Try "export LANG=C". I'm not emacs expert, but I found this enables emacs on OS X to recognize the Option key as the Meta key in iTerm,