How to auto-close vim's initial buffer on file open? - vim

So I'm using ctrlp and nerdtree. I found it troublesome whenever I enter vim, open a file by ctrlp or nerdtree, my cursor always jump to a newly opened window while the initial empty buffer window is still opened, occupying the screen. Then I always have to do <C-w>j <C-w>q -- while I've trained myself to do it with my mechanical memory, I guess there should be some smart ways to auto-close the empty window once any file is firstly opened.
Appreciate all help/tip provided in advance. This is my current .vimrc.
Update:
Thanks #romainl and for NERDTree I've no issue now. I'm checking how to configure ctrlp though.

Pressing enter will open the file in the same window, as #romainl said.
However, another tip: If you want to see only the current window, you can run
:only
which will hide all but the current split (buffers will be hidden)
I have remapped it to <leader>o
" maximize current visible window
nnoremap <Leader>o :only<cr>

Related

Set default window in Vim for opening all files/buffers

Is it possible in Vim to set a default window for opening all files and buffers there?
Consider the following scenario:
I'm using Netrw and I'm positioned into the window that has the file list. Then I use :buffer or the wildmenu to open a file and it opens right there. I want the file to be opened in a default window, no matter where I'm positioned.
Thanks in advance.
I'm not sure if that's what you are looking for but you can use the :Lexplore command. It opens netrw in a vertical split in the current directory (:pwd). You can then open a file in the other vertical split. I haven't used it much, so I don't know it's exact behavior when you have multiple splits. You can open an additional a vertical split by using v in the netrw buffer.
I have the following mapping which opens netrw in a small vertical split:
nnoremap <silent> <leader>le :Lexplore<bar>vertical resize 30<CR>

Netrw open files into tabs in opposite vertical window

Imagine I have :Vex after starting vim. I want to be able to press t and have the tabs appended to the opposite window rather than the Netrw window. Is this possible?
If I press P I can open the file into the split window but I would like to be able to tab through the files in the vertical split whilst having my Netrw window visible - just like Sublime or Komodo.
Possible?
And yes, I've scoured :h netrw!
Almost got it with some .vimrc remaps and options.
It looks like this http://i.imgur.com/rUf19SF.png
Usage:
run vim.
hit shift enter. this will open netrw as a sidebar (a small split window to the right) and focus it.
browse as usual and hit enter to open a file. this will open it in the left window by default and focus it.
hit control-w control-w. this will focus netrw again.
browse as usual but this time hit control-enter to open a file. this will open it in a new tab that also contains the netrw sidebar.
The .vimrc config:
" netrw magic
" enable mouse usage. makes it easier to browse multiple tabs
set mouse=a
" hide netrw top message
let g:netrw_banner=0
" tree listing by default
let g:netrw_liststyle=3
" hide vim swap files
let g:netrw_list_hide='.*\.swp$'
" open files in left window by default
let g:netrw_chgwin=1
" remap shift-enter to fire up the sidebar
nnoremap <silent> <S-CR> :rightbelow 20vs<CR>:e .<CR>
" the same remap as above - may be necessary in some distros
nnoremap <silent> <C-M> :rightbelow 20vs<CR>:e .<CR>
" remap control-enter to open files in new tab
nmap <silent> <C-CR> t :rightbelow 20vs<CR>:e .<CR>:wincmd h<CR>
" the same remap as above - may be necessary in some distros
nmap <silent> <NL> t :rightbelow 20vs<CR>:e .<CR>:wincmd h<CR>
Caveats:
The netrw "sidebar" in each tab is independent, meaning the current directory in a tab may not be the same in another tab. Suggestions? Thought of using the netrw buffer in every "sidebar" window, but netrw uses a new buffer whenever changing directories.
You seem to be confusing Vim's "tabs" with the "tabs" you can find in virtually every other program.
Unlike other implementations, Vim's tabs are not tied to a buffer. In Vim, "tabs" behave like what you would call "workspaces": they are meant to keep together one or more windows. Those windows could display any buffer and you can very well end up with the same buffer displayed in multiple windows in multiple tabs!
With that in mind, it would be very wrong to use them like you want. "Tabs" don't represent files and jumping to another "tab" is not equivalent to jumping to another file at all.
The window created by :Vex is a normal window. Like all the other windows, it is contained in a "tab" and can't live outside of a "tab". And you can't have "tabs" inside of windows.
So, basically, what you ask is impossible.
If you are on a Mac and this "other-editor-like feature" is really important for you (more important than, say, embrace the Vim way), you could try this MacVim fork that adds a "regular" file explorer outside of the buffer/window/tab trio. You could also try PIDA which tries to build an IDE around Vim; including a separate file explorer.
As romainl said, tabs are not files (or buffers). So, if I re-interpret your question to mean: "I want to press t and have files appear in the opposite window...". Then I suggest reading :help netrw-C. If you really do mean "append" and not "appear", then that's more involved and before I expend the effort to figure out how to do so I'd like to know that that's what you really meant. The latest netrw (as of today, that's v153f) has additional options which are mentioned in that help reference I gave above.

mvim closing last tab want not to close window

I am just getting start with mvim coming from e-texteditor on Windows. One little annoyance (for me) is that when I use the cmd+w to close tabs, if the last tab is closed the window of mvim is also closed, is there any chance that I can keep that window open with one empty tab in it?
In your ~/.gvimrc file (not your .vimrc; and create one if it doesn't exist already), add:
macm File.Close key=<nop>
nnoremap <silent> <D-w> <Esc>:bd<CR>
The first line unmaps the <D-w> menu binding so you can remap it. The second maps <D-w> to close each vim-window one at a time, which will close the tab if it is the last vim-window in the tab, and will leave the OS window open on the last remaining tabpage. The caveat is that it will also cycle through all hidden buffers in that last window, closing each in turn (though you could use <D-S-w> at this point to close the OS window at once). c.f. :help :bd. If you don't desire this behavior, you could use this instead:
nnoremap <silent> <D-w> <Esc>:tabclose<CR>
Which is more strictly what you were asking for. c.f. :help :tabclose
Duplicate the mapping replacing nnoremap with inoremap if you want it to work in insert mode, too.
You might want to look at this script. It does something similar to what you have mentioned. Using Araxia's mapping for Command-W you can replace the scripts commands to Cmd-w.

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

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