0

Since I've started using Vim tabs (I'm a bit late to the game since they've been there since 2009!), I find I'd like most thing to default to tabs, so I always open files with vim -p file1 file2 etc. I would like :help / :h to always open in tabs also. I can do :tab help ctrl-w or :tab h ctrl-w but how can I use .vimrc to always default :h or :help to open in a new tab?

Additionally, is there a way to take an open file and push it into a tab, or to tell vim to take all open buffers and dynamically shift them all into tabs?

YorSubs
  • 3,194
  • 7
  • 37
  • 60
  • I know it's quite the opposite what you are asking, but just in case: [Why do Vim experts prefer buffers over tabs?](https://stackoverflow.com/a/26710166/10247460) – Jorengarenar Sep 14 '21 at 15:03
  • It's of interest, thanks, though I've also been reading a lot on using both buffers and tabs. Both are elegant and clean systems when using Vim I think, though I'm tending towards tabs just for the visibility of what's open always being there. – YorSubs Sep 14 '21 at 15:10
  • To move window to tab is `:h CTRL-W_T`. To open help in tab - just say 'no'. – Matt Sep 14 '21 at 15:23
  • hmm, `Ctrl-W, T` doesn't seem to do that (help says "go to top window"). I'd like to open help in tabs, much more convenient to flip between them. I get that you have a personal preference Matt, but what is your reasoning (curious to know why you are so opposed)? Do you prefer tiled windows, or do you just not use anything but buffers? – YorSubs Sep 14 '21 at 15:46
  • Everyone *always* uses buffers no matter what. Windows are an abstraction one uses *on top* of buffers and tab pages are yet another abstraction *on top* of windows. One should use the right abstraction level for the task. – romainl Sep 14 '21 at 15:51
  • Well, there are *right abstractions* and *convenience*. If I'm in a script editing it and I just want to open vim help file in a tab alongside that in another tab, what is the harm? – YorSubs Sep 14 '21 at 15:52
  • The context switching. You are in context A and you wonder how to do X so you open the doc for X in a new context B so you end up with two contexts where you can't reference anything from the other context without switching. Yes, mashing `gt` and `gT` is easy, but context switching has a much heavier toll than eyeballing to the right and to the left because you must bring that context with you at every trip. – romainl Sep 14 '21 at 16:00

2 Answers2

3
  1. "take an open file and push it into a tab"

    You can do that with this normal mode command:

    <C-w>T
    

    or this equivalent Ex command:

    :wincmd T
    

    See :help ctrl-w_t and :help :wincmd.

  2. ":h to always open in tabs"

    You only need to put the aforementioned Ex command in after/ftplugin/help.vim:

    wincmd T
    
romainl
  • 186,200
  • 21
  • 280
  • 313
  • I can get `:wincmd T` to work thanks, but not `T` for some reason (just does nothing when I try it). I will explore further on that. What does `after/ftplugin/help.vim` mean? I do not find a directory called `after`, where is that? I was thinking that someone could be put into `.vimrc` to redefine `:h / :help` so that they always open in a new tab. I'll use this `after/ftplugin/help/vim` maybe but I don't know where that is? – YorSubs Sep 14 '21 at 15:55
  • "(just does nothing when I try it)" <-- what did you try, *exactly*? "What does `after/ftplugin/help.vim` mean?" <-- It's a path to a file under your `~/.vim` directory. Create it and its hierarchy if they don't exist. "redefine `:h / :help`" <-- you can't redefine built-in Ex commands. – romainl Sep 14 '21 at 16:04
  • ok, I created `~/.vim/after/ftplugin/help.vim` and I put `wincmd T` in there. I closed vim and reopened, then I did `:h ctrl-w`. Help opens as normal, and fails to open in a new tab. For `T`, I opened 2x files in vim, so that `:ls` shows them, then in one file, I do `Ctrl-w` then `t` and nothing happens. No file is moved into a tab. Sorry if I'm missing something fundamental here. – YorSubs Sep 14 '21 at 18:02
  • @YorSubs 1) You must have `filetype plugin on` to make "ftplugin" working 2) It's `Ctrl-W` and `T`, not `t`. – Matt Sep 14 '21 at 18:08
  • You mean I just type `filetype plugin on` ? I tried Ctrl-W then T (shift-t), still nothing. It says "Already only one window" but there are 4 buffers when I type `:ls` ! I'm a bit confused how I put this buffer into a tab ... – YorSubs Sep 14 '21 at 18:53
  • Tab consists of windows, not buffers. – Matt Sep 14 '21 at 19:32
  • Gotcha, that works, thanks, I get the distinction now. I guess another way to describe what I was wondering was, if I open 5 files by doing `vim *` or some other way, that I end up with 5 buffers, then I want to spray them out to 5 tabs, or indeed, the reverse, to compress all tabs into one window with 5 buffers? – YorSubs Sep 14 '21 at 19:48
  • 1
    For the the former, see `:help :argdo` and `:help :tab`. For the latter, since buffers are global, see `:help :tabdo` and `:help :close`. – romainl Sep 14 '21 at 21:00
1

You can use autocmd to always open help in a new tab

" This won't work if you open the same help file more than once
autocmd FileType help wincmd T

" This works
autocmd BufEnter *.txt if &filetype == 'help' | wincmd T | endif

Or you can define a new command to only open it in a new tab, when in a small screen or there are many to read, for example. With the following, you can use :Tabhelp ....

function! Tabhelp(arg)
  execute 'tab help '.a:arg 
endfunction

command -nargs=? -complete=help Tabhelp call Tabhelp(<q-args>)
leaf
  • 1,624
  • 11
  • 16