vim: toggle between tabs and toggle between buffers

classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

vim: toggle between tabs and toggle between buffers

ping song
experts:

currently I have a map to quickly switch between 2 tabs:

nmap ,l :exe "tabn “.g:lasttab<CR>
au TabLeave * let g:lasttab = tabpagenr()

it works nice.

but I'm thinking to extend that to another scenario, that if I got only one tab left but still have multiple buffers, the same map will toggle between the last 2 buffers used instead of tabs.

how to archive this ?

thanks.

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
 
---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/groups/opt_out.
Reply | Threaded
Open this post in threaded view
|

Re: vim: toggle between tabs and toggle between buffers

MarcWeber
> the last 2 buffers used instead of tabs.
<c-^> or :b # will do exactly that

Marc Weber

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/groups/opt_out.
Reply | Threaded
Open this post in threaded view
|

Re: vim: toggle between tabs and toggle between buffers

Paul Isambert
In reply to this post by ping song
"ping song" <[hidden email]>:

> currently I have a map to quickly switch between 2 tabs:
>
> nmap ,l :exe "tabn “.g:lasttab<CR>
> au TabLeave * let g:lasttab = tabpagenr()
>
> it works nice.
>
> but I'm thinking to extend that to another scenario, that if I got
> only one tab left but still have multiple buffers, the same map will
> toggle between the last 2 buffers used instead of tabs.
>
> how to archive this ?

Check whether there exists more than one tab page; if not, use ":b #" as
indicated by Marc:

    function! s:switch()
      if tabpagenr("$") > 1
        exe "tabnext" g:lasttab
      else
        b #
      endif
    endfunction
    noremap <silent> ,l :call <SID>switch()

Best,
Paul

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/groups/opt_out.
Reply | Threaded
Open this post in threaded view
|

Re: vim: toggle between tabs and toggle between buffers

ping song
thanks. this is nice. just one more <enter> in the end.

noremap <silent> ,l :call <SID>switch()<enter>

reading about <SID> again...(I figured it out ever)


On Thu, Dec 12, 2013 at 4:40 AM, Paul Isambert <[hidden email]> wrote:
"ping song" <[hidden email]>:
> currently I have a map to quickly switch between 2 tabs:
>
> nmap ,l :exe "tabn “.g:lasttab<CR>
> au TabLeave * let g:lasttab = tabpagenr()
>
> it works nice.
>
> but I'm thinking to extend that to another scenario, that if I got
> only one tab left but still have multiple buffers, the same map will
> toggle between the last 2 buffers used instead of tabs.
>
> how to archive this ?

Check whether there exists more than one tab page; if not, use ":b #" as
indicated by Marc:

    function! s:switch()
      if tabpagenr("$") > 1
        exe "tabnext" g:lasttab
      else
        b #
      endif
    endfunction
    noremap <silent> ,l :call <SID>switch()

Best,
Paul

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/groups/opt_out.

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
 
---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/groups/opt_out.
Reply | Threaded
Open this post in threaded view
|

Re: vim: toggle between tabs and toggle between buffers

ping song
furthermore,  I want to overide Ngt to :bN<enter>, when there is only one tab...
this looks requiring N being extracted and used as a parameter of the function...
how to implement that?

thanks!


On Thu, Dec 12, 2013 at 11:29 AM, ping song <[hidden email]> wrote:
thanks. this is nice. just one more <enter> in the end.

noremap <silent> ,l :call <SID>switch()<enter>

reading about <SID> again...(I figured it out ever)



On Thu, Dec 12, 2013 at 4:40 AM, Paul Isambert <[hidden email]> wrote:
"ping song" <[hidden email]>:
> currently I have a map to quickly switch between 2 tabs:
>
> nmap ,l :exe "tabn “.g:lasttab<CR>
> au TabLeave * let g:lasttab = tabpagenr()
>
> it works nice.
>
> but I'm thinking to extend that to another scenario, that if I got
> only one tab left but still have multiple buffers, the same map will
> toggle between the last 2 buffers used instead of tabs.
>
> how to archive this ?

Check whether there exists more than one tab page; if not, use ":b #" as
indicated by Marc:

    function! s:switch()
      if tabpagenr("$") > 1
        exe "tabnext" g:lasttab
      else
        b #
      endif
    endfunction
    noremap <silent> ,l :call <SID>switch()

Best,
Paul

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/groups/opt_out.


--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
 
---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/groups/opt_out.
Reply | Threaded
Open this post in threaded view
|

Re: vim: toggle between tabs and toggle between buffers

Nikolay Aleksandrovich Pavlov


On Dec 13, 2013 1:56 AM, "ping song" <[hidden email]> wrote:
>
> furthermore,  I want to overide Ngt to :bN<enter>, when there is only one tab...
> this looks requiring N being extracted and used as a parameter of the function...
> how to implement that?

Use v:count or v:count1. Knowing that "2:" results in ":.,.+1" you may also extract count using ":function Func() range" or ":command-range".

>
> thanks!
>
>
> On Thu, Dec 12, 2013 at 11:29 AM, ping song <[hidden email]> wrote:
>>
>> thanks. this is nice. just one more <enter> in the end.
>>
>> noremap <silent> ,l :call <SID>switch()<enter>
>>
>> reading about <SID> again...(I figured it out ever)
>>
>>
>>
>> On Thu, Dec 12, 2013 at 4:40 AM, Paul Isambert <[hidden email]> wrote:
>>>
>>> "ping song" <[hidden email]>:
>>> > currently I have a map to quickly switch between 2 tabs:
>>> >
>>> > nmap ,l :exe "tabn “.g:lasttab<CR>
>>> > au TabLeave * let g:lasttab = tabpagenr()
>>> >
>>> > it works nice.
>>> >
>>> > but I'm thinking to extend that to another scenario, that if I got
>>> > only one tab left but still have multiple buffers, the same map will
>>> > toggle between the last 2 buffers used instead of tabs.
>>> >
>>> > how to archive this ?
>>>
>>> Check whether there exists more than one tab page; if not, use ":b #" as
>>> indicated by Marc:
>>>
>>>     function! s:switch()
>>>       if tabpagenr("$") > 1
>>>         exe "tabnext" g:lasttab
>>>       else
>>>         b #
>>>       endif
>>>     endfunction
>>>     noremap <silent> ,l :call <SID>switch()
>>>
>>> Best,
>>> Paul
>>>
>>> --
>>> --
>>> You received this message from the "vim_use" maillist.
>>> Do not top-post! Type your reply below the text you are replying to.
>>> For more information, visit http://www.vim.org/maillist.php
>>>
>>> ---
>>> You received this message because you are subscribed to the Google Groups "vim_use" group.
>>> To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
>>> For more options, visit https://groups.google.com/groups/opt_out.
>>
>>
>
> --
> --
> You received this message from the "vim_use" maillist.
> Do not top-post! Type your reply below the text you are replying to.
> For more information, visit http://www.vim.org/maillist.php
>  
> ---
> You received this message because you are subscribed to the Google Groups "vim_use" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
> For more options, visit https://groups.google.com/groups/opt_out.

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
 
---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/groups/opt_out.
Reply | Threaded
Open this post in threaded view
|

Re: vim: toggle between tabs and toggle between buffers

ping song
I tried these pseudo-codes, but these are just 1 time thing and won't change over time...couldn't figure out a good way.

;if more than one tab, don't hack
if tabpagenr("$") > 1
  unmap gt
  unmap gT
;otherwise , if there is one tab, use gt,gT,Ngt to switch between buffers
else
  if v:count == 0
    map gt :bn<cr>
    map gT :bp<cr>
  else
    map gt :<C-U>exec "b" . v:count<cr>
  endif
endif






On Thu, Dec 12, 2013 at 11:23 PM, Nikolay Pavlov <[hidden email]> wrote:


On Dec 13, 2013 1:56 AM, "ping song" <[hidden email]> wrote:
>
> furthermore,  I want to overide Ngt to :bN<enter>, when there is only one tab...
> this looks requiring N being extracted and used as a parameter of the function...
> how to implement that?

Use v:count or v:count1. Knowing that "2:" results in ":.,.+1" you may also extract count using ":function Func() range" or ":command-range".

>
> thanks!
>
>
> On Thu, Dec 12, 2013 at 11:29 AM, ping song <[hidden email]> wrote:
>>
>> thanks. this is nice. just one more <enter> in the end.
>>
>> noremap <silent> ,l :call <SID>switch()<enter>
>>
>> reading about <SID> again...(I figured it out ever)
>>
>>
>>
>> On Thu, Dec 12, 2013 at 4:40 AM, Paul Isambert <[hidden email]> wrote:
>>>
>>> "ping song" <[hidden email]>:
>>> > currently I have a map to quickly switch between 2 tabs:
>>> >
>>> > nmap ,l :exe "tabn “.g:lasttab<CR>
>>> > au TabLeave * let g:lasttab = tabpagenr()
>>> >
>>> > it works nice.
>>> >
>>> > but I'm thinking to extend that to another scenario, that if I got
>>> > only one tab left but still have multiple buffers, the same map will
>>> > toggle between the last 2 buffers used instead of tabs.
>>> >
>>> > how to archive this ?
>>>
>>> Check whether there exists more than one tab page; if not, use ":b #" as
>>> indicated by Marc:
>>>
>>>     function! s:switch()
>>>       if tabpagenr("$") > 1
>>>         exe "tabnext" g:lasttab
>>>       else
>>>         b #
>>>       endif
>>>     endfunction
>>>     noremap <silent> ,l :call <SID>switch()
>>>
>>> Best,
>>> Paul
>>>
>>> --
>>> --
>>> You received this message from the "vim_use" maillist.
>>> Do not top-post! Type your reply below the text you are replying to.
>>> For more information, visit http://www.vim.org/maillist.php
>>>
>>> ---
>>> You received this message because you are subscribed to the Google Groups "vim_use" group.
>>> To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
>>> For more options, visit https://groups.google.com/groups/opt_out.
>>
>>
>
> --
> --
> You received this message from the "vim_use" maillist.
> Do not top-post! Type your reply below the text you are replying to.
> For more information, visit http://www.vim.org/maillist.php
>  
> ---
> You received this message because you are subscribed to the Google Groups "vim_use" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
> For more options, visit https://groups.google.com/groups/opt_out.

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
 
---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/groups/opt_out.

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
 
---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/groups/opt_out.
Reply | Threaded
Open this post in threaded view
|

Re: vim: toggle between tabs and toggle between buffers

Nazri Ramliy
On Fri, Dec 13, 2013 at 2:51 PM, ping song <[hidden email]> wrote:
> I tried these pseudo-codes, but these are just 1 time thing and won't change
> over time...couldn't figure out a good way.

Use the '<expr>' argument to map to have a dynamic behavior:

  function NextBufferOrNextTab()
    if tabpagenr("$") > 1
      return ":tabn " . g:lasttab . "\<cr>"
    else
      return ":bn\<cr>"
    endif
  endfun

  nnoremap <expr> ,l NextBufferOrNextTab()

Untested of course :)

I used similar method to have a dynamic map behavior for closing my
buffers [1].

Nazri

[1] quickquit.vim: https://gist.github.com/holygeek/7909458

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/groups/opt_out.
Reply | Threaded
Open this post in threaded view
|

Re: vim: toggle between tabs and toggle between buffers

Paul Isambert
In reply to this post by ping song
De: "ping song" <[hidden email]>:

> I tried these pseudo-codes, but these are just 1 time thing and won't
> change over time...couldn't figure out a good way.
>
>
> ;if more than one tab, don't hack
>
> if tabpagenr("$") > 1
> unmap gt
> unmap gT
> ;otherwise , if there is one tab, use gt,gT,Ngt to switch between
> buffers
> else
>
> if v:count == 0
> map gt :bn<cr>
> map gT :bp<cr>
> else
> map gt :<C-U>exec "b" . v:count<cr>
> endif
> endif

Do this:

    function! s:nswitch (n)
      if tabpagenr("$") >= a:n
        exe "tabnext" a:n
      else
        exe "buffer" a:n
      endif
    endfunction

    noremap <silent> gt :<C-U>call <SID>nswitch(v:count)<CR>

Now Ngt goes to tab N if it exists, to buffer N otherwise.

Best,
Paul

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/groups/opt_out.
Reply | Threaded
Open this post in threaded view
|

Re: vim: toggle between tabs and toggle between buffers

ping song
thanks Paul, your code is nearly perfect!

after some test this is the one that done all works well -- just use standard gt, gT, or Ngt to do all work with regardless of tab and buffer 

function! s:nswitch (n)
  if tabpagenr("$") > 1
    if a:n > 0
        exe "tabnext" a:n
    else
        exe "tabnext"
    endif
  else
    if a:n > 0
        exe "buffer" a:n
    else
        exe "bn"
    endif
  endif
endfunction

noremap <silent> gt :<C-U>call <SID>nswitch(v:count)<CR>

for completeness, similarly use ,l to toggle 2 tabs (if there exists multiple tabs) or 2 buffers (if there is one tab)

function! s:switch()
  if tabpagenr("$") > 1
    exe "tabnext" g:lasttab
  else
    b #
  endif
endfunction
noremap <silent> ,l :call <SID>switch()<enter>

plus, to make the buffer looks even more like a tab, I installed bailey ling's vim-airline plugin:

now there is no diffs between tabs/buffers operations :)

just another (non)related question:
is there a way to display multiple rows of tabs? 
tab is visually much better than buffers, and that's quite useful when handing multiple files -- especially the time when you need multiple files displayed in front of you so you can quickly jump back and forth when you look at the names ... 
but some google research shows "there is no way to display multiple rows of tabs unless modifying the VIM source code" -- means VIM don't support this, right?

regards
ping


On Fri, Dec 13, 2013 at 4:39 AM, Paul Isambert <[hidden email]> wrote:
De: "ping song" <[hidden email]>:
> I tried these pseudo-codes, but these are just 1 time thing and won't
> change over time...couldn't figure out a good way.
>
>
> ;if more than one tab, don't hack
>
> if tabpagenr("$") > 1
> unmap gt
> unmap gT
> ;otherwise , if there is one tab, use gt,gT,Ngt to switch between
> buffers
> else
>
> if v:count == 0
> map gt :bn<cr>
> map gT :bp<cr>
> else
> map gt :<C-U>exec "b" . v:count<cr>
> endif
> endif

Do this:

    function! s:nswitch (n)
      if tabpagenr("$") >= a:n
        exe "tabnext" a:n
      else
        exe "buffer" a:n
      endif
    endfunction

    noremap <silent> gt :<C-U>call <SID>nswitch(v:count)<CR>

Now Ngt goes to tab N if it exists, to buffer N otherwise.

Best,
Paul

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/groups/opt_out.

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
 
---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/groups/opt_out.