23
votes

Is there any way in gVim to rearrange tabs by dragging and dropping them with the mouse? The behavior I'm looking for is that similar to tabs in Firefox and Chrome.

I know that it's possible to change tab order using :tabm n but that requires figuring out exactly how many tabs in you'd like to move to. Using the mouse would be more useful for this spatial task.

Any methods to move tabs left/right by one position would also be useful, since one could remap keys and move tabs without thinking too hard.

6
This is not useful for GTK users, but I have found that moving tabs with the mouse works just fine in MacVim.werkshy

6 Answers

23
votes

Here's what's in my vimrc regarding tabs:

" Move tabs with alt + left|right
nnoremap <silent> <A-Left> :execute 'silent! tabmove ' . (tabpagenr()-2)<CR>
nnoremap <silent> <A-Right> :execute 'silent! tabmove ' . tabpagenr()<CR>
10
votes

Here is a function to move a tab to the left one position. Put it in your vimrc file and set up your keys as you see fit (to call it longhand, :execute TabLeft()).

Note that these functions "roll" tabs from first to last and last to first, respectively, so moving the first tab left makes it the last tab, and moving the last tab right makes it the first tab.

function TabLeft()
   let tab_number = tabpagenr() - 1
   if tab_number == 0
      execute "tabm" tabpagenr('$') - 1
   else
      execute "tabm" tab_number - 1
   endif
endfunction

...and to the right

function TabRight()
   let tab_number = tabpagenr() - 1
   let last_tab_number = tabpagenr('$') - 1
   if tab_number == last_tab_number
      execute "tabm" 0
   else
      execute "tabm" tab_number + 1
   endif
endfunction
4
votes

Thanks, and I have modified your code for my vimrc :

function ShiftTab(direction)
     let tab_number = tabpagenr() 
     if a:direction == 0
         if tab_number == 1
             exe 'tabm' . tabpagenr('$')
         else
             exe 'tabm' . (tab_number - 2)
         endif
     else
         if tab_number == tabpagenr('$')
             exe 'tabm ' . 0
         else
             exe 'tabm ' . tab_number
         endif
     endif
     return ''
endfunction

Then in my GVim, I map [ctrl+shift+left] to move left, [ctrl+shift+right] to move left

inoremap <silent> <C-S-Left>  <C-r>=ShiftTab(0)<CR>
inoremap <silent> <C-S-Right>  <C-r>=ShiftTab(1)<CR>

noremap <silent> <C-S-Left>  :call ShiftTab(0)<CR>
noremap <silent> <C-S-Right> :call ShiftTab(1)<CR>
3
votes

chris.ritsen's solution stopped working for me in vim v7.4 so here's a simpler alternative:

" Move tabs left/right
nnoremap <silent> <s-left> :-tabmove<cr>
nnoremap <silent> <s-right> :+tabmove<cr>
1
votes

Ken Takata wrote a patch to do this https://groups.google.com/forum/#!msg/vim_dev/LnZVZYls1yk/PHQl4WNDAgAJ . One option is to download vim source code, aply this patch and compile.

0
votes

Move Tabs to the Left / Right

This doesn't involve using a mouse, but it uses very simple keymaps for gvim:

noremap <A-[> :-tabmove<cr>
noremap <A-]> :+tabmove<cr>

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

  • To the left using: Alt + [
  • To the right using: Alt + ]