How do I map keys not simultaniously in vim? - vim

I don't know if sequentially is the right word, but I'd like to nnoremap Ctrl+t to Ctrl+y and then a comma. Is that possible or do I need to do this another way?

Yes, add
nnoremap <C-t><C-y>, a
to your ~/.vimrc, where <C-t> and <C-y> are ctrl+t and ctrl+y, respectively, and a is whatever action you want to have the keys map to.

You map the keys you want to press to what you want to happen.
If you want Ctrl+t to happen when you press Ctrl+y then ,, then your mapping should look like this:
nnoremap <C-y>, <C-t>
If you want Ctrl+y then , to happen when you press Ctrl+t, then your mapping should look like this:
nnoremap <C-t> <C-y>,
The concept of mapping is introduced in chapter 40 of the user manual, which you should have read, and further discussed under :help mapping.
But, judging by your comments under the other answer, creating a normal mode non-recursive mapping is not the solution to your problem.
First, because the plugin exposes <C-y>, in normal, visual, select, and insert modes so you must cover all of them:
" insert mode
inoremap <C-t> <C-y>,
" visual mode and select mode
vnoremap <C-t> <C-y>,
" normal mode
nnoremap <C-t> <C-y>,
Second, because <C-y>, is itself a mapping so you want your custom mapping to be recursive:
imap <C-t> <C-y>,
vmap <C-t> <C-y>,
nmap <C-t> <C-y>,
While the mappings above work, the whole idea a bit shortsighted because:
The <C-y> in <C-y>, is a "leader" that is common to all the mappings exposed by that plugin. You can now press <C-t> instead of <C-y>, but all the other functionalities of that plugin are still behind that <C-y> leader and still follow that <leader><key> pattern so your system is now inconsistent.
You are overriding <C-t> in normal and insert modes, where it is pretty useful.

Related

Remapping <ESC> breaks <S-TAB> mapping in vim

I had the following mapping on my vimrc
nnoremap <TAB> gt
nnoremap <S-TAB> gT
Then I mapped ESC to clear highlights as:
noremap <silent> <ESC> :noh<return>
And then <S-TAB> wont work anymore. If I remove <silent> from <ESC> mapping I see :noh when I press <S-TAB>. I don't know if <S-TAB> and <ESC> has something in common. I'm on Linux, using vim on gnome-terminal.
You're right in assuming that <S-TAB> and <ESC> have something in common. S-TAB is an escape-prefixed keycode.
The ESC keycode is ^[, while S-TAB is ^[[Z. You can see the first part of the S-TAB key code matching the ESC keycode.
See a full table of combinations here.
So you just can't remap the escape key while also remapping one of the key codes including it. Either pick a different key to clear highlights or a different way of switching tabs.

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.

VIM ctrl+tab to change tab shifts cursor backwards

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>

gvim: make <S-Up>, <S-Down> move in screen lines

I chose to use gvim as my primary text editor, but still would like it to behave more like other visual editors I'm used to. One aspect of that is that when I have wrap enabled (set linebreak) and use the arrow keys <Up> and <Down> in insert mode, I'd like to move the cursor to the previous / next screen line, not logical line. This can be achieved using the mappings:
inoremap <Up> <C-O>gk
inoremap <Down> <C-O>gj
...and everything is fine.
Except, in select mode. While using <S-Right>, <S-Left> works as expected, <S-Up> and <S-Down> still move in terms of logical lines. On http://vim.wikia.com I found the following suggestion for additional mappings:
noremap <S-Up> vgk
noremap <S-Down> vgj
inoremap <S-Up> <C-O>vgk
inoremap <S-Down> <C-O>vgj
The two latter mappings now enable that when I start a selection by pressing <S-Down> in insert mode, I get a selection from the previous position to the same position in the next screen line. But when I already have a selection (already am in select mode), pressing <S-Down> moves one line down but loses the selection.
I would expect to achieve this it would be necessary to have specific mapping for select mode (snoremap), but wasn't able to figure out how to do it.
Because of the discussion with glts whether select mode is useless or not, maybe some background information is in order: Select mode appears to be vim's attempt to provide something close to the selection behavior found in most other visual editors on MS Windows, Mac OS, and even Linux, which in turn is inspired by IBM's CUA. Since it is only really useful with the accompanying keyboard mappings ^C, ^X, ^V, it is meant to be used in conjunction with mswin.vim which provides these mappings. My question is motivated by an attempt to complement these mappings such that select mode works as expected also for wrapped text.
For Select mode, if that is really what you mean, these mappings would work:
vnoremap <S-Up> gk
vnoremap <S-Down> gj
imap <S-Up> <Esc>gh<S-Up>
imap <S-Down> <Esc><Right>gh<S-Down>
nmap <S-Up> gh<S-Up>
nmap <S-Down> gh<S-Down>
Note the gh command (Select mode) instead of v (Visual mode).
But be warned that – and this is an assumption on my part – the general populace of Vim users shun Select mode, seeing as it runs counter to the Vim way.
Visual mode is much more powerful, since in addition to replacing text, you can also yank it into a register, make it uppercase or lowercase, change the extent of the Visual selection, etc. etc. Have a look at :h vim-modes.
Here is what I came up with myself:
Make <Up> and <Down> move to previous / next screen line. (In insert mode, <C-O> switches to normal mode for one command. In normal mode, gj and gk are the 'move by screen line' commands.)
inoremap <Up> <C-O>gk
inoremap <Down> <C-O>gj
Same for <S-Up> and <S-Down> in insert mode, entering select mode. (In normal mode, v enters visual mode. gj and gk work also in visual mode. In visual mode, <C-G> enters select mode.)
inoremap <S-Up> <C-O>vgk<C-G>
inoremap <S-Down> <C-O>vgj<C-G>
Same for <S-Up> and <S-Down> in select mode. (In select mode, <C-O> switches to visual mode for one command.)
snoremap <S-Up> <C-O>gk
snoremap <S-Down> <C-O>gj

Vim: overloaded mapping for multiple modes

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

Resources