Vim close window without closing buffer - vim

How do I close a window or unsplit without deleting the buffer?

A window is a viewport on a buffer. In vim to manage windows it is CTRL+w the leading command, that you can follow with several options (in bold those that answer to your question):
CTRL+w, v: Opens a new vertical split
CTRL+w, s: Opens a new horizontal split
CTRL+w, c: Closes a window but keeps the buffer
CTRL+w, o: Closes other windows, keeps the active window only
CTRL+w, right arrow: Moves the cursor to the window on the right
CTRL+w, r: Moves the current window to the right
CTRL+w, =: Makes all splits equal size
Then, you need to switch the buffers in the windows:
:ls lists all opened buffers
:b5 switches to your 5th buffer
Finally, to open all buffers in a vertical split, use: :vertical sball.
Very useful when you open multiple files as buffers after a grep:
grep -rno --exclude-dir={dir1,dir2,dir3} "searchterm" *
vim $(!! -l)
For more info, see the doc: vimdoc.sourceforge.net

If you want to edit another buffer in the window, just use :edit / :buf. The current buffer will be replaced, and remains in the buffer list (i.e. it shows up in :ls).
If you want to close the windows split, use :close. The :quit command will work, too, but has the side effect of closing Vim when this is the last window.
In order to leave buffers that have changes, you need
:set hidden
If you know how Vim deals with buffers, this is a recommended option that many users have set.

Vim windows are closed using :q.
However, if you don't have another window open, it will exit from Vim. If you do have another window to switch to, only the current window is closed, and buffer remains open. You may need to set hidden.
To close a buffer, you would need to do :bdelete.
You can check if your buffer is open or not by using :buffers.

Related

How to open a vim terminal in the full area of the vim buffer?

I really like the built-in :terminal in recent vim versions. I have one pain point though: sometimes (often?), I would like the terminal to take the full vim window size. However, usually, using :terminal only uses a partial split on half the vim window size, even if I start from an empty buffer. From an empty buffer, using ctrl-w o maximizes.
Any way to open at once into a "full size split" that takes the full vim buffer, instead of needing the extra ctrl-w o step?
You can do :tab terminal to open a terminal window in a new tab page.
You can do :terminal ++curwin to open a terminal in the current window.

How to stop vim from splitting horizontally when opening a new file?

Usually when I open a file in Vim when there's already one open Vim opens the file in a whole new... buffer? Screen thing? In other words, it takes up the full vim client area so that I have to :bn through to see the next file, which I prefer. But sometimes when I open a file Vim decides to split the window horizontally and put it in one of those horizontal buffer things.
When does it decide to split the window as opposed to giving me the whole thing? How can I prevent that behavior? And perhaps most important, what is the name for that kind of behavior so I can do better searches in the future?
Quoting from :help window:
A buffer is an in-memory text of a file
A window is a viewport of a buffer
Buffers are enumerated. When you run :bufnext or :bn, you go to the next buffer; you can also use :b<num> to go to a specific buffer, say :b2 to go to buffer 2. You can get the list of buffers with :ls.
When you :split or :vsplit, you open a new window, i.e. a new viewport for a buffer. You can have two windows show the same buffer, and so on.
Also, a tabpage is a collection of buffers; open a new tab with :tabnew and cycle through them with gt and gT to go forward and backward respectively.
I highly recommend you read the helpfile; it's all there.

vim:have multiple windows without splitting?

is there any way I can have multiple windows in a terminal based vim, without splitting the window? so I could just switch to the other file by hitting a key ?
if "window" you meant is vim-window, the answer is no.
In vim, a window is a viewport onto a buffer. You can use multiple windows on one
buffer, or several windows on different buffers.
So window is viewport, if you there is no splitting you are watching on single window, no multiple.
I guess what you meant is buffer: A buffer is a file loaded into memory for editing. The original file remains unchanged until you write the buffer to the file.
You can have one window (without splitting) and 10 files(buffers). You could :ls to see the buffer list, and also by command to switch among those buffers/files. display them in single window.
you can get more info about switching files/buffers by:
:h buffer
You could use tabs.
To open a new tab you can use :tabe <file>. After that you can use gt or gT to move to the next or previous tab.
If you are opening a bunch of files from the command line you can use vim -p <files> to open them all in tabs.
Take a look at :h tab-page-commands

in VIM, How to set the initial window size via file browser?

I'm using vim to browse through different folders. (i.e., issue :tabe . command in vim)
However, when I open the file either in split window (pressing 'o') or in vertical split window(pressing 'v'), the newly opened window size is really small. (while meanwhile, vim's file browser's windows stays pretty big which I don't really need).
I know that I can manually change the window size by Ctrl+W with either -/+ or split, or for vertical split, or '=' to make the window sizes equal; but that's too troublesome.
I want to check if there're existing ways to set the default size of the window opened using "o" or "v" to be bigger?
Thanks a lot ;)
The new window should take half of the height or half of the width of the current window which can give you small windows pretty quickly.
The option that defines that proportion can be found in :help netrw:
let g:netrw_winsize = 75
Note that using this method will make any further split even smaller than before:
let g:netrw_winsize = 50 (default)
|-netrw--------------------------------------------|
|-netrw------------------|-file--------------------|
|-netrw------|-file------|-file--------------------|
let g:netrw_winsize = 75
|-netrw--------------------------------------------|
|-netrw------|-file--------------------------------|
|-netrw|-file|-file--------------------------------|
The "problem", here is that netrw splits its own window, not the previous window.
Netrw can be used to open the file under the cursor in the current window, in a split window or in another tab but the way it splits its own window makes it hard to use it the way you want. AFAIK, the most common usage is:
open netrw with :Ex,
navigate,
hit <CR> to open the file under the cursor in the current window,
edit,
re-open netrw in its latest state with :Rex (for Peter Rincker),
GOTO 2
An alternative is to use :Vex to open netrw in a vertical split and use P to open the file in the previous window.
I'm afraid Netrw is not really designed to work like what you seem to want it to work. IMO, netrw is more like an "open…" dialog than the kind of file explorer pane you can see in most editors/IDEs. I'd suggest you either get used to it or try NERDTree which has only a subset of netrw's features but is designed to be more like those file explorer panes.
Actually the split size is not relative to netrw, it's the default size vim sets for newly created splits, so if you want to resize the actual split which is in you case the navigator (netrw) you can use this commend:
:vertical resize 30
There is :Lex now to open a left explorer. You can place let g:netrw_winsize=30 in your vimrc file to keep the explorer small.

VIM: How can i open a file at right side as vsplit from a left side NerdTree panel?

I installed NerdTree plugin.
How can i open a file at right side as vsplit from a left side NerdTree panel ?
To make vsplit put the new buffer on the right of the current buffer:
set splitright
Similarly, to make split put the new buffer below the current buffer:
set splitbelow
I haven't tried this with NerdTree, however.
There's a s command, but it opens a file split to the left of current buffer. Though you can press Ctrl+W r to swap windows then.
This is a bit of a hack, but how I do it is this:
Put cursor in window I want to open file into
Hit <leader>n<leader>n (this closes NERDtree and then opens it again with the cursor in NERDtree)
Select the file
On my system this opens it on the last window I was just on if the file isn't already open on my screen.
Start in the window you want to open your file into.
Hit <leader>n<leader>n to close and reopen NERDTree
Select the file you want!
A quick C-w = will get your windows back to proper proportions.
Not sure if anyone else is still struggling with this, but here's how I dealt with it.
You can use
:ls
to list the available buffers. Which would look something like:
1 "foo.txt"
2 "blame_the_user.java"
:b1 to select foo.txt
:b2 for blame_the_user.java
This method can be done from any window setup using :sp or :vs.
Open the buffers you want to split first.

Resources