Shortcut to switch tabs in MacVim

11,965

Solution 1

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.

Solution 2

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

Solution 3

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

Solution 4

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

Solution 5

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.)

Share:
11,965

Related videos on Youtube

Ye Lin Aung
Author by

Ye Lin Aung

Programmer.

Updated on September 18, 2022

Comments

  • Ye Lin Aung
    Ye Lin Aung over 1 year

    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.

  • sabertooth
    sabertooth about 11 years
    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.
  • JESii
    JESii about 9 years
    This is not what the OP is asking for... the question was to switch tabs in MacVim, not terminal
  • Brian Underwood
    Brian Underwood over 8 years
    Thanks, I much prefer this solution to the selected one because I use command-right|left-arrow for going to the end/beginning of lines!
  • gcb
    gcb over 8 years
    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 >> <filename> (1)"
  • Oliver Joseph Ash
    Oliver Joseph Ash almost 8 years
    How could one make this work in insert mode?
  • Cam
    Cam over 7 years
    But this IS a MacVim keyboard shortcut. Command keys don't translate to terminal vim.
  • brettlyman
    brettlyman over 6 years
    I am using MacVim and as mentioned in this solution, it already has keyboard commands to switch tabs. I simply was missing the <kbd>Shift</kbd> key when doing <kbd>⌘</kbd>+<kbd>}</kbd>.
  • Nik
    Nik almost 5 years
    This is beyond beautiful! Thanks! 🙏
  • j5shi
    j5shi almost 4 years
    Best answer ever.