55

Is there a way to change tabs order in Vim (i.e. change the position of the tabs in the tab bar)? For example, let's say my current tabs are in this order:

A | B | C | D

But I would like to switch the position of the tabs to something like:

A | C | B | D

How can I do that?

double-beep
  • 5,031
  • 17
  • 33
  • 41
nothing-special-here
  • 11,230
  • 13
  • 64
  • 94
  • 1
    possible duplicate of [Is there a vim command to relocate a tab?](http://stackoverflow.com/questions/7961581/is-there-a-vim-command-to-relocate-a-tab) – cfi Sep 22 '15 at 09:17

3 Answers3

75

You can use :tabmove followed by the tab number to move past. For example, :tabmove 3 will make the current tab move past the 3rd. :tabmove 0 moves to the beginning and :tabmove (without a number) moves to the end.

Another way - though not orthodox - is to enable mouse via :set mouse=a and drag-and-drop tabs around. It might look simpler for a start.

Eric Mathison
  • 1,210
  • 10
  • 16
Mihai Maruseac
  • 20,967
  • 7
  • 57
  • 109
  • 15
    I've gotten used to the (slightly) shorter version: `:tabm 3` – Walter Jul 02 '12 at 14:10
  • There's a way to use the mouse for this, I have updated the answer:) – Mihai Maruseac Jul 02 '12 at 15:57
  • 2
    If it's convenience you're looking for, you can take a look at my .vimrc file [here](https://github.com/kshenoy/dotvim/blob/master/mappings.vim), particularly lines #312-339. It is, in essence, the same as the previous answers, but with pretty wrapper around it. – kshenoy Jul 02 '12 at 22:13
  • 5
    kshenoy - The link is no longer valid as of 2013/11/29 – squeegee Nov 29 '13 at 20:52
  • 2
    Oddly, gVim won't let you move tabs around even with `:set mouse=a`. If you're using gVim and you're confused, the mouse trick only applies to the text menu displayed in "regular" vim. – Two-Bit Alchemist Feb 27 '15 at 19:20
10

Move Tabs to the Left / Right

To me it makes much more sense to move the tabs to the left or right of their current position instead of first figuring out the exact numerical position I want them at. These simple keymaps do exactly that:

noremap <A-Left>  :-tabmove<cr>
noremap <A-Right> :+tabmove<cr>

Now you'll be able to move the current tab:

  • To the left using: Alt + Left
  • To the right using: Alt + Right

For MacVim, try using M instead of A (i.e. <M-Left>)

Sheharyar
  • 73,588
  • 21
  • 168
  • 215
  • This mapping does not move the tabs by one step on my Ubuntu 14.04 and vim 7.4. – Right leg Dec 21 '16 at 05:22
  • I'm also on Ubuntu 14.04 with Vim 7.4 and it works for me. Maybe your terminal is not configured to handle `Alt` key. Try something else (such as your `Leader` key) – Sheharyar Dec 21 '16 at 14:24
  • Sorry, I posted that previous comment a bit in a hurry. What I meant is, that mapping does not move tabs by one step, but moves them back and forth between two locations. Say `tab` is at location `A`; alt+right moves it to `B`, then alt+right does nothing but alt+left moves it back to `A`, then alt+left does nothing but alt+right moves it forth to `A`, and so on. – Right leg Dec 21 '16 at 14:48
  • That's exactly the intention and answers the question. Try it with a few more tabs and you'll know why it's better. – Sheharyar Dec 21 '16 at 14:51
  • 1
    Wow this is a much better technique. Just what I wanted. – VinGarcia Sep 04 '17 at 21:34
  • Don't know why only +tabmove is working. (vim 7.4.629) – Fisher Oct 16 '17 at 14:04
4

For me -tabmove is not working.

I'm using below command: Ctrl+Shift+PageUp|PageDown.

nmap <C-S-PageUp>   :tabmove -1<cr>
nmap <C-S-PageDown> :tabmove +1<cr>
Fisher
  • 376
  • 3
  • 14