Map TAB key to switch splitted windows in vim - vim

How can I map the TAB key to switch among splitted windows in vim? I want to be able to type tab and switch among the windows, without having to press the leader key before pressing tab.

You can use :nnoremap or :nmap like discussed on this vim.fandom page.
:nnoremap <TAB> <C-W><C-W>

Related

vim: Tagbar windows opened when typing <Enter> in normal mode

When typing <Enter> in normal mode in Vim with the Tagbar plugin installed, the Tagbar window is opened automatically. I want to disable this functionality. What must I do?
put the flowing code in you .vimrc
unmap <cr>
Your mapping for <C-m> is actually the cause of the Enter key opening Tagbar. If you remove that map from your vimrc, the enter key will no longer trigger :TagbarToggle.
Mappings for <C-m> and <CR> (Enter) are synonymous in Vim:
The following table shows the mapping between some of the keys on the keyboard and the equivalent Ctrl-key combination:
Ctrl-I Tab
Ctrl-[ Esc
Ctrl-M Enter
Ctrl-H Backspace
If you use one of the Ctrl-key combination in the above table in a map, the map also applies to the corresponding key. Both the keys produce the same key scan code. For example, if you create a map for CTRL-I, then you can invoke the map by pressing Ctrl-I or the Tab key.
This means when you set nmap <C-m> :TagbarToggle<CR>, it is the same as
also setting nmap <CR> :TagbarToggle<CR>.
You'll probably want to choose a new key instead of M. The alternative is to
change the key code sent by <C-m> at the operating system level with some
remapping program.
The terminal key bindings come from readline, the program that processes input text in
your terminal prompt. A full list of key bindings is in the readline
manual.
You can find more info about Vim key codes by typing :help keycodes in Vim, or reading the help docs here.
Try :help tagbar to open the documentation. It sounds like you might have a mapping in your vimrc file that says something like
nnoremap <silent> <CR> :TagbarToggle<CR>
or
nnoremap <silent> <CR> :TagbarOpen<CR>
if you find and remove that mapping will no longer open Tagbar

Ctrl + w + w obviously cannot perform to switch tab on a google compute engine window

Ctrl + w is a shortcut to closing an open window on VM instance of Google Compute Engine. Hence it will ask to perform that action before letting me type one more w, to enact switching of tabs between the directory tree and the script to work on in vim.
I've tried the following, as mentioned here
map <C-l> :tabn<CR>
map <C-h> :tabp<CR>
map <C-n> :tabnew<CR>
While I'm not sure what tabn and tabp indicate, I tried the first two line and (C as Ctrl) neither of these respond to anything new. I used source ~/.vimrc command before expecting a change is reflected. What is wrong here?
I think you're mixing up Vim tabs and Vim windows.
Vim windows will split the screen vertically or horizontally into separate panes, which are all visible at the same time. That's typically used by directory tree plug-ins which want to display a navigator on a sidebar.
Vim tabs group a set of windows, so that you can switch between whole sets of windows at once and still easily go back to where you were before. (People often use tabs to work on different projects and switch between them, though opening one file per tab, fullscreen, is also a somewhat common workflow.)
The mappings that use Control-W are window mappings, not tab mappings. (You can switch to next tab with gt and previous tab with gT).
The normal commands to cycle windows are Ctrl-W w (to move right/down) and Ctrl-W W (to move left/up), so you can use these two mappings:
nnoremap <C-l> <C-w>w
nnoremap <C-h> <C-w>W
If you want a mapping to open a new window with a new blank file, you can use:
nnoremap <C-n> <C-w>n
If your problem is with typing Control-W in specific, perhaps a better option is to map a key sequence you're not using to replace Control-W, but keep it to just the prefix, so that all other commands that follow are still available?
Perhaps use Control-Q, which is just next to W in the keyboard:
nmap <C-Q> <C-W>
And you might want to remap the commands that use the same key twice, so in your case use Q twice where there's two W's:
nmap <C-Q> <C-W>
nnoremap <C-Q>q <C-W>w
nnoremap <C-Q>Q <C-W>W
nnoremap <C-Q><C-Q> <C-W><C-W>
Those four there, that would be my recommendation, if Control-W is an inconvenient sequence for you.

Switching nodes (tabs) NerdTree vim on insertions mode

I've been searching for the shortcut to switch tab when i'm in insertion mode. I know by pressing gT or gt for switching tab on visual mode. Is nerdTree already have this features or do I need to map the key for this?
First, this has nothing to do with NERDTree. Tabs are a built-in feature of Vim. It may be more useful with NERDTree (as you open multiple locations in different tabs), though.
If you go to the :help page of gt, you'll see all alternative keys:
:tabn[ext] *:tabn* *:tabnext* *gt*
<C-PageDown> *CTRL-<PageDown>* *<C-PageDown>*
gt *i_CTRL-<PageDown>* *i_<C-PageDown>*
In Vim's help, the i_ prefix represents insert mode. So, the built-in mapping to switch tabs in insert mode is Ctrl + PageUp / PageDown.
If you want to use different keys, you can use mappings like the following:
inoremap <C-Tab> <C-\><C-N>:tabnext<CR>
inoremap <C-S-Tab> <C-\><C-N>:tabprevious<CR>

Tab Navigation mapping is not working

NOTE: I am using Terminator instead of terminal. But as all other mappings are working fine why does these are not working.
I have tried to use these mappings in my vimrc file to be able to use Tab Navigation. But its not working at all.
nnoremap <C-S-tab> :tabprevious<CR>
"nnoremap <C-tab> :tabnext<CR>
nnoremap <C-tab> :tabn<CR> "I also tried this
nnoremap <C-t> :tabnew<CR>
inoremap <C-S-tab> <Esc>:tabprevious<CR>i
inoremap <C-tab> <Esc>:tabnext<CR>i
inoremap <C-t> <Esc>:tabnew<CR>
inoremap <C-S-w> <Esc>:tabclose<CR>
"Also to go to the nth tabpage Use <A-Fn>
nnoremap <A-F1> 1gt
nnoremap <A-F2> 2gt
nnoremap <A-F3> 3gt
nnoremap <A-F4> 4gt
nnoremap <A-F5> 5gt
nnoremap <A-F6> 6gt
nnoremap <A-F7> 7gt
nnoremap <A-F8> 8gt
nnoremap <A-F9> 9gt
nnoremap <A-F10> 10gt
NOTE: I have ctags and cscope installed. So I think there might be some confliction as ctrl-t is to jump back from a certain tag. And only this mapping is working for new tab.
Also I have checked ctrl-PageDown is working fine for the same purpose.
2nd Question:
How does this key notation works in vimrc.
Is it something like this:
All modifier keys should be used in Caps like
C for Ctrl.
A for Alt.
S for Shift.
And other keys are all in small.
But what about keys like:
Home
End
Backspace
Escape
PageUp
PageDown
Tab
Function keys etc.
How to use them?
Here
I read that how these should be used in mapping but even they have used tab instead of Tab in mappings.
Brace yourself for disappointment.
The terminal keycodes
Vim accepts terminal keycodes, so not all key combinations are possible. The best way to figure out which keys are recognizable is to open insert mode and press ctrl+v followed by your key combination. This will show you the raw codes. Do this for another key combination. If the raw codes are the same then Vim can not distinguish between them. e.g. ctrl+v ctrl+shift+tab.
Your mappings
You should probably avoid doing insert mode mappings to switch tabs. It simply isn't the Vim Way as insert mode should only be used in short bursts.
Your :tabprev and :tabnext mappings can simplified into gT and gt mappings. Personally I do not mind the default gt or gT mappings.
<key> notation
As far as I know the case does not matter. All my mappings are lowercase. For a list of <> notation please see :h key-notation.
You are correct on the <c-..> for control, <a-...> for alt, and <s-...> for shift. Example combination would be <c-s-space>. Note: most <c-s-...> mappings are going to fail.
Using Tabs
Vim's tabs are not like most text editors tab. They are more like viewports into a group of windows/splits. Additionally, Vim is buffer centric, not tab centric like most editors. For example using features like Vim's quickfix list is often easier without tabs (See :h 'switchbuf if you must use tabs). Vim's tabs often get in the way of using a splits as there are better window and buffer navigation commands available. I personally have many files open (sometimes 100+) using no tabs and use on average 1-2 splits without any issue.
Bottom line: Learn to use buffers effectively.
Conclusion
I would suggest you break this tab workflow quickly and learn to love buffers. You won't really need your mappings and you will not be working against Vim's nature.
Read :help key-notation for an explanation of… Vim's key notation.
It is generally a good idea to play it safe so I recommend to follow these conventions when mapping combos:
always use an uppercase letter for the modifier key, C for Control, S for Shift, A for Alt, D for Command (MacVim GUI only), M for Meta,
always use a lowercase letter for the alphabetical keys, abc…xyz,
always capitalize the first letter of "special" keys, Tab, Space, Up, etc.
Examples:
<S-Up>
<C-r>
<A-LeftMouse>
However, the following notation works just as well so… whatever notation you choose, try to be consistent:
<s-UP>
Using multiple modifiers in a single mapping doesn't work reliably so you will be better in the long run if you completely avoid them.
nnoremap <C-S-j> :echo "csj"<CR>
nnoremap <C-j> :echo "cj"<CR>
now press <C-j> and <C-S-j> in normal mode.
<C-S-w> is indistinguishable from <C-w>.
:verbose map <C-t>
shows you what is mapped to <C-t> and where the mapping occurred. You can use it to debug your mappings.
And I agree with Peter, you are using both tab pages and insert mode wrongly.

How do I press "<C-L> <C-W>l<C-W>_" in Vim?

In one of the vim config files I have noticed this keyboard mapping
map <C-L> <C-W>l<C-W>_ supposedly for easier moving in tabs and windows. What does that translate to keyboard presses? What is that underscore at the end for?
The command map <C-L> <C-W>l<C-W>_ maps Ctrl-L to Ctrl-W, l, Ctrl-W, _.
You invoke this binding by just pressing Ctrl-L. To invoke what it binds to you would type Ctrl-W, then l, followed by Ctrl-W again, and finally _ (which on a US keyboard is shift-hyphen). This is two separate bindings, <C-W>l moves the cursor to the window to the right, and <C-W>_ resizes current window to the maximum possible vertical size.
The Ctrl+wlCtrl+w_ keys sequence is somewhat too long so someone has created a shortcut ("mapping" in Vim-speak): Ctrl+L for it.
<C-w>l<C-w>_ moves the the cursor to the window on the right (<C-w>l) and maximizes it vertically (<C-w>_).
Mappings always follow the same structure:
map (or imap for insert mode mapping, nmap for normal mode mapping, etc.)
some whitespace
the shortcut you want, here <C-L>
some whitespace
the sequence of commands triggered by the shortcut
See :help windows for more info on window management and :help mapping for more info on mappings.

Resources