Switch buffers in MacVim

6

2

I want to be able to switch buffers (:bn! / :bp!) in MacVim, using shortcuts like

Alt (Option) - Left Arrow Key/Right Arrow Key

So far I wasn't able to do that.

Here's my mapping for the keys, which works perfectly fine in Linux, but not on MacOS:

map <M-Right> :bn!<CR>
map <M-Left> :bp!<CR>

Art

Posted 2011-07-14T05:24:34.403

Reputation: 921

Answers

8

Vim initialization files are processed in this order (simplified; see :help initialization):

  1. $VIM/vimrc
  2. $HOME/.vimrc
  3. $VIM/gvimrc
  4. $HOME/.gvimrc

The problem is that MacVim maps M-Right and M-Left (and several other key combinations) in its $VIM/gvimrc (i.e. …/MacVim.app/Contents/Resources/vim/gvimrc); this will override any mappings you have made on those key combinations in $HOME/.vimrc.

If you examine this startup file (i.e. :view $VIM/gvimrc), you will find that these particular mappings can be inhibited by setting the special variable macvim_skip_cmd_opt_movement (to any value) in your $HOME/.vimrc.

let macvim_skip_cmd_opt_movement = 1

This will inhibit ten default MacVim mappings (Command and Option for each of Left, Right, Up, Down, and BS).

Alternatively, you could just put your mappings in your $HOME/.gvimrc (where they will override the default MacVim mappings even if you have not inhibited them with macvim_skip_cmd_opt_movement).

Chris Johnsen

Posted 2011-07-14T05:24:34.403

Reputation: 31 786

Chris, you are awesome!!! Thank you so much!!! – Art – 2011-07-18T02:37:19.303

3

Try putting this in your .vimrc file:

" Set left and right option/alt keys to be meta keys
set macmeta

Then try those mappings again and see if they work. If you want to reverse it simply do :set nomacmeta.

If you also use this .vimrc file on computers without MacVim on them use this instead:

" Set left and right option/alt keys to be meta keys
if has("gui_macvim")
    set macmeta
endif

lyallcooper

Posted 2011-07-14T05:24:34.403

Reputation: 481

I get 'Invalid argument macmeta=rl' error message. If I do ':set macmeta rl' it turns all the text into right-to left view – Art – 2011-07-14T06:51:13.340

Well you don't want to do ':set macmeta rl' because that is essentially two commands ':set macmeta' and ':set rl' (which you figured out does right to left text).

I realize I made a mistake in my original answer now (I couldn't test it because I don't have MacVim). I'll edit it, and see if the new solution works for you. – lyallcooper – 2011-07-14T06:56:15.617

"set macmeta" unfortunately does not fix it, pressing alt-'->' just makes cursor jump to next word – Art – 2011-07-14T23:28:23.480

Thanks for the has("gui_macvim") though! – Art – 2011-07-19T03:05:48.830