16

Let's say I have four open files: file0.txt, file1.txt, file2.txt, and file3.txt. I open all of them in that order as tabs. So my tab ordering is this:

  • file0.txt
  • file1.txt
  • file2.txt
  • file3.txt

Then let's say that I want to instead reorder my tabs so that file2.txt comes before file1.txt:

  • file0.txt
  • file2.txt
  • file1.txt
  • file3.txt

How do I do that? Is there a Vim command to move the current tab to the left or right, or otherwise reorder the currently opened tabs?

18

You can use the tabmove command. (:help tabmove will tell you all you need to know)

The command takes two kind of parameters:

A sign (+ or -) followed by a number:

  • :tabmove +2 will move your tab of 2 places to the right
  • :tabmove -2 will do the same to the left.

You can also use the command with a simple number to set the position of the tab:

  • :tabmove 0 will put the tab as the first one
  • :tabmove 2 will put the tab in the third position
  • :tabmove will put the tab as the last one

Also I put those lines in my .vimrc

" move current tab to left/right
noremap <Leader><Left>  :execute 'silent! tabmove ' . (tabpagenr()-2)<CR>
noremap <Leader><Right> :execute 'silent! tabmove ' . tabpagenr()<CR>

They allow you to move the current tab of one place with the combination Leader+Right arrow and Leader+Left arrow maybe they could be useful to you.

Edit: As @TomMcDo pointed out in the comments the previous mappings could be simplified and still work properly:

noremap <Leader><Left>  :tabmove -1<CR>
noremap <Leader><Right> :tabmove +1<CR>
  • 3
    In your mappings, why not use :tabmove -1 and :tabmove +1? – tommcdo Jul 3 '15 at 13:27
  • @tommcdo: Well... I made those mappings several months ago and I have no idea of what was in my mind at this moment. And I didn't event gave a thought about it when I copied it in my answer. Anyway you're totally right I'll edit my answer to make is simpler thanks for the heading :-) – statox Jul 3 '15 at 13:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.