Search code examples
vimcmdmappingkey-bindingsnerdtree

How do I map CMD (Command ⌘) to shift MacVim window


I know that I can use ctrl+w to shift between different Vim windows. But how do I remap the CMD-key to replace ctrl+w in various way? I'm specifically looking to bind cmd with the arrow keys, so that I can shift to NERDTree easily with CMD+LeftArrow. Appreciate the assistance.

I've tried to add the following to $MYVIMRC...

nmap <silent> <D-h> :wincmd h<CR>   // For going to NERDTree 
nmap <silent> <D-l> :wincmd l<CR>   // For going back to file I'm working on.

Solution

  • In the left-hand side of a mapping, Command+Left is written <D-Left>. With this and other issues fixed (see below), your mappings should look like this:

    nnoremap <D-Left> <Cmd>wincmd h<CR>
    nnoremap <D-Right> <Cmd>wincmd l<CR>
    

    or, simply, like this:

    nnoremap <D-Left> <C-w>h
    nnoremap <D-Right> <C-w>l
    

    Other issues:

    • Recursive mappings (nmap) should be reserved to the rare situations where you want to use another mapping in your mapping. This is not the case, here, so nnoremap is the best choice.
    • The mapped commands don't echo anything so <silent> is useless.
    • Vim's comment leader is ", not //.
    • You can't have comments on the same line as a mapping anyway, see :help map-comments.
    • The newish :help <Cmd> is cleaner than using : in the right-hand side of a mapping.

    Note that these mappings only work in the MacVim GUI.