20

Are there any shortcuts to switch the Tabs from one to another in MacVim?

MacVim Tabs

Any tips to bind the shortcuts myself in .vimrc like ⌘ + 1 for Tab 1 and ⌘ + 2 for Tab 2. For example, like switching browser tabs.

edit: i'm back to working on linux. Not being forced to sufer a Mac just entered my list of demands for new jobs. Good luck to anyone else still dealing with this.

gcb
  • 4,764
  • 11
  • 53
  • 73
Ye Lin Aung
  • 5,650
  • 7
  • 32
  • 35

5 Answers5

26

You can of course change shortcuts with OSXs system preferences for your keyboard as shown here: How to Remap Any Keyboard Shortcut in Mac OS X

Some might prefer to do it via their .vimrc:

if has("gui_macvim")
  " Press Ctrl-Tab to switch between open tabs (like browser tabs) to 
  " the right side. Ctrl-Shift-Tab goes the other way.
  noremap <C-Tab> :tabnext<CR>
  noremap <C-S-Tab> :tabprev<CR>

  " Switch to specific tab numbers with Command-number
  noremap <D-1> :tabn 1<CR>
  noremap <D-2> :tabn 2<CR>
  noremap <D-3> :tabn 3<CR>
  noremap <D-4> :tabn 4<CR>
  noremap <D-5> :tabn 5<CR>
  noremap <D-6> :tabn 6<CR>
  noremap <D-7> :tabn 7<CR>
  noremap <D-8> :tabn 8<CR>
  noremap <D-9> :tabn 9<CR>
  " Command-0 goes to the last tab
  noremap <D-0> :tablast<CR>
endif
cseelus
  • 363
  • 3
  • 6
18

Since MacVim is an actual program on Mac OS, you can map tab switching the same way you map commands in any program (which I personally just learned about recently).

Open up System Preferences, select "Keyboard", then "Application Shortcuts" (in the left menu). Under the menu on the right, click on the plus (+) to add a new command. Choose MacVim for the application, and for the menu title, type "Select Next Tab", and choose a shortcut (I chose Cmd+right arrow). Then do the same thing for the command "Select Previous Tab".

"Select Next Tab" and "Select Previous Tab" are found in MacVim under the "Window" menu. Any option you see in any of the menus for an app can be remapped using this method.

DevDaddyNick
  • 196
  • 1
  • 3
  • 1
    Since the question was about Macvim, this solution is nicer. The only thing is that when you want to port your .vimrc to other machines this may not work there but I still prefer this. Thanks. – sabertooth Mar 13 '13 at 03:11
  • on windows/linux i map Alt+1...10 to the tab order. Would be nice to have command+1 go to the first tab, as it is for every single other application on macs. Anyway to do that? The menu for that is "Buffer >> (1)" – gcb Dec 10 '15 at 14:08
  • 1
    @gcb: See the other answer by cseelus. – nishanthshanmugham Oct 03 '22 at 07:26
14

You can Select Next Tab with +} and Select Previous Tab with +{

The shift key is required to not just hit a [ instead of a }
So the shortcut is +shift+] or +shift+[
This shortcuts works in many apps, i.e. in the Terminal

Masolino
  • 172
  • 1
  • 3
  • This is not what the OP is asking for... the question was to switch tabs in MacVim, not terminal – JESii May 16 '15 at 05:07
  • 2
    But this IS a MacVim keyboard shortcut. Command keys don't translate to terminal vim. – spoulson Dec 01 '16 at 22:31
  • I am using MacVim and as mentioned in this solution, it already has keyboard commands to switch tabs. I simply was missing the Shift key when doing +}. – brettlyman Dec 11 '17 at 18:03
5

I have the following in my ~/.vimrc for Linux. You should be able to change the "<M-" sequence to "<D-" to get what you want:

" Meta+1-0 jumps to tab 1-10, Shift+Meta+1-0 jumps to tab 11-20:
let s:windowmapnr = 0
let s:wins='1234567890!@#$%^&*()'
while (s:windowmapnr < strlen(s:wins))
    exe 'noremap <silent> <M-' . s:wins[s:windowmapnr] . '> ' . (s:windowmapnr + 1) . 'gt'
    exe 'inoremap <silent> <M-' . s:wins[s:windowmapnr] . '> <C-O>' . (s:windowmapnr + 1) . 'gt'
    exe 'cnoremap <silent> <M-' . s:wins[s:windowmapnr] . '> <C-C>' . (s:windowmapnr + 1) . 'gt'
    exe 'vnoremap <silent> <M-' . s:wins[s:windowmapnr] . '> <C-C>' . (s:windowmapnr + 1) . 'gt'
    let s:windowmapnr += 1
endwhile
unlet s:windowmapnr s:wins
Heptite
  • 19,383
  • 5
  • 58
  • 71
4

In addition to making your own mappings, there are built-in vim shortcuts. Try a number followed by gt. For example: 3gt takes you to the 3rd tab. You can also do just gt to go to the next tab, or gT to go to the previous one.

(Since vim 7.something, tabs have been baked in even in the text-mode non-gvim version.)

murftown
  • 141
  • 2