Alert sound in Vim after placing comments after key mapping in .vimrc - vim

I'm trying map the following keys to switch splits in Vim.
nnoremap <s-j> <c-w>j " Shift + j to switch the split below
nnoremap <s-k> <c-w>k " Shift + k to switch the split above
nnoremap <s-h> <c-w>h " Shift + h to switch the split on the left
nnoremap <s-l> <c-w>l " Shift + l to switch the split on the right
This works well except I hear an alert sound when performing the switch. For example, there is no alert sound when pressing Ctrl+w and then j, but there is an alert for Shift+j.
I suspect there is an error happened, and don't like just turning off the alert sound without understanding.
Maybe the remapping does more than just Ctrl+w + j?
Does any vim expert have some thoughts?

Removing your comments will likely fix this problem. See :help :quote.
Also…
Why <s-j> when you could simply use J?
:help J, :help L, :help H, and :help K are all very useful commands. Are you sure you want to override them?

Related

switching windows with <leader> instead of ctrl

I recently start using leader key in vim, and mapped to space
Earlier I use window switching with ctrl + {h, j, k, l}
nnoremap <C-h> <C-w>h
nnoremap <C-j> <C-w>j
nnoremap <C-k> <C-w>k
nnoremap <C-l> <C-w>l
And now I was trying something like
nnoremap <leader>h <C-w>h
nnoremap <leader>j <C-w>j
nnoremap <leader>k <C-w>k
nnoremap <leader>l <C-w>l
I already remove any prior mapping with the above mentioned key.
Now issue I am facing is, with ctrl key i can switch the window panes without lifting finger from ctrl While with space I have to press Leader and {h, j, k, l} simultaneously and then I have to press leader and {h, j, k, l} if want to go to some other window pane.
What I am trying to say is, let say my window vertically split b/w two and I wish to go to second window and come back to original.
With ctrl key: ctrl + l + h
with leader key: space + l, space + h
I want my leader key work exact same as ctrl, since it is convenient to use
Is this possible?
Also If you have some advice for newbie like me, I will be glad to hear it.
What you call "leader key" is not a special key at all and certainly not a modifier, like Ctrl, Shift, or Alt so you can't make it work like one. Unless they involve one of those modifiers, Vim mappings are always sequential: you press the first key, then the second one, etc.
If you absolutely want chord-like mappings, you could try arpeggio.

vimdiff and move among left and right pane

I am using vimdiff for the first time. Online I found written that to move from the left pane you use CTRL + w + Left or right arrow
This does not work for me. But I see that if I press just CTRL + w and press w for a sec and let it go, it switches pane after ~500ms.
Is this how it is supposed to work? Am I doing something wrong?
Ctrl+w and right and left arrow can be used to move between any split windows on vim, not only vimdiff splits.
These keys do work here on cygwin; also, Ctrl+w w also moves to the next window, but without the delay you mentioned.
It is possible that you have mapped these keys in your .vimrc or via some vim plugin. You can check this with :map w, :map <left> and :map <right>.
As moving between windows is something that you use often, you may consider using the following mappings:
nnoremap <C-J> <C-W>j
nnoremap <C-K> <C-W>k
nnoremap <C-H> <C-W>h
nnoremap <C-L> <C-W>l
Then you can use Ctrl+h and Ctrl+l to move left and right, without moving your hands from the home row. And the nnoremap will ensure that these works despite of any other mappings that you may have.
Press Ctrl + W and then (after releasing Ctrl + W) press the arrow keys to change the pane.
It is very useful to use set mouse=a in your .vimrc file. It gives you possibility to switch between windows using mouse. Additionally you can resize windows using it.
If you prefer to use keyboard I have also mapped arrow keys in .vimrc in this way:
map <C-Left> <C-W>j
map <C-Down> <C-W>k
map <C-Up> <C-W>h
map <C-Right> <C-W>l
To move among left and right pane, Press ctrl+w and then ctrl+r. This is both left and right vice-versa.
You can also use :wincmd w for next window, and :wincmd W for previous window.
The :wincmd is especially useful when ctrl+w is captured by the environment. For example see: https://stackoverflow.com/a/73749587/811335

Vim - How do I use a key mapping command like <C-L>?

I'm almost certain that someone else has also had this question, and this may be a repeat, but I'm not sure what to call a command like <C-L> and so I haven't had any luck finding an existing question. If this is a duplicate, please redirect me.
The question arises from a vimrc section that reads like this:
" Map <C-L> (redraw screen) to also turn off search highlighting until the
" next search
nnoremap <C-L> :nohl<CR><C-L>
So what combination of keys do I press (in which mode) to input a <C-L> mapping?
in this line:
nnoremap <C-L> :nohl<CR><C-L>
nnoremap means normal no-recursive map <C-L>... which means, if you press ctrl + l in NORMAL mode, the mapped key-strokes would be applied.
<C-L> means ctrl + l
if you type
:h control
you can see the keycodes:
<C-...> control-key *control* *ctrl* *<C-*
The capital "C" character in <C-L> represents the control key while the capital "L" character represents a "L" character. So pressing Ctrl+L in normal mode should invoke the mapping.

Best of both worlds: arrow keys for cursor movement or flipping through buffers

I really like this vim trick to use the left and right arrows to flip between buffers:
"left/right arrows to switch buffers in normal mode
map <right> :bn<cr>
map <left> :bp<cr>
(Put that in ~/.vimrc)
But sometimes I'm munching on a sandwich or something when scrolling around a file and I really want the arrow keys to work normally.
I think what would make most sense is for the arrow keys to have the above buffer-flipping functionality only if there are actually multiple buffers open.
Is there a way to extend the above to accomplish that?
I'd rather have a completely different mapping because:
cursors are really useful, and not having them because you have a hidden buffer will annoy you a lot
some plugins use <left> and <right> because they are less obfuscated than l and h; those plugins are likely to break with such mappings
Anyway, you can try this:
nnoremap <expr> <right> (len(filter(range(0, bufnr('$')), 'buflisted(v:val)')) > 1 ? ":bn\<cr>" : "\<right>")
nnoremap <expr> <left> (len(filter(range(0, bufnr('$')), 'buflisted(v:val)')) > 1 ? ":bp\<cr>" : "\<left>")
To see documentation on the pieces above:
:h :map-<expr>
:h len()
:h filter()
:h range()
:h bufnr()
:h buflisted()
I use alt-direction to switch between buffers.
nmap <A-Left> :bp<CR>
nmap <A-Right> :bn<CR>
If you modifying hl's defaults, then the arrows would feel more useful. (Like changing whichwrap to allow hl to go past the end of line.)
I do something similar with jk to make them different from my arrows:
" work more logically with wrapped lines
set wrap
set linebreak
noremap j gj
noremap k gk
noremap gj j
noremap gk k
That will wrap long lines and jk will move to what looks like the line below. (If you have one long line, then you'll move to the part of that line below the cursor.) Great for editing prose or long comments.
See also
help showbreak
I map Tab and Shift+Tab to switch buffers when in normal mode (makes sense to my brain and the keys are not doing anything useful otherwise).
Add this to your .vimrc
" Use Tab and Shift-Tab to cycle through buffers
nnoremap <Tab> bnext<CR>
nnoremap <S-Tab> :bprevious<CR>

Auto 'zz' in vim after a jump

After I make a jump to anywhere in the world, whether in the current file or a different file, is it possible to make vim automatically run zz (re-center on current line)?
I want this after things like search, ctrl-o and ctrl-i ... and pretty much any movement other than hjkl.
Thanks.
Voila:
" Center screen on next/previous selection.
nnoremap n nzz
nnoremap N Nzz
" Last and next jump should center too.
nnoremap <C-o> <C-o>zz
nnoremap <C-i> <C-i>zz

Resources