Prevent :bd from closing window in VIM - vim

I'm trying to get using to working in VIM with split windows.
So.... I split my VIM into two windows using ctrl V S, and I switch between windows using ctrl W W.
I'm using to typing :bd to delete the current buffer. Unfortunately, that has a side effect of closing the current window.
Is there a way to delete the buffer without closing current window ?

You can achieve this with a custom command, often named :Kwbd (keep window, buffer delete). There is an entire discussion and several alternatives on the Vim Tips Wiki.
See also this plugin on vim.org.

Related

Gvim: Move tab to new window

The opposite question seems to be asked a lot: how to move a window into a new tab in an existing window. What I'm hoping is that a tab that I have open in gvim can be moved out into its own window or into another existing window.
Is this possible?
Thanks!
Same Vim instance
If that tab shows just a single window, you just have to note its buffer number (e.g. via :ls or :echo bufnr(''), or by including it in the statusline), and then close the tab via :close (:set hidden helps with modified buffers), then going to the target tab / window, and re-opening the buffer there via :buf N or :sbuf N.
If you need to support multiple windows in a tab page, you'd have to write a custom command / mapping that first remembers the buffers, and then applies the above steps for all of them.
Different Vim instances
Edit: The above is for movement within a single Vim instance. If you want to move a buffer to another GVIM instance, you first have to :bdelete it in the current Vim, to avoid swap file messages. Launching in new instances is easy:
:execute 'bdelete | !start gvim' shellescape(expand('%:p'), 1)
This passes the (full absolute) path of the current file to a fresh GVIM.
To move a file to an existing GVIM (you need to know its v:servername), you need to use the remote client-server communication (:help remote.txt), e.g. by sending a similar :drop command via remote_send(), like this:
:execute 'bdelete | call remote_send("GVIM1", ":drop " . ' . string(fnameescape(expand('%:p'))) . '. "\<CR>")'
Here is how you can "move" the current buffer to a second GVim instance:
:!gvim --remote %
:bw
Note that Vim must be built with the +clientserver option.
No, it is not possible.
You cannot move a vim tab into a window, no matter new or existing. Because a vim tab page is a collection of windows. You cannot move a collection of windows into one single window.

Need some help configuring Vim

I'm new to Vim (almost new) and have some questions.
The problem came from the following:
I have NERDTree and MiniBufExplorer plugins (I like them very much!).
I have mapped Ctrl-F to show up MiniBufExplorer window.
So when I focus on tree window, then push Ctrl-F, find a file I want and then press Enter, the resulting file opens in tree window (which was previous one). So, I want to have a window with a specific Name/ID and tell MiniBufExplorer to always open buffers there. In most cases I don't use split windows so it would not be a problem for me to open buffers always in same window.
I have looked into manual of Vim but didn't found any reference to window's ID, so I wonder if there is a way to give an ID to window and then configure buffer explorer to always open buffers there.
Thanks for help in advance.
There is a switch to tell MiniBufExplorer to try to open files in a buffer that is modifiable (that is to say, not NERDTree):
let g:miniBufExplModSelTarget = 1
Prevent opening files inside NERDTree or MiniBuffExplorer

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

I should get a command prompt rather than a popup when I try to close an unsaved window

I am using git configuration mentioned here
If I create a new tab and then if I do commmand W ( I am using mac) to discard that window then I get a popup for which I have to use mouse.
I have seen others use vim where in similar cases they get a prompt at the bottom which is something like
C for cacel
N for No.
S far Save(not sure).
What configuration change I need to do so that I start getting command prompt rather then GUI prompt?
I am using macvim.
What you're looking for is:
:set guioptions+=c
command W is a standard the MacOS close tab key stroke. You can use vim's own :q or :q! to close it and forget any changes to the buffer.
If you're using macvim (currently my favorite incarnation of Vim for the Mac), the prompt you get, while GUI-ish like the rest of Macvim, is keyboard-controllable: return to accept the default Save, Esc to Cancel, command-D to mean Don't Save (i.e. quit and lose changes). If you're using Apple=supplied vim on Apple's Terminal.app, the popdown from command W (which doesn't offer Save but just Cancel and Close) is similarly keyboard controllable: Esc to Cancel, return to accept the default Close (and lose unsaved changes, if any).
If you're using some other version of vim, it would help if you let us know which one!-)

Resources