Opening a window in a horizontal split of a vertical split - vim

I want have both NERDTree and TagList open at the same time, but I don't need for them to have the whole height of the screen. Instead I would like to have them split horizontally within a single vertical split.
More specifically, I want to be able to open one, say NERDTree, and have it take up the full height of the screen. Then when I open TagList, to have it horizontally split with the already open NERDTree window.
Is there a way to do this? Possibly even a non 'specific to these two plugins' way?

NERDTree and Taglist aren't going to open inside splits.
Let's say you have a split buffer on the left (for eg. source code), and Taglist on the right.
You can put this in a function, and invoke it with a mapping:
NERDTree " open NERDTree
wincmd J " move NERDTree to the very bottom
wincmd k " move the cursor to the above split (the source code buffer)
wincmd H " move the split to the very left
wincmd l " move the cursor back to the NERDTree split
resize 20 " resize the split height to 20
exe 'vertical resize ' . g:NERDTreeWinSize " reset the split width
This is pretty dodgy, but it works. You'll see a brief visual glitch while the split jumps around the screen.

Horizontal split:
:split
Vertical split:
:vsplit
Then when you have split your screen how you prefer, open nerdtree inside the desired frame.
(Or you can use abbreviation :sp :vsp like suggested by #Vivek Pandey).

I've posted a solution in a duplicate post here:
NERDTree & TList in
It involves editing the 'taglist.vim' script a bit to add a new split option when opening.

Related

Vim function to switch between splits

I am trying to find a function which will switch back to the original window in a split view for vim. I know about shortcut and I also know about the function :call cursor. But is there a function which can let me switch back to the previous split window so I could stick it in my vim function?
In a vimscript you can use:
" Save the window where you are currently
let l:currentWindow=winnr()
" Do stuff where you change of window
" Go back to the original window
exe l:currentWindow . "wincmd w"
For more information the doc is always an excellent reading:
:h wincmd
:h winnr()
Edit Another way to do it is to use wincmd p:
wincmd is the vimscript equivalent to Ctrlw in normal mode.
In normal mode when you change of window you can use Ctrlw + p to come back to the previous window. So in vimscript you simply use:
wincmd p
To go back to the previous window.
Of course if the rest of your function use more than 2 splits you will not go back to your initial window but if you have only two splits it can be lighter than using a variable to keep the number of your window.

How to change the default position of quickfix window in Vim?

Setup: MacVim with MiniBufExplorer plugin window spanning the entire top and Taglist plugin window on the right.
Due to the fact that I keep my Taglist on the right, whenever I open the quickfix window, it is positioned on the far right, below the Taglist window (with the same width as the Taglist window)
Is it possible to change the default opening position logic, so that the quickfix window will open below my main code window (down and to the left) or maybe span the entire width at the bottom of the Vim viewport?
While it is not possible to change the default split-window behavior
of the :copen command, one can approach the issue in two ways.
1. Use the commands that directly alter window splitting directions
(see :help :vertical and below until the “Closing a window” paragraph).
For instance, consider the commands:
:botright copen
or
:botright cwindow
to make the quickfix window open as the bottommost one, or even:
:vertical topleft cwindow
to open it to the top left of the current window.
These commands can be shortened to :bo cope, :bo cw, and :vert to cw, respectively. Also, one can, of course, create a short mapping or
a custom command for their quick invocation.
2. Alternatively, move the quickfix window to the bottom of the window
layout using an auto-command:
:autocmd FileType qf wincmd J
This trigger takes advantage of the fact that the quickfix window can
be easily distinguished by its file-type, qf. The wincmd J command
is equivalent to the
[Ctrl+W,
Shift+J]
shortcut sequence instructing Vim to move the current window to the
very bottom of the screen (see :help :wincmd and :help ^WJ).
By default, Vim opens the new window above the current one for horizontal splits, and to the left of the current one for vertical splits (:help opening-window). You can customize this behavior like most other things in Vim:
make the new window appear below the current window.
:set splitbelow
make the new window appear on the right.
:set splitright

Open before opened buffer in vertical window in vim

I have two opened buffers (i.e. A and B) and one window. Currently I see A, I would like to open B in vertical window. To do it in horizontal window I can hit Ctrl+w ^, how to do it in vertical window? And how to change horizontal windows to vertical windows?
To display your two buffers vertically, you can use :
:vsplit #
or the short version
:vs #
To reorganize your layout from horizontal to vertical, you must use:
<C-W>K : will move the current windows to to the top with a horizontal split
<C-W>J : will move the current windows to to the bottom with a horizontal split
<C-W>H : will move the current window to the the left with a vertical split
<C-W>L : will move the current window to the the rigth with a vertical split
See :help windows.txt for more information
You may use Vertical Split
Ctrl+Wv
command line :vertical split
open a vertical split: :vs
go to that split (assuming it's on the right: CTRL-W l
open buffer B there: :b B

Open a buffer as a vertical split in VIM

If you are editing a file in VIM and then you need to open an existing buffer (e.g. from your buffer list: :buffers) how can you open it in a vertical split?
I know that you already can open it with a normal split like:
:sbuffer N
Wehere N is the buffer number you want, however, the above opens that N buffer horizontally, not vertically.
I'm also aware that you can change the window placement after opening and have a Vertical Split like so:
Ctrl-W H
Ctrl-W L
Which will vertically split the window to the right or the left.
It seems to me that if there is a sbuffer there should be a vsbuffer but that doesn't exist (not that I am aware of)
Also, please note that I am not looking for a plugin to solve this question. I know about a wealth of plugins that will allow you to do this.
I am sure I might be missing something that is already there.
EDIT:
In the best spirit of collaboration, I have created a simple Function with a Mapping if someone else stumbles across this issue and do not want to install a plugin:
Function:
" Vertical Split Buffer Function
function VerticalSplitBuffer(buffer)
execute "vert belowright sb" a:buffer
endfunction
Mapping:
" Vertical Split Buffer Mapping
command -nargs=1 Vbuffer call VerticalSplitBuffer(<f-args>)
This accomplishes the task of opening a buffer in a right split, so for buffer 1, you would call it like:
:Vbuffer 1
Try:
:vert sb N
which will open a left vertical split (by default, unless you have modified some options).
To open a split to the right, on the other hand:
:vert belowright sb N
:vsp | b1
1 being some buffer number. Use buffers to list all buffers.
Here's some additional info on splits, if you're interested.
Link
The answer to the OP that I found most useful is embedded deep in Jerinaw's answer and a comment on it, and in Wolfson's answer. But I felt it might be brought out more. (Nor have those been voted most highly, even though they seemed to me the ones that answered OP best.)
The answer to the question, Why is there not :vsbuffer, is that there is. It's called :vsplit and does the trick either as
:vsplit NameOfBuffer
OR
:vsplit #NumberOfBuffer.
(In this second use, take care to note that the hash # is significant. If you want to get to buffer number 3, you need to say :vsplit #3, not just :vsplit 3 which will instead create a new file named "3".)
Again, this answer is embedded above, it's just not brought out clearly enough for the quick scanner, IMV.
You can ease your pain by adding the following to your .vimrc
cabbrev vb vert sb
Now you can use it in the following way.
:vb <buffer>
You can also combine :ls that lists your current buffers and the commands to open the desired buffer in either
current window: :b <N/bufname>
vertical split: :vsp | b <N/bufname>
horizontal split: :sp | b <N/bufname>
For this, I've added the following mappings to my ~/.vimrc (order of mappings represents the above list of desired windows)
nnoremap <leader>b :ls<cr>:b<space>
nnoremap <leader>v :ls<cr>:vsp<space>\|<space>b<space>
nnoremap <leader>s :ls<cr>:sp<space>\|<space>b<space>
Based on this, you can see the buffer list as soon as you hit
<leader>b
<leader>v
<leader>s
and then just enter the desired buffer number N. This will then open the buffer in the desired window. You can of course still use a part of the buffer name bufname as well.
I mapped the <leader> to , based on
let mapleader = ","
For some people (e.g. me) this could even replace plugins like MiniBufExpl and thus save space on the screen.
you can use Neovim,like that:
autocmd FileType python nmap <F5> :rightbelow vertical split <bar> :term python %<cr>

"Minimizing" vertical VIM window splits

I use horizontal and vertical window splits in religiously in VIM and up until recently, I enjoyed the comfort of two commands to effectively hide (or minimize) my horizontal splits. I set them up adding the following lines to my .vimrc file:
set winminheight=0
map <C-J> <C-W>j<C-W>_
map <C-K> <C-W>k<C-W>_
Hitting Control-j or Control-k navigates through horizontal splits by going up or down. What I'd like to accomplish is the same thing for vertical splits by showing or hiding the left or right split using Control-Shift-h and Control-Shift-l; h moving to the left, l moving to the right. I have tried the following with little to no success:
set winminwidth=0
map <S-C-L> 500<C-W>h<C-W>_
map <S-C-H> 500<C-W>l<C-W>_
The action would be similar to utilizing Control-w-< and Control-w->, only moving the vertical split completely to the left or write, not just one line at a time.
Any ideas on how to accomplish this? Thanks.
First up, you won't be able to use <S-C- (shift + control) in your code (see below). But you can use the 'mapleader' as your "shift" and then use the <C-h> and <C-l> like you want to. Like this:
set winminwidth=0
nmap <Leader><C-h> <C-W>h500<C-W>>
nmap <Leader><C-l> <C-W>l500<C-W>>
The common leader keys in vim are comma and back-slash:
:let mapleader = ","
But you'll find that this gets annoying to require 3 keystrokes for this, so you might as well just drop the control key stroke. This way (if your leader is comma) you can just press ",h" and ",l" to go to the splits to your left and right:
set winminwidth=0
nmap <Leader>h <C-W>h500<C-W>>
nmap <Leader>l <C-W>l500<C-W>>
" (FTW) :D
...
A guy named Tony Chapman answers why you can't use control + shift:
Vim maps its Ctrl+printable_key
combinations according to ASCII. This
means that "Ctrl+lowercase letter" is
the same as the corresponding
"Ctrl+uppercase letter" and that
Ctrl+<key> (where <key> is a printable
key) is only defined when <key> is in
the range 0x40-0x5F, a lowercase
letter, or a question mark. It also
means that Ctrl-[ is the same as Esc,
Ctrl-M is the same as Enter, Ctrl-I is
the same as Tab.
So yes, Ctrl-s and Ctrl-S (i.e. Ctrl-s
and Ctrl-Shift-s) are the same to
Vim. This is by design and is not
going to change.
Try
set winminwidth=0
map <S-C-L> <C-W>h<C-W>|
map <S-C-H> <C-W>l<C-W>|
This doesn't move a window completely to the left or right (that's <C-W>H and <C-W>L), it just moves the cursor to the left (or right) window and maximizes that window horizontally.
See :help CTRL-W_bar for more.
Crl-w 1 |
will minimize current window in Vim.

Resources