There are several differences between Mac OS X's Terminal application and the xterm and xterm-like applications common to Unix systems running X Windows: You cannot customize the characteristics of the Terminal with command-line switches such as - fn, - fg, and - bg. Text-only applications on OS X. I feel there should be an article on wikipedia somewhere covering the non-GUI applications such as the Python shell, Emacs and Vim releases bundled with OS X: I added a brief section on them here but it was (probably justifiably) removed as irrelevant by another editor. GNU Emacs For Mac OS X Pure builds of Emacs for Mac OS X. Download Emacs Version 26.3 Universal Binary (63.494 MB) Released 2019-09-02 Usually there's a nifty page here with a big download button. But you are using a browser which doesn't support SVG.
Intro
If your Emacs does not recognize your Alt key as the Meta key anymore, then you probably have some other key bound to Meta. In some distributions, this is the Windows key, or a Penguin key on some keyboards. On Macintoshes meta is often the Option
key, which often is caught for other purposes.
The ESC
key can be used as a prefix, and often works without initial hassle. Well, if running EmacsOnTerminal, better see below.
On some ports, Emacs finds an existing Meta key and no longer treats Alt as the Meta key. You have the following options, then:
- Start using the new Meta key
- Make the new Meta key something else
- Change all Emacs keybindings from Meta to Alt (don't!)
- If using X11, another XModMap config… (Just in case the first didn't work… for me at least!)
X-window things
Using the XKB Extension
- SettingMetaWithXKB – Using XKB to set the meta key
XFree86 option
If you're using XFree86 4, you can get a similar effect by putting something like this in XF86Config-4:
Other keyboard type
Select a pc101 keyboard type instead of a pc104. X doesn't even know about the Windows/Menu keys anymore, and Emacs works you would expect it to. Obviously this trick only works if you don't use the Windows/Menu/Penguin/whatever keys.
Mac Os X Versions
Use Super for mod4
Put the following into your ~/.Xmodmap file (create if necessary).
This will make your Windows/Penguin key the Super key. Thus, Emacs notices that no key will be Meta and accepts Alt instead of Meta.
This is because Meta and Alt are different and you can bind things to M-a and to A-a and they should not be confused. But in the case where there is no Meta binding, it's preferable to treat Alt as Meta since Alt bindings are much more rare than Meta bindings in Emacs.
Just clear mod4
An alternative to adding the Super modifier key is to just clear mod4.
Check your xmodmap by running xmodmap or ‘xmodmap -pm'. Assume the following result:
Meta_L is in mod4… that's not good. Just clear it by adding ‘clear mod4' to .Xmodmap or running the command ‘xmodmap -e 'clear mod4''. And now everything is fine.
xmodmap background
There are several terms and concepts which can be confusing.
Note there are two types of mappings xmodmap can handle: keycode to keysym and keycode to modifier. The latter are somewhat unique to X, and are the most common source of confusion. In fact xmodmap makes it somewhat confusing because it's configuration directives for modifiers are based on keysyms, which it internally resolves into keycodes.
Also note that modifiers like Alt, Meta, Super, Hyper, NumLock, and ScrollLock don't have fixed modifier names – they are reported to the application as one of the five generic modifiers. It is up to the application to determine the meaning of the modifier.
The way applications are supposed to do this is to inspect the list of keysyms for the keycodes assigned to that modifier. For example upon receiving a key press event with mod1, if mod1's keycode list includes a keycode with a mapping to Alt_L, this would mean mod1 is the Alt modifier. Some applications instead follow static mapping conventions. For example mod1 might be treated as Meta regardless of the assigned keysym. This used to be true of Emacs, so you will see lots of advice about putting Alt, Meta, or Super on specific modifiers. This shouldn't matter to current versions of Emacs.
However, to complicate things further, Emacs does implement two special cases. First, if there is no modifier with a keycode with a mapping to Meta_L or Meta_R, Emacs will use the Alt modifier as the Meta modifier. And if Alt and Meta correspond to the same modifier, it will only use the Meta modifier.
Here's the output of xmodmap:
As you can see, the Shift keys act as a shift modifier, the Control keys act as a control modifier, and the left Alt key acts as mod1 modifier. And in addition to that, you can see that the 'Use Super for mod4' solution has been used in this setup. There is no mention of Meta for any modifier, so Emacs will use the Alt modifier (mod1) as a Meta modifier.
On some keyboards there is a real Meta key. In that case and in some other cases you may want Meta to be distinct from Alt. In that case you would need to take one of the modifiers (for example mod4), and assign Meta_L and/or Meta_R and remove any other keycodes.
Emacs only changes
If this problem exists only on X Windows, I seem to have some luck with setting x-alt-keysym to meta.
Or, if you want to use the windows key (or penguin key) as meta, you can do it by setting x-super-keysym to meta.
OS Specific things
MS Windows
On MS Windows you can use the Windows keys as Meta keys. This allows normal menu handling through the keyboard using the Alt key. However to do that fully you currently need a slightly patched version of CVS Emacs. You can get this is from EmacsW32 web site.
Via SSH - use PUTTY
If you are using windows and access a remote unix or GNU box using a term, one good term program, IMHO, is PuTTY. Very small and compact, yet does all the right things.. and customizable. Alt is automatically Meta. Page up/Down get passed right through to emacs, etc. The only problem I have had to date with putty is that the ‘pcprint' hasn't worked for me in putty (tried when using ssh).. anyone? – DeepakGoel
GNU/Linux
Use Alt for the Windows Manager
On some systems (eg. Debian) the Alt key generates 'Alt' and the Windows key generates 'Meta'. Instead of changing things around, adapt to it and use 'Alt' key combinations for the window manager (eg. Sawfish). If you use a lot of hotkeys in your window manager, this is recommended.
MacOS
If you're in a hurry, ESC maps to Meta, so you can type ESC x
instead of M-x
. This even works over SSH, on any keyboard layout.
Terminal
When running emacs from Terminal, you can set Terminal's preferences/settings/keyboard and check 'Use option as meta key', and the option/alt key should properly behave as the Meta key. You can also remap the Command key to be a control key under System Preferences/Keyboard/Keyboard/Modifier Keys to use emacs in Terminal. The downside to this approach is that it does not retain the Command key's functionality for use in other Mac applications,
If you use Iterm2, you should edit Iterm2's profile/Keys, and check Left Alt Key to Esc+.
X11
The Apple X11 server application has the meta at the Option button, which is caught by the application itself for various purposes, and never passes on to the X11 client. Disabling Enable keyequivalents under X11
under the input preferences is one easy way to fix this, but this will effectively kill a few useful keybindings of the X11 servers too.
For some reason under leopard (Mac OS-X 10.0 and higher), internationalization may be turned on. This will result in insertion of a non-ASCII character in the native EmacsForMacOS when you use the Meta key. For example, you may see a 'phi' when you type M-X, instead of the behavior you want (this is discussed in 26.13 Coding Systems for Terminal I/O in version 23). To disable this emacs-unfriendly behavior, enter the line
in the initialization (~/.emacs) file. (Does this also fix the related problem in Aquamacs Emacs, discussed below?)
You can then run emacs from X11 windows (Applications/Utilities/X11.app) with the option/alt key as the meta key. Furthermore, you may use the xmodmap command to remap the command key to Control only under X11. This has the advantage of allowing the command key to behave expectedly with other Mac applications. xmodmap can also ensure that the Alt/Option key is set to be the Meta key in emacs. You can do this with the following ~/.xmodmap file (the lines starting with exclamation points are comments and can be omitted):
Use the above file with the command
before starting emacs (for instance, from /etc/bashrc if you use the login shell option to xterm, i.e. xterm -ls
in X11/Application/Customize for terminal).
You can fix c-h so it brings up help (instead of acting as another delete key) by adding these lines to ~/.xmodmap
However, you must then also execute the command stty erase ^?
.This tells the terminal session that C-? should be used as a backspace/erase key, instead of C-h. Note that '^?' is NOT the caret followed by a questionmark; it is C-?. Generate ^? by either shift-control-questionmark, or if keycode 59 is already set to 127, by hitting the delete key. In emacs, you may generate it by typing C-q prior to either of those sequences. The two steps of reassigning the delete key (keycode 59) and using stty to make it the backspace should be performed together, i.e. from the /etc/bashrc file. Performing these two steps 'frees' C-h to be available for emacs to use as the help key. [NOTE: If you use both emacs and vim, this may break vim's backspace! One 'fix' is to put
Emacs For Mac Os X Xterm 10
into your .vimrc file.]
Native GUI
When using native EmacsForMacOS, the variables mac-option-modifier
, mac-command-modifier
and mac-command-key-is-meta
are useful for changing the behavior of different modifier keys.
In AquamacsEmacs, Option (Alt) is mapped to Meta - however, because Option is used to input a lot of non-ASCII characters such as ü or £ on a variety of keyboards, the special character input methods take precedence by default. You can deselect 'Option key produces only special characters' in the Options menu to use Option as Meta, or you set the variable mac-command-modifier to ‘meta - you will lose functionality, of course. If you use a non-English keybboard layout, the emulation modes might be just what you want. While they leave the Option key mapped to Meta, they will allow you to input many common characters ([,],{,}, etc.) directly with the Option key, just as you would otherwise. You can find them in the Options → Option Key menu - or use, e.g., M-x emulate-mac-german-keyboard-mode.
In CarbonEmacsPackage, you can set the variable mac-option-modifier to ‘meta when your option key is used to input special characters. See also mac-control-modifier and mac-command-modifier if you want to map the meta-key to another modifier key (Ctrl, for example).
Make sure you aren't mapping meta to send another modifier key. Under System Panel → Keyboard & Mouse → Keyboard → Modifier Keys - make sure Option Key is set to Option.
If you want to use your option key as meta in your terminal on the mac, under Terminal → Preferences → Settings → Keyboard check off the check box 'Option as meta key'
Best option in Emacs 23 is to revert to Emacs 22 settings (alt is alt, cmd is meta) with this snippet:
CocoaEmacs has a group of customizable options named Ns, for adjustment of GNUstep/Mac OS X specific features. Some variables in this group can be set to either assign behavior to modifier keys, or remove it to make them behave as usual in Mac OS X. The variables are named ns-{control,command,altenate,function}-modifier. (Each has an alias of the form mac-{control,…}-modifier.) To make a Option (Alt) work as described for AquamacsEmacs above, set ns-alternate-modifier to none (No modifier) in the customize buffer and save the changes.
Console and Terminal
FreeBSD console
Option 1 - Use the Esc
key
That is, press then release Esc
then hit x
to get M-x
.
Option 2 - Use the us.emacs keymap
There is a keymap for Emacs that others report works for them, but it didn't work for me. To try it, you can run
Put
in /etc/rc.conf to use it permanently.
Option 3 - Use a customized keymap
Ideally a keymap could send meta
with the Alt
key. With the us.iso.kdb keymap and newcons, the left Alt
does this, but the right Alt
key does not. With the us.iso.kdb keymap and syscons neither Alt
key works for me.
With newcons I created a customized keymap based off of us.iso.kbd. I simply replaced all instances of ralt
with lalt
.
With syscons, I use this ugly workaround until I can find a better solution. Again, I created a customized keymap based off of us.iso.kbd. First I changed the line with lalt
and replaced all instances of lalt
with meta
. I did the same for ralt
. Next, I used the Windows
key on my keyboard to send alt
. To find the scancodes sent for a key, you can use the misc/kbdscan port. In my case the Windows
key sends the scancode 105, so I added
to my customized keymap.
Finally, I added
to /etc/rc.conf to permanently use the customized keymap.
NetBSD console
before starting emacs (for instance, from /etc/bashrc if you use the login shell option to xterm, i.e. xterm -ls
in X11/Application/Customize for terminal).
You can fix c-h so it brings up help (instead of acting as another delete key) by adding these lines to ~/.xmodmap
However, you must then also execute the command stty erase ^?
.This tells the terminal session that C-? should be used as a backspace/erase key, instead of C-h. Note that '^?' is NOT the caret followed by a questionmark; it is C-?. Generate ^? by either shift-control-questionmark, or if keycode 59 is already set to 127, by hitting the delete key. In emacs, you may generate it by typing C-q prior to either of those sequences. The two steps of reassigning the delete key (keycode 59) and using stty to make it the backspace should be performed together, i.e. from the /etc/bashrc file. Performing these two steps 'frees' C-h to be available for emacs to use as the help key. [NOTE: If you use both emacs and vim, this may break vim's backspace! One 'fix' is to put
Emacs For Mac Os X Xterm 10
into your .vimrc file.]
Native GUI
When using native EmacsForMacOS, the variables mac-option-modifier
, mac-command-modifier
and mac-command-key-is-meta
are useful for changing the behavior of different modifier keys.
In AquamacsEmacs, Option (Alt) is mapped to Meta - however, because Option is used to input a lot of non-ASCII characters such as ü or £ on a variety of keyboards, the special character input methods take precedence by default. You can deselect 'Option key produces only special characters' in the Options menu to use Option as Meta, or you set the variable mac-command-modifier to ‘meta - you will lose functionality, of course. If you use a non-English keybboard layout, the emulation modes might be just what you want. While they leave the Option key mapped to Meta, they will allow you to input many common characters ([,],{,}, etc.) directly with the Option key, just as you would otherwise. You can find them in the Options → Option Key menu - or use, e.g., M-x emulate-mac-german-keyboard-mode.
In CarbonEmacsPackage, you can set the variable mac-option-modifier to ‘meta when your option key is used to input special characters. See also mac-control-modifier and mac-command-modifier if you want to map the meta-key to another modifier key (Ctrl, for example).
Make sure you aren't mapping meta to send another modifier key. Under System Panel → Keyboard & Mouse → Keyboard → Modifier Keys - make sure Option Key is set to Option.
If you want to use your option key as meta in your terminal on the mac, under Terminal → Preferences → Settings → Keyboard check off the check box 'Option as meta key'
Best option in Emacs 23 is to revert to Emacs 22 settings (alt is alt, cmd is meta) with this snippet:
CocoaEmacs has a group of customizable options named Ns, for adjustment of GNUstep/Mac OS X specific features. Some variables in this group can be set to either assign behavior to modifier keys, or remove it to make them behave as usual in Mac OS X. The variables are named ns-{control,command,altenate,function}-modifier. (Each has an alias of the form mac-{control,…}-modifier.) To make a Option (Alt) work as described for AquamacsEmacs above, set ns-alternate-modifier to none (No modifier) in the customize buffer and save the changes.
Console and Terminal
FreeBSD console
Option 1 - Use the Esc
key
That is, press then release Esc
then hit x
to get M-x
.
Option 2 - Use the us.emacs keymap
There is a keymap for Emacs that others report works for them, but it didn't work for me. To try it, you can run
Put
in /etc/rc.conf to use it permanently.
Option 3 - Use a customized keymap
Ideally a keymap could send meta
with the Alt
key. With the us.iso.kdb keymap and newcons, the left Alt
does this, but the right Alt
key does not. With the us.iso.kdb keymap and syscons neither Alt
key works for me.
With newcons I created a customized keymap based off of us.iso.kbd. I simply replaced all instances of ralt
with lalt
.
With syscons, I use this ugly workaround until I can find a better solution. Again, I created a customized keymap based off of us.iso.kbd. First I changed the line with lalt
and replaced all instances of lalt
with meta
. I did the same for ralt
. Next, I used the Windows
key on my keyboard to send alt
. To find the scancodes sent for a key, you can use the misc/kbdscan port. In my case the Windows
key sends the scancode 105, so I added
to my customized keymap.
Finally, I added
to /etc/rc.conf to permanently use the customized keymap.
NetBSD console
For whatever reason, the Alt key does nothing for me. In /etc/wscons.conf i add these lines near the bottom:
I guess this may be more correctly solved by changing the wscons terminal types to something better than vt100, but I've not been able to figure out the correct naming.
OpenBSD console
Add the following line to /etc/wsconsctl.conf:
You can also set it interactively using the wsconsctl command (but the setting will be lost unless you add it to /etc/wsconsctl.conf):
Linux console
If your Meta key works in the console, but Meta + Control + something doesn't work, it may have to do with your .inputrc assuming the console sends 8-bit characters. Take a look at the dumpkeys man page and friends. You will find that for most Linux kernels, some combinations of keys and modifiers are defined, but not for all -- because the kernel will only take around 256 different events.
XTerm
Before trying this make sure that Meta in other terms like aterm is working. The XTerm converts per default Metacommands. To use the Meta-key in XTerm edit follwing:
Mac Emacs 27
The metaSendsEscape flag can also be tried or toggled by holding CTRL and pressing mouse button one and then selecting the ‘meta sends escape' option in the resulting xterm menu.
terminals
It might not be obvious that when using EmacsOnTerminal, at least certain popular DEC terminals in the absense of an Escape-key send the escape prefix sequence on C-3
or C-[
. Often the Emacs term/ libraries configure the f11
to be an ESC
too.
source hacking for xemacs
Locate src/event-Xt.c, find the lines
.. and disable the conditional.
Right now I have XDarwin going and can use xterms and all that jazz. What Im curious about, though, is how come on linux, when I type 'emacs' in an xterm window, emacs pops up in a new window, but in OS X, it only opens up in the xterm window (or Terminal window)? I dont want to use xemacs...though if anyone could point me to a good port of that Id appreciate it too. I really just want to use the good ol emacs but be able to use my mouse to click on the menu items and scroll bar, etc. And also get colors going.
Thanks!