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.

5 Answers 5

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.

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, 2013 at 3: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 >> <filename> (1)"
    – gcb
    Dec 10, 2015 at 14:08
  • 1
    @gcb: See the other answer by cseelus. Oct 3, 2022 at 7:26
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
4
  • 1
    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! Sep 30, 2015 at 2:23
  • 1
    How could one make this work in insert mode? Jul 1, 2016 at 12:24
  • This is beyond beautiful! Thanks! 🙏
    – Nik
    Aug 1, 2019 at 12:09
  • Best answer ever.
    – j5shi
    Jun 23, 2020 at 2:57
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

3
  • This is not what the OP is asking for... the question was to switch tabs in MacVim, not terminal
    – JESii
    May 16, 2015 at 5:07
  • 2
    But this IS a MacVim keyboard shortcut. Command keys don't translate to terminal vim.
    – spoulson
    Dec 1, 2016 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 <kbd>Shift</kbd> key when doing <kbd>⌘</kbd>+<kbd>}</kbd>.
    – brettlyman
    Dec 11, 2017 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
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.)

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .