I think this is a bug in Mac OS X and has something to do with the way the keyboard shortcuts for Spaces are processed. Try this:
- Make sure Spaces is turned on in the System Preferences with the standard 4 spaces layout, and standard key combination for switching to a space.
- Open a folder in the Finder.
- Press and hold CTRL, and press any of the number keys. If you press 5, 6, 7, 8, 9 or 0 first you will get a beep. Same if you press one of those keys again (still holding CTRL). If you then press 1, 2, 3 or 4, you will switch to that space number; or just stay in that space if you were already in it. If you then press any of 5, 6, 7, 8, 9 or 0 again, it won't beep anymore.
If you simply turn off Spaces, the above won't happen and whatever order you press the keys in, they will continue to beep. If you change the number of spaces to only two, then 3, 4, 5, 6, 7, 8, 9 and 0 will give you a beep until you've pressed either 1 or 2. I don't know of an application in which those keys have a non-Spaces functionality and would do something useful other than beep, so I haven't been able to try what happens in such an application. In any case, I don't think you need to worry there's something wrong with your keyboard or the way you've configured the keyboard bindings; it seems to be a bug in Mac OS X.
Edit: Try the above in a new folder in which you've created folders named "1", "2" and so on. I find the Finder's behavior in this case somewhat strange: if you press CTRL-8, it will beep yet also switch the selection to the folder named "8". But again, pressing 5 through 9 will neither beep, nor change the selected folder once you've pressed any of 1 through 4.
I have similar desires to yours. I solved it using a system-level modifier-key re-bind.
(Of course, this is a mess; but I find it worth it to maintain my MacVim-trained hybrid of OS-wide controls (save, new, copy, paste) within command-line vim
s.)
Here's how I did it:
- Install Karabiner, which is a very, very powerful program (and an essential part of any poweruser's / programmer's OS X installation, IMHO)
Enable “Command_L to Control_L (only in Terminal)”:
Map ⌃-prefixed controls matching whichever MacVim commands you're most used-to in command-line vim
. For instance, to support ⌘S,
noremap <silent> <C-S> :update<CR>
vnoremap <silent> <C-S> <C-C>:update<CR>
inoremap <silent> <C-S> <C-O>:update<CR>
Restore expected ⌘-sequences for Terminal.app itself, by special-casing them in System Preferences. For instance, if you want ⌘N to still control Terminal tabs, instead of mapping it to vim
files, try:
Now, anything you don't add in this list, will be passed through verbatim to command-line vim
(and, of course, any other command-line program you use.)
Caveats
This approach has several further subtle downsides, that I want to hilight for anyone considering taking it (as I do):
- ⌘␣ will no longer map to the system-wide Spotlight. (I've an open issue on Karabiner, and will come back here once I have a stopgap solution.)
- For reasons I can't ascertain, even with the ⌃W exclusions set up in System Preferences, the tab-closing behaviour of Terminal.app changes with this setup: ⌘W will close individual tabs in a window; but if the current tab is the last tab, it will not close the window. Instead, it has no effect. I've had to re-train myself to use ⇧⌘W to close the window if there's only one tab left.
- If you've trained yourself to use ⌃V in MacVim to do block-wise selection, then you'll have to re-train yourself to use the alternative, ⌃Q (or, now, ⌘Q). Additionally, you'll have to add
stty -ixon
in your shell's startup-script, as ⌃Q is consumed by default to preform an archaic shell-control function (one with no modern usage.)
Alternative ⌘W behaviour
In addition to the above, I decided to further override ⌘W as a universal ‘close this’ key, applicable to multiple applications.
Instead of mapping ⌘W to ‘Close Tab,’ as above; I personally map that to ⇧⌘W (to retain “immediately close this tab” semantics.) Then, in any given program, I map ⌃W to some sort of close-save-quit functionality.
For instance, in my .vimrc
:
noremap <C-W> :confirm quit<CR>
vnoremap <C-W> <C-C>:confirm quit<CR>
inoremap <C-W> <C-O>:confirm quit<CR>
… or my .zshrc
:
function close_tab { exit }
zle -N close_tab_widget close_tab
bindkey '' close_tab_widget
bindkey -M vicmd '' close_tab_widget
With those two snippets in place, and your Terminal configured to ‘Close if shell exits cleanly,’ a series of ⌘Ws will first quit vim, and then close the shell entirely.
In addition, now additional on-exit functionality of programs can be preserved: for instance, vim
will ask before quitting if files are unsaved (just like MacVim); and zsh
will ask before quitting if there are background jobs.
Postscript: Not directly related to your question; but you should also check out ‘Ubiquitous Vim Mode’ in Karabiner. It's “a bit much” for me to use everyday, but it's a very cool idea, and surprisingly-well implemented.
Best Answer
In the Terminal preferences, go to Settings and select Keyboard in the panel. There you can set various key combinations to send whatever string you wish to the shell (or the program currently running).