Open multiple help windows in the same tab page - vim

When I have a help window open in vim, and I then search another topic in the help, I jump to the new topic (and away from the original one) in the original help window.
Is it possible to simply open a second help window, preferably in a new window split?
I'm used to opening multiple tabs when trouble shooting in my browser, and I feel limited when browsing Vim's help. This is especially the case when I'm reading the help, and then stumble across some vim feature that I wasn't looking for, but sounds interesting/useful.
Of course I've been looking in vim's help documentation for the answer, but come up short...

You can prepend :tab to commands that split the window to force the new window to be opened in a new tab page:
:tab help :tab
That said, every jump to a new tag in the :help is stored in a :help tagstack so it is easy to backtrack with <C-t>.
Of course I've been looking in vim's help documentation for the answer, but come up short...
The feature is introduced in chapter 8 of the user manual:
You can put ":tab" before any Ex command that opens a window. The window will be opened in a new tab page. Another example:
:tab help gt
Will show the help text for "gt" in a new tab page.

Based on the answer from #romainl, coupled with the plugin Tabmerge, I've got a function that can open multiple help windows in the same tab page:
function! NewHelpSplit(subject)
let current_tabpage = string(tabpagenr())
" open a help page in a new tab
:execute ':tab :help ' a:subject
" merge that tab as a split in current tab (bottom, means the original tab
" content will be on the bottom, and therefore the help will be on the top)
:execute ':Tabmerge ' current_tabpage ' bottom'
endfunction
Defining a new command:
:command -nargs=1 NHelp :call NewHelpSplit("<args>")
So to use it to open a new help window on the subject of tabs, even if you already have a help window open, do:
:NHelp tabs

Related

Can I open a new tab to left of the current tab?

Issuing :tabnew somefile will open somefile in a new tab to the right of the current tab. Can I somehow get Vim to open a tab to the left of the current tab?
Update: The suggested answers do allow me to open a new tab left, but they break file name auto completion, which is a no-go.
Since Vim 7.4.530 (2014), you can use negative values for [count] in :[count]tabnew to open tabs. To open a tab directly to the left of the current tab, use:
:-1tabnew
Documentation: https://vimhelp.appspot.com/tabpage.txt.html#:tabnew
:[count]tabe[dit] :tabe :tabedit :tabnew
:[count]tabnew
Open a new tab page with an empty window, after the current
tab page. If [count] is given the new tab page appears after
the tab page [count] otherwise the new tab page will appear
after the current one.
:tabnew " opens tabpage after the current one
:.tabnew " as above
:+tabnew " opens tabpage after the next tab page
" note: it is one further than :tabnew
:-tabnew " opens tabpage before the current one
:0tabnew " opens tabpage before the first one
:$tabnew " opens tabpage after the last one
Similar functionality is also available for :tabclose, :tabonly, :tabmove, see the commit linked above. If this does not work, use :version to check if your Vim is up-to-date and/or use :help tabnew to check whether the documentation looks like the one cited here.
To utilize the behavior #romainl described without having to resort to knowing current tab page number use the following command:
command -nargs=* -bar Tabnew :execute (tabpagenr()-1).'tabnew '.<q-args>
. Note: it is perfectly save to use 0tabnew: this does what intended and makes new tab the first one, even though there is no tab page that has number below 1.
If you are sure you never use this command with ++opt or +cmd you can use -complete=file just after -bar. Note: besides its name it is not a completion option because it as well does filename expansion (and shows errors in case -nargs=1 and globs expanded in too many filenames). Unfortunately this behavior is not even mentioned in documentation.
You can use a [count]. Supposing you are at tab #4, :3tabnew creates a new tab on the left of the current tab.
Keep in mind, though, that tabs are always created to the right of the current tab or tab #[count]. :3tabnew effectively means "create a new tab after tab #3".
You can write your own command to do this
:command -nargs=1 TabnewBefore exec "tabnew <args>" | exec "tabmove -1"
Then to use it
:TabnewBefore somefile
If you want it to be the default 'tabnew' bahaviour you can do
:ca tabne TabnewBefore
Now if you type tabne and press space on the command line it does what you want, if you want the original behaviour type the full command tabnew
You can put these definitions into your .vimrc file for future use

How can I maximize a split window?

Invoking :help in Vim, I got the help manual page with split window. I want to maximize the help manual window and close the other window.
How can I do this? What is the Vim command to do this?
You can employ Ctrl+WT (that's a capital T) to move any open window to its own tab.
As mentioned by others Ctrl+W_ / Ctrl+W| to maximize within the current tab/window layout (while respecting min height/width settings for various other windows).
(Ctrl+W= resizes all windows to equal size, respecting the minimum height/width settings)
Edit To the comment
start vim (e.g. gvim /tmp/test.cpp)
invoke help :help various-motions - opens a split window
move help into separate tab maximized: C-wT
enjoy reading the fine manual :)
move the help back into the original tab:
mAZZ<C-w>S`A
mA: set global mark A
ZZ: close help buffer/tab
C-wS: split original window
`A: jump to saved mark A
You can avoid using a mark for normal (non-help) buffers. Let me know if you're interested.
With :help [topic] you open up a topic that interests you.
Ctrl-Wo will minimize the other windows (leaving only the help window open/maximized).
(Ctrl-Wo means holding Ctrl press W, and then o)
You can expand a window to its maximum size using Ctrl+W_ (underscore). The final size of the expanded window will be constrained by the value of the winminheight option. When you close the help window, your previous window(s) will be restored to their former sizes.
I prefer to use tabs for that. Use
:tabedit %
to open a file maximized in a new tab, once you are done return to the old setup with all windows using
:tabclose
I find this the ideal solution as this works together with :cw and the Tagbar plugin. Taken from: vim.wikia
I like to use 'M' to maximize and 'm' to minimize.
It won't look great as it'll shrink all the other open windows that are in the same buffer, but I found it to be more useful when dealing with tabs. So for instance, instead of opening a new tab for that file then having to close it after you're done with it or want to minimize it.
nnoremap <C-W>M <C-W>\| <C-W>_
nnoremap <C-W>m <C-W>=
The reason for nnoremap is that I don't care about recursive mapping, but just map should also work.
Install the plugin vim-maximizer, then you can maximize the current window and restore with F3
You can get help window in full size without closing/resizing any other windows by using
tab help {topic}
This will open help window in a new tab, other windows will be left as-is (really resized so that tabline can be shown, but this is only one additional line above). You can close help as usual: at least :bw<CR> and <C-w>c work for me closing new tab as well.
Plugin ZoomWin
by Charles Campbell
This plugin remaps
Ctrl-w o
to do both: maximize and restore previous layout.
This plugin can be downloaded from two locations
https://www.vim.org/scripts/script.php?script_id=508 (v24)
http://www.drchip.org/astronaut/vim/index.html#ZOOMWIN (v25)
To get just the help up, then close the other window, do this: :helpCTRL-WCTRL-W:close. Then you'll have just the help up.
Somehow the ZoomWin plugin did not work at all for me, I now see there are other plugins but I already wrote this and gives me the exact effect I wanted (with a minor quirk detailed below):
function! ToggleZoom(zoom)
if exists("t:restore_zoom") && (a:zoom == v:true || t:restore_zoom.win != winnr())
exec t:restore_zoom.cmd
unlet t:restore_zoom
elseif a:zoom
let t:restore_zoom = { 'win': winnr(), 'cmd': winrestcmd() }
exec "normal \<C-W>\|\<C-W>_"
endif
endfunction
augroup restorezoom
au WinEnter * silent! :call ToggleZoom(v:false)
augroup END
nnoremap <silent> <Leader>+ :call ToggleZoom(v:true)<CR>
Use the mapped key (Leader and + in my case) to toggle between maximized / previous layout. If you change to another split in the same tab, maximization turns off.
If you change tabs, the split stays maximized, although somehow it won't cover the complete full width anymore, with the width minimized windows gaining back some 4 columns or something. Anyway it works acceptably for me even with that minor quirk.
edit: somehow it works fine now, must've messed up in some way before.
in your .vimrc, just place
nmap - :res<CR>:vertical res<CR>$
When you want maximize current window, just press - in command mode. Press = when you want to restore the last status of multiple window

Using :Vexplore effectively

So, the :Vexplore command in vim opens a nice little directory browser to the left.
But how do I then open a file from that side-pane into the main window on the right?
One would assume there's a simple mapping for it, but I can't seem to find it.
I think you want o or P
Also, have a look at the documentation, e.g.
:he netrw-p11
It turns out it's just a single line in .vimrc:
let g:netrw_browse_split=4 " Open file in previous buffer
Source: http://vimcasts.org/episodes/the-file-explorer/#comment-45366660
I'm sure what you're looking for is this:
:Vexplore!
This is the same command you would use to navigate to different windows in Vim (like quickfix window, or different split), everything is explained in
:help windows.txt
But to answer to your question directly:
CTRLwCTRLh to move to the left window
CTRLwCTRLl to move to the right window
then Enter to select the file you want to open.
You might want to read :help netrw as well
The latest netrw plugin (up to v153f) now provides the :Lexplore command, which opens an explorer on the right hand side of the vim display. It sets g:netrw_chgwin so edits occur in the window to the right of the netrw window. You can get it from http://www.drchip.org/astronaut/vim/index.html#NETRW .

How can I open a help file in Vim on a new buffer in an existing window?

I often take a look at help files in Vim, but sometimes I want to read one in full screen. Since the :help command opens it in a new window, and closing the old window, if it was the only one besides of the help file, for some reason closes Vim, the only way I found of doing this was opening the help file, and then reopening it in a new tab.
I wondered, is there any way to make the :help command (or another command) to open a help file in the same window, but a new buffer?
You might be looking for :only or CTRL-W o (the same command). This makes the current window the only one on the screen. All other windows are closed.
You can also vertically split the help window with:
:vert help {subject}
BTW, :help actually does open in a new buffer, it's just "unlisted". To list all buffers, including the unlisted ones:
:buffers!
If I understand the question correctly, all you need to do is to chain the help command call with the only command:
:help <subject> | only
The :help will usually open a new window unless the active window's buffer buftype is already help. So to truly reuse a window you must open a new empty buffer in that window with :enew, change the buftype with :set buftype=help and then issue the :help <whatever>.
For convenience you could define a command to do that in your .vimrc:
command! -nargs=1 -complete=help H :enew | :set buftype=help | :h <args>
And then use :H {subject} from any window.
Using this method you truly reuse the window and that allows you to use C-^ to go to the alternate for example. It will also respect your window layout (split windows, etc) unlike the other answers.
You can open a new tab for help with :tab help. This will give you a full screen help. Also look at :help :tab.
You can use :help to open the help window, then Ctrl+W_ to make that window full screen (mostly, see the winminheight option).
To open full size new tab with your desired topic:
:tab help {subject}
:tab h {subject}
Subject is any valid :help argument.
To split the current window:
:vert help {subject}
:vert h {subject}
A more general variant of #Shamaoke's answer is to open the main help menu in a full window.
:help | only
I wrote a custom command using capital H like so (works exactly like :h except that it uses the whole window):
command! -nargs=1 -complete=help H call HelpFullScreen( <f-args> )
function! HelpFullScreen( topic )
exe "h " . a:topic
wincmd j
try
clo
catch /^Vim(\a\+):E444:/ " can't close last window
endtry
endfunction
Works like a charm!

How to jump back to NERDTree from file in tab?

I usually:
Choose the needed file.
Open it in a tab(t character, by default).
But how I can jump back to NERDTree to open one more file in a tab?
Temporary solution I use now in my .vimrc file:
map <F10> :NERDTree /path/to/root/of/my/project
But it's not very useful to start navigation again and again from the root directory.
Ctrl-ww
This will move between open windows (so you could hop between the NERDTree window, the file you are editing and the help window, for example... just hold down Ctrl and press w twice).
Ctrl+ww cycle though all windows
Ctrl+wh takes you left a window
Ctrl+wj takes you down a window
Ctrl+wk takes you up a window
Ctrl+wl takes you right a window
NERDTree opens up in another window. That split view you're seeing? They're called windows in vim parlance. All the window commands start with CTRL-W. To move from adjacent windows that are left and right of one another, you can change focus to the window to the left of your current window with CTRL-w h, and move focus to the right with CTRL-w l. Likewise, CTRL-w j and CTRL-w k will move you between horizontally split windows (i.e., one window is above the other). There's a lot more you can do with windows as described here.
You can also use the :NERDTreeToggle command to make your tree open and close. I usually bind that do t.
If you use T instead of t there is no need to jump back because the new tab will be opened, but vim's focus will simply remain within NERDTree.
You can focus on a split window using # ctrl-ww.
for example, pressing:
1 ctrl-ww
would focus on the first window, usually being NERDTree.
Since it's not mentioned and it's really helpful:
ctrl-wp
which I memorize as go to the previously selected window.
It works as a there and back command. After having opened a new file from the tree in a new window press ctrl-wp to switch back to the NERDTree and use it again to return to your previous window.
PS: it is worth to mention that ctrl-wp is actually documented as go to the preview window (see: :help preview-window and :help ctrl-w).
It is also the only keystroke which works to switch inside and explore the COC preview documentation window.
ctrl-ww Could be useful when you have limited tabs open. But could get annoying when you have too many tabs open.
I type in :NERDTree again to get the focus back on NERDTree tab instantly wherever my cursor's focus is. Hope that helps
The top answers here mention using T to open a file in a new tab silently, or Ctrl+WW to hop back to nerd-tree window after file is opened normally.
IF WORKING WITH BUFFERS: use go to open a file in a new buffer, silently, meaning your focus will remain on nerd-tree.
Use this to open multiple files fast :)
You can change the tabs by ctrl-pgup and ctrl-pgdown. On that tab you came from the NERDTree is still selected and you can open another tab.
In more recent versions of NERDTree you can use the command :NERDTreeFocus, which will move focus to the NERDTree window.
gt = next Tap
gT = previous Tab
if you want you can enable the mouse support editing ~/.vimrc file.
put set mouse=a
after that you enable files click in NERDTree.
All The Shortcuts And Functionality is At
press CTRL-?

Resources