Move between Vim windows with <tab> and <s-tab> - vim

I have the following lines in my .vimrc:
nnoremap <tab> :wincmd w<cr>
nnoremap <s-tab> :wincmd W<cr>
I want to move between Vim windows quickly using in Normal Mode. The above mappings work all right between windows, but when I get to the MiniBufExplorer, it gets stuck and doesn't rotate to the first window.
How should I map this so that it doesn't move into MiniBufExplorer?

There are two lines in the minibufexpl.vim plugin that remap Tab to cycle through the buffer names shown in the MiniBufExplorer window. If you remove/comment these, your tab remapping will work.
nnoremap <buffer> <TAB> :call search('\[[0-9]*:[^\]]*\]')<CR>:<BS>
nnoremap <buffer> <S-TAB> :call search('\[[0-9]*:[^\]]*\]','b')<CR>:<BS>
Additionally, there are global settings below which already control the C-Tab functionality for switching between either windows or buffers. You may want to modify these or at least be aware of this feature. NB, you will still have to remove the above Tab mapping to get Tab (instead of C-Tab) based movement.
if !exists('g:miniBufExplMapCTabSwitchBufs')
let g:miniBufExplMapCTabSwitchBufs = 0
endif
" Notice: that if CTabSwitchBufs is turned on then
" we turn off CTabSwitchWindows.
if g:miniBufExplMapCTabSwitchBufs == 1 || !exists('g:miniBufExplMapCTabSwitchWindows')
let g:miniBufExplMapCTabSwitchWindows = 1
endif
" If we have enabled <C-TAB> and <C-S-TAB> to switch buffers
" in the current window then perform the remapping
"
if g:miniBufExplMapCTabSwitchBufs
noremap <C-TAB> :call <SID>CycleBuffer(1)<CR>:<BS>
noremap <C-S-TAB> :call <SID>CycleBuffer(0)<CR>:<BS>
endif
" If we have enabled <C-TAB> and <C-S-TAB> to switch windows
" then perform the remapping
"
if g:miniBufExplMapCTabSwitchWindows
noremap <TAB> <C-W>w
noremap <S-TAB> <C-W>W
endif

Not exactly what you asked for but these are useful keyboard shortcuts to move between windows.
map <c-j> <c-w>j
map <c-k> <c-w>k
map <c-h> <c-w>h
map <c-l> <c-w>l
This makes Ctrl + <direction> move between windows (including MiniBufExpl when it's open). Tab is probably better reserved for code completion, check out the SuperTab plugin.

Related

Mapping <c-w> to be "<c-w>w" in Vim

I am having trouble to activate the following mappings:
nnoremap <buffer> <C-K> <c-w>w
nnoremap <buffer> <C-S-K> <c-w>W
Where, I want have "pressing Ctrl+K in normal mode" to send me to the next buffer (of the same screen), in a clockwise way. <c-w>W in Vim will go the other direction. I want to map this action to be Ctrl+Shift+w.
Any idea why the mapping fails? I found only the latex-suite.vim has a mapping of <c-w> and none <c-s-w> has been mapped. The plugin mapping is buffer only.
Thank you for your input.
All the best,
-Linfeng
With the <buffer> keyword, your mappings only apply to the current buffer. So, when you have these in ~/.vimrc, they aren't effective, and if you type them in, you would need to have the same current buffer visible in multiple window splits. I don't think that's what you want. Instead, define them to be global mappings:
nnoremap <C-K> <c-w>w
nnoremap <C-J> <c-w>W
Edit: I've replaced <C-S-K> with <C-J>, because shifted control sequences are generally not available in Vim, unfortunately.
They could still be overwritten by some filetype plugins using nnoremap <buffer> <C-K>.
Alternative
I'd prefer the following set of directional mappings. They provide more precision, at the cost of wasting more keys:
nnoremap <C-j> <c-w>j
nnoremap <C-k> <c-w>k
nnoremap <C-h> <c-w>h
nnoremap <C-l> <c-w>l

How do I navigate buffers in vim?

I normally work with more than 5 files at a time. I use buffers to open different files.
I use commands such as :buf file1, :buf file2 etc.
Is there a faster way to move to different files?
What I use:
Go to the previous buffer: :b# or :bp
Go to the next buffer: :bn
If you know your file is loaded in buffer 5: :b5
To get a list of buffers: :buffers or shorter: :ls
And have a short look on :he buffer
And the wiki entry on Easier Buffer Switching on the Vim Wiki: http://vim.wikia.com/wiki/Easier_buffer_switching
SO already has a question regarding yours: How do you prefer to switch between buffers in Vim?
A few mappings can make your life a lot easier.
This one lists your buffers and prompts you for a number:
nnoremap gb :buffers<CR>:buffer<Space>
This one lists your buffers in the "wildmenu". Depends on the 'wildcharm' option as well as 'wildmenu' and 'wildmode':
nnoremap <leader>b :buffer <C-z>
These ones allow you to cycle between all your buffers without much thinking:
nnoremap <PageUp> :bprevious<CR>
nnoremap <PageDown> :bnext<CR>
Also, don't forget <C-^> which allows you to alternate between two buffers.
Below I describe some excerpts from sections of my .vimrc. It includes mapping the leader key, setting wilds tab completion, and finally my buffer nav key choices (all mostly inspired by folks on the interweb, including romainl). Edit: Then I ramble on about my shortcuts for windows and tabs.
" easier default keys {{{1
let mapleader=','
nnoremap <leader>2 :#"<CR>
The leader key is a prefix key for mostly user-defined key commands (some plugins also use it). The default is \, but many people suggest the easier to reach ,.
The second line there is a command to # execute from the " clipboard, in case you'd like to quickly try out various key bindings (without relying on :so %). (My nmeumonic is that Shift-2 is #.)
" wilds {{{1
set wildmenu wildmode=list:full
set wildcharm=<C-z>
set wildignore+=*~ wildignorecase
For built-in completion, wildmenu is probably the part that shows up yellow on your Vim when using tab completion on command-line. wildmode is set to a comma-separated list, each coming up in turn on each tab completion (that is, my list is simply one element, list:full). list shows rows and columns of candidates. full's meaning includes maintaining existence of the wildmenu. wildcharm is the way to include Tab presses in your macros. The *~ is for my use in :edit and :find commands.
" nav keys {{{1
" windows, buffers and tabs {{{2
" buffers {{{3
nnoremap <leader>bb :b <C-z><S-Tab>
nnoremap <leader>bh :ls!<CR>:b<Space>
nnoremap <leader>bw :ls!<CR>:bw<Space>
nnoremap <leader>bt :TSelectBuffer<CR>
nnoremap <leader>be :BufExplorer<CR>
nnoremap <leader>bs :BufExplorerHorizontalSplit<CR>
nnoremap <leader>bv :BufExplorerVerticalSplit<CR>
nnoremap <leader>3 :e#<CR>
nmap <C-n> :bn<cr>
nmap <C-p> :bp<cr>
The ,3 is for switching between the "two" last buffers (Easier to reach than built-in Ctrl-6). Nmeuonic is Shift- 3 is #, and # is the register symbol for last buffer. (See :marks.)
,bh is to select from hidden buffers (!).
,bw is to bwipeout buffers by number or name. For instance, you can wipeout several while looking at the list, with ,bw 1 3 4 8 10 <CR>. Note that wipeout is more destructive than :bdelete. They have their pros and cons. For instance, :bdelete leaves the buffer in the hidden list, while :bwipeout removes global marks (see :help marks, and the description of uppercase marks).
I haven't settled on these keybindings, I would sort of prefer that my ,bb was simply ,b (simply defining while leaving the others defined makes Vim pause to see if you'll enter more).
Those shortcuts for :BufExplorer are actually the defaults for that plugin, but I have it written out so I can change them if I want to start using ,b without a hang.
You didn't ask for this:
If you still find Vim buffers a little awkward to use, try to combine the functionality with tabs and windows (until you get more comfortable?).
" windows {{{3
" window nav
nnoremap <leader>w <C-w>
nnoremap <M-h> <C-w>h
nnoremap <M-j> <C-w>j
nnoremap <M-k> <C-w>k
nnoremap <M-l> <C-w>l
" resize window
nnoremap <C-h> <C-w><
nnoremap <C-j> <C-w>+
nnoremap <C-k> <C-w>-
nnoremap <C-l> <C-w>>
Notice how nice ,w is for a prefix. Also, I reserve Ctrl key for resizing, because Alt (M-) is hard to realize in all environments, and I don't have a better way to resize. I'm fine using ,w to switch windows.
" tabs {{{3
nnoremap <leader>t :tab
nnoremap <M-n> :tabn<cr>
nnoremap <M-p> :tabp<cr>
nnoremap <C-Tab> :tabn<cr>
nnoremap <C-S-Tab> :tabp<cr>
nnoremap tn :tabe<CR>
nnoremap te :tabe<Space><C-z><S-Tab>
nnoremap tf :tabf<Space>
nnoremap tc :tabc<CR>
nnoremap to :tabo<CR>
nnoremap tm :tabm<CR>
nnoremap ts :tabs<CR>
nnoremap th :tabr<CR>
nnoremap tj :tabn<CR>
nnoremap tk :tabp<CR>
nnoremap tl :tabl<CR>
" or, it may make more sense to use
" nnoremap th :tabp<CR>
" nnoremap tj :tabl<CR>
" nnoremap tk :tabr<CR>
" nnoremap tl :tabn<CR>
In summary of my window and tabs keys, I can navigate both of them with Alt, which is actually pretty easy to reach. In other words:
" (modifier) key choice explanation {{{3
"
" KEYS CTRL ALT
" hjkl resize windows switch windows
" np switch buffer switch tab
"
" (resize windows is hard to do otherwise, so we use ctrl which works across
" more environments. i can use ',w' for windowcmds o.w.. alt is comfortable
" enough for fast and gui nav in tabs and windows. we use np for navs that
" are more linear, hjkl for navs that are more planar.)
"
This way, if the Alt is working, you can actually hold it down while you find your "open" buffer pretty quickly, amongst the tabs and windows.
Once the buffers are already open, you can just type :b partial_filename to switch
So if :ls shows that i have my ~./vimrc open, then I can just type :b vimr or :b rc to switch to that buffer
There are many ways to solve. The best is the best that WORKS for YOU. You have lots of fuzzy match plugins that help you navigate. The 2 things that impress me most are
1) CtrlP or Unite's fuzzy buffer search
2) LustyExplorer and/or LustyJuggler
And the simplest :
:map <F5> :ls<CR>:e #
Pressing F5 lists all buffer, just type number.

VIM auto resize focused window

I am slowly learning vim and its powerful capabilities.
I have a question in regards to splitting windows (mainly horizontal splits).
Is there a way to automatically resize the currently selected (focused) window? Let's say, for example, a setting so that the focused window will always take up 70% of the screen.
Using Ctrl-w (number) +/- every time is not really efficient, especially when I am jumping between a few files constantly.
Also it would be cool if there was a way to restrict it to only horizontally splitted windows.
The 'winheight' setting determines the minimal number of lines for the current window. Some users set this to 999 for "Rolodex mode". The following sets this to 70%:
:let &winheight = &lines * 7 / 10
For anything fancier, you can hook into the WinEnter event via an :autocmd, and then set the window height to N via :Nwincmd _. Stupid example:
:autocmd WinEnter * execute winnr() * 2 . 'wincmd _'
Sounds like you want golden-ratio : Resize windows automatically using the Golden Ratio.
Once I found out you could disable the "automatic" part of golden-ratio, I started using it myself with these settings:
" Don't resize automatically.
let g:golden_ratio_autocommand = 0
" Mnemonic: - is next to =, but instead of resizing equally, all windows are
" resized to focus on the current.
nmap <C-w>- <Plug>(golden_ratio_resize)
" Fill screen with current window.
nnoremap <C-w>+ <C-w><Bar><C-w>_
I use these mapping for split screens:
nnoremap <C-j> <C-w>j
nnoremap <C-k> <C-w>k
nnoremap <C-h> <C-w>h
nnoremap <C-l> <C-w>l
nnoremap c<C-j> :bel sp new<cr>
nnoremap c<C-k> :abo sp new<cr>
nnoremap c<C-h> :lefta vsp new<cr>
nnoremap c<C-l> :rightb vsp new<cr>
nnoremap g<C-j> <C-w>j<C-w>_
nnoremap g<C-k> <C-w>k<C-w>_
nnoremap g<C-h> <C-w>h<C-w>_
nnoremap g<C-l> <C-w>l<C-w>_
nnoremap d<C-j> <C-w>j<C-w>c
nnoremap d<C-k> <C-w>k<C-w>c
nnoremap d<C-h> <C-w>h<C-w>c
nnoremap d<C-l> <C-w>l<C-w>c
This way if you want to jump between splits you can use C-hjkl. If you want to "create" a split you can use cC-hjkl. If you want to maximize a split you can use gC-hjkl. And if you want to delete a split you can use dC-hjkl.
If you want specifically 70% instead of maximized that you could use
nnoremap g<C-j> <C-w>j:let &winheight = &lines * 7 / 10<cr>
for example.

Remap Ctrl-ArrowKeys to switch between split buffers

How can I remap Ctrl-ArrowKeys to move between split buffers, like I can with <C-w> <ArrowKeys>?
Right now <C-Right> deletes until the end of line and goes into insert mode - which is not what i prefer (I use C for this) and would be happy to get rid of this behavior.
nnoremap <silent> <C-Right> <c-w>l
nnoremap <silent> <C-Left> <c-w>h
nnoremap <silent> <C-Up> <c-w>k
nnoremap <silent> <C-Down> <c-w>j
It must be said that not all mappings work on all terminal, so you mileage may vary. This is working well with gvim.
I also use those mapping when working with spli windows:
" To move between buffers and maximise the selected one
map <C-J> <C-W>j<C-W>_
map <C-K> <C-W>k<C-W>_
Combined with
" This sets the minimum window height to N
set wmh=0
It's pretty good.
I have been used this for a while now and I'm completely addicted!

easier way to navigate between vim split panes

I am using NERDTree on vim and usually open files with i
Is there an easy way to switch between different panes? Currently I use CTRL+W+W to move from one pane to another.
Long ago I found a tip (once on vim.org, now on wikia, apparently) that I've stuck with. Remap ctrl-[hjkl] to navigate splits. It has served me well.
" Use ctrl-[hjkl] to select the active split!
nmap <silent> <c-k> :wincmd k<CR>
nmap <silent> <c-j> :wincmd j<CR>
nmap <silent> <c-h> :wincmd h<CR>
nmap <silent> <c-l> :wincmd l<CR>
I prefer hitting single keys over hitting key-chords. The following maps pane movement to arrow keys:
" Smart way to move between panes
map <up> <C-w><up>
map <down> <C-w><down>
map <left> <C-w><left>
map <right> <C-w><right>
I know this is an old question, but I have a perfect way. Using the number of the split.
split_number C-w C-w
The panes are numbered from top-left to bottom-right with the first one getting the number 1.
for example to go to split number 3 do this 3 C-w C-w, press Ctrl-w twice.
Key mappings are definitely the way to go. I use the mappings mentioned by overthink. I also include the following mappings in my vimrc to move the splits themselves.
" Move the splits arround!
nmap <silent> <c-s-k> <C-W>k
nmap <silent> <c-s-j> <C-W>j
nmap <silent> <c-s-h> <C-W>h
nmap <silent> <c-s-l> <C-W>l
This makes it so that if the split opens in the wrong spot (lets say the left side and I want it on the right) I go to that split and hit <C-S-l> and the split moves where I want it to.
In order to be consistent with changing tabs via gt & gT, I'm currently trying out the g mappings for changing splits. I tend to hit the shift key as I go for the Ctrl key so this helps me avoid that mistake until I get better at not doing so.
nnoremap gh <C-W><C-H>
nnoremap gj <C-W><C-J>
nnoremap gk <C-W><C-K>
nnoremap gl <C-W><C-L>
I have mapped ctrl+w ctrl+w to <tab> (under normal mode as in normal mode tab does not have any use)and that's have made my life easier as now I can switch between panes easily by pressing <tab>.
For switching to a particular pane, I can press <i> + <tab> to switch between panes as split window panes also got their own number which can replace i.
Ex. i = 1,2...n.
Very easy way of achieving it. Type this shortcut twice, and that should work
ctrl+w ctrl+w

Resources