I want to set up some mapping that:

  • if there are multiple open tabs, then goes to the next tab, like :gt
  • otherwise it cycles through the open buffers, like :bn

How do I go about doing this?


1 Answer 1


You could use expression mappings:

nnoremap <expr> YOUR_KEY len(gettabinfo()) > 1 ? 'gt' : ':bn<CR>'
nnoremap <expr> YOUR_KEY len(gettabinfo()) > 1 ? 'gT' : ':bN<CR>'

<expr> says to interpret the mapping as an expression. gettabinfo() returns the list of the open tabs so if the length of this list is greater than one you know you have more than one tab. Depending on the result you want your mapping to execute either the tab command or the buffers one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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