How can I maximize a split window? - vim

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

Related

Preventing Vim preview window from moving main

Is there an autocmd for when the preview window is opened/closed?
I want to scroll the main window n lines up when it the preview window is opened, then n lines down when it is closed, to counteract the "moving text" effect that occurs natively.
Am I able to do this with the relevant autocmd (and what is it), or is there a better way for me to achieve this?
There is no such autocmd event. But you can use WinEnter and BufDelete associated with previewwindow option to achieve something similar.
Using WinEnter you can check previewwindow; if you are on preview window, you can set a buffer variable to differ this event from subsequent events that can be generated by moving to another window and back to preview window. You can also set au BufDelete <buffer> call MyRestoreMainWindow() to call your function when preview window is closed.
I see this question asked often and always scratch my head wondering what is that window-shifting people talk about that I don't experience.
Well, today it occurred to me that two options that I've added to my ~/.vimrc a long time ago have the pleasant side effect of preventing that dreaded window-shifting:
set splitbelow
set splitright
Give it a try!
I was actually wondering the same thing except with the tab bar -- how to prevent that annoying shift from occuring when the tab bar is shown or hidden. Have you considered a wrapper function? The following seems to work for the ps example (it will still cause a shift if the preview window would obscure the cursor)
se splitbelow splitright
fun! PsWrapper(text)
let view=winsaveview()
exe 'ps' a:text
call winrestview(view)
endfun
While we're here ... the tab bar case seems to require some black magic. Ie, as someone pointed out, the tabbar will cause the text to scroll down if the cursor is above the middle line (??). But this seems to work - to always show a tab bar:
let [view,g:stal]=[winsaveview(),&stal]
let [view.topline,&stal]=[view.topline+!g:stal,2]
call winrestview(view)
and to restore the original tabbar setting
let [view.topline,&stal]=[view.topline-!g:stal,g:stal]
call winrestview(view)
You can't really do this with a simple autocmd - Using the WinEnter/WinLeave/BufEnter/BufLeave auto commands all have minor quirks (stated in the vim documentation) so they won't consistently solve your problem completely.
If this happens to you when opening splits, then you can solve this like #romainl suggested, by defining in your .vimrc :
set splitright
set splitbelow
BUT... This will still happen when opening various 'preview' windows, or using the quickfix or location list windows vim has to offer. I use them a lot, and this problem really annoyed me, so I wrote a plugin to solve this.
You can check it out here: https://github.com/gillyb/stable-windows
It works by maintaining state of the cursor position and top line number of the windows open in your vim layout, and restoring them each time you switch to a different buffer.
It's relatively new (as of writing this answer) so if you find any bugs feel free to open an issue, and I will try to address them quickly.
Hope this helps! :)

How can I temporarily make the window I'm working on to be fullscreen in vim?

I use vim, and usually have more than one vertical/horizental window open, usually editing c++ header files alongside cpp files. How can I temporarily make the window I'm working on to be fullscreen, edit what I want, and then exit fullscreen?
By fullscreen I mean to fit vim window only, and not my total display screen.
Ctrl+W_ will maximize a window vertically.
Ctrl+W| will maximize a window horizontally.
So far as I'm aware, there is no way to restore the previous layout after these actions, but Ctrl+W= will resize all windows to equal sizes.
An option could be to pursue the editing in a new tab. The following command opens the active buffer into a new tab allowing you to see the buffer in the hole vim window.
:tab split
And close the tab when you're done:
:tabc
Edit:
You can always use the following command to use tt as a shortcut (or better add it to your .vimrc):
:noremap tt :tab split<CR>
and close is when you're done :
:wq
If I understand what you're asking, I think you'll find the ZoomWin plugin helpful (GitHub). If you've got a bunch of split windows, and you want to temporarily make the current window the only visible one, you can hit <C-w>o. When you want to revert to the previous split state, hit <C-w>o again.
[Edit] Note on key mappings:
The default key mapping for this plugin is <C-w>o, but that conflicts with a default Vim key mapping. By default, that does :only, which makes the current window the only window. If you'd like to retain that functionality, you can remap ZoomWin to another key. I remap it to <C-w>w, because I like to use the :only option as well. Here's my mapping:
nnoremap <silent> <C-w>w :ZoomWin<CR>
Note that this also overrides a default Vim mapping, related to moving to other visible windows (:help CTRL-W_w), but I never used that one anyway.
Use Ctrl w_ to maximize the current window vertically.
These are some useful commands that help work with windows:
:e filename - edit another file
:split filename - split window and load another file
ctrl-w up arrow - move cursor up a window
ctrl-w ctrl-w - move cursor to another window (cycle)
ctrl-w= - make all equal size
10 ctrl-w+ - increase window size by 10 lines
:vsplit file - vertical split
:sview file - same as split, but readonly
:hide - close current window
:only - keep only this window open
:ls - show current buffers
:b 2 - open buffer #2 in this window
I've tried ZoomWin and a few others. The problem is, they all destroy and try to re-create the windows. This is especially problematic with custom plugins like NERDTree, Tagbar and a few others. Icons and fonts are not drawn properly, sizes are messed up etc..
zoomwintab.vim is a simple zoom window plugin that uses vim's tabs feature to zoom into a window inspired by ZoomWin plugin but in a non-destructive manner.
https://github.com/troydm/zoomwintab.vim
I use Tmux, so I mapped it to <leader> z to stay in sync with tmux's <prefix> z
nnoremap <leader>z :ZoomWinTabToggle<CR>
An awesome plugin for toggling windows fullscreen is vim-maximizer.
After it's installed you can simply use <F3> (default shortcut) to toggle fullscreen on the window.
You can also customize the shortcut keys, for example if you wanted to use <C-w> z (similar to tmux shortcut):
nnoremap <silent><C-w>z :MaximizerToggle<CR>
vnoremap <silent><C-w>z :MaximizerToggle<CR>gv
inoremap <silent><C-w>z <C-o>:MaximizerToggle<CR>
Somehow the ZoomWin plugin did not work at all for me, my experience was kind of what arithran says. I couldn't find other plugins so I wrote this:
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>
It creates the effect. You 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.
Manoj somewhat answered with a lot more useful info, but as a first step this is what works for me:
:hide: Hide the current buffer - with a two-buffer split, this makes the other buffer full screen. Unlike :only, this command does not close unmodified buffers so you can unhide them.
:unhide: Re-create splits for each open buffer
The unhide command does not restore the previous layout, so you will have to manually rearrange your windows if needed ex. using one of these for simple vertical/horizontal splits (this is CTRL-W followed by the uppercase navigation letter - release CTRL-W before entering the letter):
CTRL-W SHIFT-H: Move window to the far left
CTRL-W SHIFT-J: Move window to the very bottom
CTRL-W SHIFT-K: Move window to the very top
CTRL-W SHIFT-L: Move window to the far right
These are the same keys used for navigation, without SHIFT (lowercase navigation letters):
CTRL-W H: Move focus to the left window
CTRL-W J: Move focus to the bottom window
CTRL-W K: Move focus to the top window
CTRL-W L: Move focus to the right window
The H, J, K and K keys alone move the cursor - arrow keys on the keyboard may work as well for navigation.

Is it possible to restore windows status after maximum window?

I always have this problem. When a windows seems too small to view code, i will type :only the maximum this window and hide other windows. But when the editing is finished i quit want to restore the previous status of windows. Exactly the same before i maximum one window. It there any plugin to do this job? Or it's build-in in vim?
EDIT: I found a plugin called ZoomWin can actually do this job. But 0 can't been map to :ZoomWin in my vim. Still don't know why. The help file says i can use 0 to call Zoomvim just after i installing this plugin.
You could probably write a script using mkview and loadview if you wanted to keep this all in one tab, however, this is the exact thing that vim's tabs were made for. I suggest using the following mapping to map \0 to open the current buffer in a new tab. To close the tab just do :q as you would normally do and you'll go back to your previous tab which contains the window layout you want.
:nnoremap <leader>0 :tabedit %<cr>
Note that this mapping uses <leader> so if you've changed your mapleader then the sequence will not be \0.

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 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