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.
Related
I currently map my F2 and F3 as following:
map <F2> :tabn <CR>
map <F3> :tabp <CR>
imap <Esc> :tabn <CR>
imap <Esc> :tabp <CR>
I try to figure out how to map the normal/insert mode at the same time.
I spent some times on googling around without any luck.
Any suggestion would be appreciated.
The best I've found so far is:
nnoremap <F2> :tabn<CR>
imap <F2> <C-O><F2>
nnoremap <F3> :tabp<CR>
imap <F3> <C-O><F3>
Still an extra line for each key, but at least the actual command (:tabn, :tabp) is only mentioned once (and only needs to be changed in one place if you want to change it).
Honestly in my opinion, these mappings are probably not worth it.
gt/gT already exist to move between tabs. See :h gt
Normal mode is crucial to Vim and it makes sense to use this mode and not insert mode for movement between tabs. It is called normal mode for a reason as it is the mode you should normally be in.
Aside about tabs and buffers
Your mappings suggest a heavy tab centric workflow. I know it might sound weird but maybe try and use less tab panes together with a more buffers centric workflow. Here are some nice posts about it:
Why do Vim experts prefer buffers over tabs?
Use buffers effectively!
I am using vim-airline plugin which works pretty good however, the only way to switch between tabs is to user :bp or :bn which is shortcuts for :bprevious or :bnext. The problem with this is that if I am on first tab and want to switch to the last tab i.e. 10th tab then I have to type :bn ten times to get there. How can I switch directly? Maybe something pressing arrow keys would be beneficial.
Your problem is that you installed a plugin designed to obfuscate Vim's regular commands and workflows without even knowing those regular ways. The shortcut you took lead you to a dead-end so your only reasonable solution is to go back to the main road and learn Vim properly, without training wheels and crutches.
So… from your question, it seems you are using Airline's so-called "smarter tabline" which displays your open buffers in a fake tabline.
If it was a real tabline, they would actually be tab pages and you would move between them with their own set of commands.
But they are buffers and yes, you are supposed to move between them with these commands:
:bnext
:bprevious
:bfirst
:blast
:b10
:b <buffer-name>
which can all be mapped for your convenience, of course.
But… that plugin doesn't show buffer numbers, so you can't use :b10 to jump reliably to the tenth buffer in your fake "tabline" so that's one less tool in your tool-belt.
And some special buffers, like the quickfix list, can be reached with :bn/:bn without — probably — being listed in your fake "tabline" so that makes your fake "tabline" a rather poor abstraction, even without considering the glaring limitations of tabs in general.
And there's the idiosyncratic behavior of that fake "tabline" which becomes a semi-real "tabline" when you actually use tab pages.
Conflating two very different — and powerful in their own ways — concepts into a single bastardized one is not really a good idea.
I suggest you disable that option and use buffers and tab pages as they are meant to be used.
Reference:
:help buffers
:help tab-page
Agree with #romainl but you can always map your +tab to :bn or :bp for your ease.
" Tab navigation like Firefox.
nnoremap <C-S-tab> :bprevious<CR>
nnoremap <C-tab> :bnext<CR>
In addition to Rafi's answer, put this in your .vimrc to get direct access to a buffer / airline tab.
nmap <leader>1 :bfirst<CR>
nmap <leader>2 :bfirst<CR>:bn<CR>
nmap <leader>3 :bfirst<CR>:2bn<CR>
nmap <leader>4 :bfirst<CR>:3bn<CR>
...
Alternatively, you can double down on airline with
let g:airline#extensions#tabline#buffer_idx_mode = 1
nmap <leader>1 <Plug>AirlineSelectTab1
nmap <leader>2 <Plug>AirlineSelectTab2
if you are using vim-airline, I'm strongly suggested that to using AirlineSelectTab command, you can see it via :help airline, below is the configuration.
let g:airline#extensions#tabline#buffer_idx_mode = 1
nmap <leader>1 <Plug>AirlineSelectTab1
nmap <leader>2 <Plug>AirlineSelectTab2
nmap <leader>3 <Plug>AirlineSelectTab3
nmap <leader>4 <Plug>AirlineSelectTab4
nmap <leader>5 <Plug>AirlineSelectTab5
nmap <leader>6 <Plug>AirlineSelectTab6
nmap <leader>7 <Plug>AirlineSelectTab7
nmap <leader>8 <Plug>AirlineSelectTab8
nmap <leader>9 <Plug>AirlineSelectTab9
nmap <leader>0 <Plug>AirlineSelectTab0
nmap <leader>- <Plug>AirlineSelectPrevTab
nmap <leader>+ <Plug>AirlineSelectNextTab
If you are using tabs instead of buffers
" Tab navigation (works only in gvim, not in console)
nnoremap <C-tab> gt
nnoremap <C-S-tab> gT
I have configured vim to switch between tabs using Ctrl + arrow keys.
Ctrl + ← will switch to tab that is on the left of current tab.
Ctrl + → will switch to tab that is on the right of current tab.
Ctrl + ↑ will switch to first tab.
Ctrl + ↓ will switch to last tab.
To achieve above behaviour update your vimrc with following lines:
nnoremap <C-Left> :tabprevious<CR>
nnoremap <C-Right> :tabnext<CR>
nnoremap <C-Up> :tabfirst<CR>
nnoremap <C-Down> :tablast<CR>
This works with vim-airline plugin as well.
Reference: Using vim tab pages
I found this code the forum to make my life easier when switching between tabs and I placed it in my ~/.vimrc. Happily, it works well. Unfortunately, whenever I ctrl+tab to change tab, the cursor moves back one character. Can anyone help me keep the cursor in one place?
set showtabline=2
:nmap <C-S-tab> :tabprevious<cr>
:nmap <C-tab> :tabnext<cr>
:nmap <C-t> :tabnew<cr>
:map <C-t> :tabnew<cr>
:map <C-S-tab> :tabprevious<cr>
:map <C-tab> :tabnext<cr>
:map <C-w> :tabclose<cr>
:imap <C-S-tab> <ESC>:tabprevious<cr>i
:imap <C-tab> <ESC>:tabnext<cr>i
:imap <C-t> <ESC>:tabnew<cr>
The cursor moves back one character because that's how i works. Technically, you can change
:imap <C-tab> <ESC>:tabnext<cr>i
to
:imap <C-tab> <ESC>:tabnext<cr>li
but staying in insert mode is not a very good idea. If you can't help it, I'd suggest you remove the three imap from your ~/.vimrc to force you to use Vim correctly.
By the way…
You don't need that leading : in your ~/.vimrc:
map <C-t> :tabnew<cr>
You don't really need those :map because you already have nmap (and imap, but it should go).
<C-t> is very useful, both in insert mode (indents the current line) and in normal mode (goes back in the tagstack). You shouldn't override it like that.
<C-S-Tab> will probabbly not work in a terminal.
Tabs are not designed as 1-to-1 proxies for files and thus should not be used as such. You should use buffers instead.
Everything romainl said is good advice, in particular that tabs are not supposed to work as one-to-one proxies for files, like in other editors. Learn to use buffers, and your life will be easier. They're really very simple: all you really need is :set hidden, :ls, :bn, :bp, maybe some mappings like the ones you're setting up for tabs, and a bit of the Vim help.
That said, romainl's mapping will fail at the end of a line. A better solution uses <C-o>, which takes you out of insert mode temporarily for one command, and takes into account stuff like end-of-line behavior. You could use:
:imap <C-tab> <C-o>:tabnext<cr>
I use mappings to normal mode commands that I'd also like to work in insert mode. This can be done by adding <C-o> to insert mode mapping:
nmap <C-Up> 10<Up>
imap <C-Up> <C-o>10<Up>
But this means repeating each mapping twice.
To avoid repetition, I've tried to "overload" some other key, then use it for mode-specific part:
" F12 selects prefix suitable for current mode
nmap <F12> <Nop>
imap <F12> <C-o>
" single mapping relying on "overloaded" F12
map <C-Up> <F12>10<Up>
For some reason, it doesn't work. F2 in insert mode just inserts <F2> as text.
Any idea what's wrong and how to fix it?
Bonus points if you can extend the solution to visual mode.
As ZyX has already pointed out, there is no single :map command for all modes, because it mostly doesn't make sense. If you really want to define a mapping for all modes, use both :map and :map!; see :help map-modes.
As you typically define mappings only once in your .vimrc, I would not worry too much about the little duplication, but if you do, you can use a wrapper function to avoid this:
function! MapBoth(keys, rhs)
execute 'nmap' a:keys a:rhs
execute 'imap' a:keys '<C-o>' . a:rhs
endfunction
call MapBoth('<C-Up>', '10<Up>')
Original
nnoremap <F2> :w<CR>
inoremap <F2> <Esc>:w<CR>a
map sometimes does not set it for all modes. I don't know the exact reason, so to be sure I like to explicitly set all mapping in my configuration file. I suggest that you do the same as there are cases where you can get something unexpected due to different modes. That's why it is important to consider every remapping that you do for each particular mode with care.
In addition, favor *noremap command instead of just *map everywhere you can as recursive mapping is a known source of errors, especially for beginners.
Lastly, I don't know what are you trying to achieve by binding writing of a file in visual mode. Are you aiming for partial buffer writing (it's when you selected something in visual mode, then hit this file-writing shortcut and only selected text is written)? Or do you want the whole file to be written when you are in visual mode, regardless of whether you selected anything or not when you hit the file-writing shortcut? Provide more information on that. Personally, in either case it is weird mapping for visual mode, as it is really not indented for that. It's rather better to keep such stuff in normal mode.
Update
As others have already given exhaustive answers on your question, I just thought that it would be helpful if add my 2 cents, but in slightly different direction. By looking on what you are trying to do, namely mapping navigation features involving arrow keys in insert mode, I can infer that you are very new to Vim. As you probably already know, the philosophy behind Vim is that you should never ever touch mouse during your work inside Vim - call it a kind of golden rule.
What I want to point out now, is what I call a silver rule, and it basically looks like this:
noremap <Up> <Nop>
noremap <Down> <Nop>
noremap <Left> <Nop>
noremap <Right> <Nop>
inoremap <Up> <Nop>
inoremap <Down> <Nop>
inoremap <Left> <Nop>
inoremap <Right> <Nop>
In other words, prevent yourself from using arrow keys (everywhere except command-line mode). Your fingers should always be only in the region of character keys. Vim is all about modes. Insert mode is not for navigation - it is intended for bursts of typing. When you work with code or just text (doesn't matter) you spend most of your time in normal mode - navigating - looking through the file, seeking where to land next in order to edit something, add something, i.e. to do your next input burst for which you switch to insert mode, and when you are finished you switch back to normal mode to look for some more meat - like a predator. :)
So what is it all about? I just want to head you to the right direction right from the beginning. This way you can become intermediate Vim user very quickly - just a few days. In order to get better feeling of all the aforementioned I suggest that you should definitely watch Vim Novice Video Tutorials by Derek Wyatt where he talks about all that stuff in more detail and shows it in action in the screencasts. There are also Intermediate and Advanced tutorials by him which you might also look when you are comfortable with the basics.
I wish you happy vimming! :)
There are no commands to define mappings for all modes: :map maps for normal, operator-pending and visual modes (really visual and select at once) which is clearly stated in documentation. It does not make any sense to have same mapping for all modes, though unlike movement ones saving may be done in all modes with exactly the same rhs:
function s:Save()
update
return ''
endfunction
noremap <expr> <F2> <SID>Save()
noremap! <expr> <F2> <SID>Save()
. noremap! is another multi-mode mapping command, it covers insert and command mode now. You can’t move the cursor from <SID>Save() function (textlock) thus this method is not applicable for cursor movement commands, but you can use variables in order not to repeat the same thing twice:
let s:tendownlhs='10j'
execute ' noremap <C-Down> '.s:tendownlhs
execute 'inoremap <C-Down> <C-o>'.s:tendownlhs
. Now without command mode as this is tricky and likely useless.
If it is okay for the mapping to end up in normal mode, you could combine a for loop with <C-\><C-n> mappings. <C-\><C-n> switches from any mode to normal mode.
For example, this allows switching panes with Alt-{h,j,k,l} from any mode:
for map_command in ['noremap', 'noremap!', 'tnoremap']
execute map_command . ' <silent> <M-h> <C-\><C-n><C-w>h'
execute map_command . ' <silent> <M-j> <C-\><C-n><C-w>j'
execute map_command . ' <silent> <M-k> <C-\><C-n><C-w>k'
execute map_command . ' <silent> <M-l> <C-\><C-n><C-w>l'
endfor
noremap maps in Normal, Visual, and Operator-pending mode
noremap! maps in Insert and Command mode
tnoremap maps in Neovim's Terminal mode
I'd like to use two "controls" as a toggle key to switch between normal mode and insert mode in Vim. So I add the following two lines into my .vimrc
nmap <C-><C-> i
imap <C-><C-> <ESC>
But it doesn't work. What's wrong with the above two lines?
It seems you are trying to map Ctrl+Space to toggle insert mode.
nnoremap <C-space> i
imap <C-space> <Esc>
(Came from this Vim tip (marked obsolete, but there's a link to a more rich document on avoiding which includes the tip).)
Remember that this is not guaranteed to work across all terminals and platforms. Some terminals and platforms may eat a given Ctrl+something shortcut, while others don't, so find one that works in your environment.
nnoremap <silent><C-space> :startinsert
inoremap <silent><C-space> <C-O>:stopinsert
That's definitely not going to work. You could use an F key instead.
nnoremap <C-SPACE> i
inoremap <C-SPACE> <ESC>l
works perfectly with GVIM 7.4