change shortcut to activate a keymap in vim in insert mode - vim

So I'm currently having the problem of activatig caps-lock during insert mode, exiting insert mode with active capslock and having this nightmare that you are probably familiar with.
So as suggested in this post, I crated a new keymapping:
in /home/username/.vim/keymap/insert-only_capslock.vim you can find:
let b:keymap_name = "CAPS"
loadkeymap
a A
b B
c C
...
x X
y Y
z Z
A a
B b
C c
...
X x
Y y
Z z
and in my ~/.vimrc you can find:
set imsearch=-1
set keymap=insert-only_capslock
set iminsert=0
All this code allows my to go into insert mode, type Ctrl-^ and activate capslock and as soon as I exit insert mode, the capslock goes back to normal.
MY QUESTION =
how can I change this shortcut Ctrl-^?
I can't find any resource on internet. The only post I found that explains something really basic is this one, but it doesn't really cover how to change the shortcut to activate a custom keymap.

how can I change this shortcut Ctrl-^?
:help i_ctrl-^ is a regular insert mode command (not a "shortcut") which can be mapped like any other:
inoremap <key> <C-^>
It is up to you to decide what key combination to use in place of <key>.

Related

H doesn't take cursor to top of screen

I've read that H should make the cursor go to the top of the current screen. Also L doesn't goto bottom of current screen. However, M does goto middle of current screen. How can I fix this? Or find out hat the actual mapping is.
HML are not mappings; they are hardcoded commands. But some plugins or your own configuration can create mappings that override those default commands.
This command tells you where a mapping was last set and what it does:
:verbose map H
It sounds that there is mapping that overwritten the default H, L function.
you can use :verbose map H to find out the current key mapping on H and where it was done.
It's likely that a plugin has remapped those two keys. You can find out with
:verbose map H
:verbose map L
In addition to the normal suspicions about an interfering mapping given in other answers, you may have your 'scrolloff' option set to some non-zero value. When this option is set to N, Vim always keeps N lines of text between the cursor and the top/bottom of the window. As you scroll, this means that the text scrolls when the cursor reaches a line within N lines of the top/bottom, but when using H or L, it means the cursor will only move to within N lines of the top/bottom of the window.
In particular, if 'scrolloff' is set higher than the window height, then your cursor always stays in the middle of the screen as you scroll, and H/L do nothing at all.

Override vim command for yank

The shortcut Y, for some reason, does not operate the way D and C
do. Instead of yanking from the current position to the end of the
line, Y yanks the whole line. Y does the same thing as yy.
Is it possible to override this functionality ?
I want Y to acts as y$
Use a map in Normal mode:
:nnoremap Y y$
Learn how to look up commands and navigate the built-in :help; it is comprehensive and offers many tips. Pertaining to your question, :help Y offers this:
If you like "Y" to work from the cursor to the end of line (which is more logical, but not Vi-compatible) use :map Y y$.

Vim keybinding invoked by two keys

What I want to do: How can I create a custom keybinding in vim to write "hello world" while in insert mode, by first holding down 'w' then pressing '2'?
Note, I don't want my command invoked by pressing w then 2 -- the w key must be held down then 2 pressed. Note also, that if the 2 key is first held down then w pressed, the command should not be called.
The reason for wanting w to first be held down then pressing 2 is that, if I wanted to type "w2" literally I could do that by pressing+depressing w then pressing 2
Did you try TextExpander or, even better, TypeIt4Me?
Anyway, the exact mechanism you describe is not possible in Vim in insert mode natively. Since w is not a modifier key you can't keep it pressed and expect Vim to register the second keypress.
The closer I could think of is arpeggio which lets you map things to simultaneously pressed keys.
type :inoreab w2 hello world or write inoreab w2 hello world in your .vimrc
then when you are in INSERT mode, type w2<space>, the two words will be up there.
leader key (<leader>) could be anykey, user can map it by himself. default is \. btw, I don't think it is a good idea to map w as leader key. it conflict with w (word forward) in normal mode.
I mapped , (comma) as leader key.
more detail pls check :h <leader>
EDIT for OP's comment
Can I somehow do this without having to press <space>?
Yes you can.( However I don't recommend that way.)
you could go mapping way to skip the space.
:inoremap w2 hello world
note that if you have that mapping, in INSERT mode, anytime you press w2 "hello world" will be immediately there. I am not sure if it is convenient for you, for me not.

Write to two registers at the same time in Vim

I'm using:
set clipboard=unnamed
So that cut and copied selections go to the * register, which is conveniently linked to the x clipboard (I'm using Linux).
I would like to have it also in the + register to get both middle-mouse-pasting and Ctrl-V giving the same results in other applications. I guess this might related to the difference between xclip and xclip -selection c.
For now my workaround is:
noremap Y "+y
So that I could do Yy to copy the current line to + register and then use Ctlr-V in some applications. But then I have to think in advance if I will use the mouse or Ctrl-V.
Is there a way I could have the + and * registers have the same value when I yank or when I select some text with the mouse?
I think you want to
:set clipboard+=unnamed,unnamedplus
From the help:
unnamedplus A variant of "unnamed" flag which uses the clipboard
register '+' (|quoteplus|) instead of register '*' for
all operations except yank. Yank shall copy the text
into register '+' and also into '*' when "unnamed" is
included.
Note that you need a fairly new Vim 7.3 (patchlevel 151?) for this option value.
Thinking out of the box, you can install autocutsel which sinchronizes both clipboards.
As discussed in the comments, here's a solution:
vnoremap Y ygv"+y
However that does not work, only the other way around:
vnoremap Y "+ygvy
Here's what it does:
noremap Y: Map to the Y key, using default behavior of keybindings. :h noremap
"+y: Yank into + register.
gv: Re-select previous visual selection.
y: A normal yank into the * register (in Linux).
Glad I was able to help you solve it. :)

How do I set up vim to automatically change my background color depending on whether CAPS LOCK is on or not?

I'm an avid vim user and have started to write some SQL code recently. I like to write my SQL statements in CAPS and sometimes forget to switch CapsLock 'off' and I then quickly wreak havoc on my code before I realise what's happening.
I have so far not found any way to tell whether the CapsLock key is on other than looking at my keyboard (which requires me to look away from the screen which I consider a big delay).
Ideally I would like vim to automatically change my background colour whenever CapsLock is 'on' but I'd be willing to settle for some other on-screen indicator of CapsLock status as a compromise.
The person who suggested mapping lowercase letters to upper case is on the right track but we need to add some more details. The trick is to map SQL keywords to upper case. Then you can type them in lowercase and vim will convert them to uppercase for you. This is done using abbreviations, not mappings. For example, if you create the abbreviation
:iab ATT American Telephone and Telegraph
then every time you type 'ATT' in your code, Vim will automatically translate it to 'American Telephone and Telegraph' as soon as you hit the space bar. (Try it! It's fun!)
So, create a new file and start entering abbreviations for all the SQL keywords.
iab select SELECT
iab like LIKE
iab where WHERE
...
Since you only want these abbreviations to apply when you're editing SQL source, save this file to ~/.vim/after/ftplugin/sql.vim The commands in this file will be executed when Vim detects it has opened a SQL file.
The file type detection only works if you turn it on so make sure your .vimrc has lines like:
set nocompat
filet detect plugin on
Once you get this working you should save time and have less carpal tunnel from holding down the shift key all the time!
The closest you can get is Kent's answer. Vim cannot see CapsLock (or NumLock, or ScrollLock, or any other modifier key by itself) because the status of these keys is not sent across a terminal.
Note that, in theory, gVim could see these modifiers, but in practice it does not. gVim's keyboard handling is superior to vim's though in many other ways.
What I did is bound my caps lock key to the "Compose" key, so my capslock key is effectively missing and I just force myself to use SHIFT instead. :)
You might want to try it, it may sound masochistic, but its better in the long run. Some argue the Caps lock is redundant.
The best I can give you is to have vim toggle the background color whenever the CAPSLOCK
key is hit in vim.
Vim can't detect the CAPSLOCK key alone. What I can give you is a mapping so you can use some other key (in this example, F3) to act like the CAPSLOCK key for insert mode, and change the background color when all-caps is on. Hopefully this will give you the functionality you need.
Put the following in your ~/.vimrc or in the appropriate ~/.vim/ftplugin/<filetype>.vim :
" let the case be toggled in normal mode
map <expr> <F3> ToggleInsertCase()
" let the case be toggled in insert mode
imap <expr> <F3> ToggleInsertCase()
let toUpper = 0
func! ToggleInsertCase()
let g:toUpper = 1 - g:toUpper
if (g:toUpper == 1)
highlight Normal ctermbg=Blue " the background color you want when uppercased
" convert all the letters to uppercase in insert mode
let i = char2nr('a')
while i <= char2nr('z')
let c = nr2char(i)
exe 'inoremap' c toupper(c)
let i = i + 1
endwhile
else
highlight Normal ctermbg=Black " the background color you want normally
" let letters be as normal in insert mode
let i = char2nr('a')
while i <= char2nr('z')
let c = nr2char(i)
exe 'iunmap' c
let i = i + 1
endwhile
endif
" don't insert anything when this function is called in normal mode
return ""
endfunc
This borrows some from Tim Pope's capslock.vim.
If you really want to use the CAPSLOCK key for this, depending on your platform, there's a bunch of free apps that let you remap
your CAPSLOCK key to something else, so you could set it up so that when you hit CAPSLOCK, vim (and everything else) got F3. Or whatever.
This would interfere with other apps getting the CAPSLOCK key though (unless the key-remapper app is clever), so that could be troublesome.
Why not have a filetype plugin for .sql files that contain imap commands to convert all your keystrokes to caps. That way, you never have to toggle the actual capslock key on the keyboard and things will be fine when you go back to code.
Add the following in the file $VIM/vimfiles/ftplugin/sql_too.vim :
imap a A
imap b B
imap c C
imap d D
imap e E
imap f F
imap g G
imap h H
imap i I
imap j J
imap k K
imap l L
imap m M
imap n N
imap o O
imap p P
imap q Q
imap r R
imap s S
imap t T
imap u U
imap v V
imap w W
imap x X
imap y Y
imap z Z
I agree with Kent, and remap the Caps Lock key to the control key. I used AutoHotKey to do this on Windows boxes, but there's a registry setting as described here and many other places. If you're not using Windows, you can also easily remap the caps lock key on MacOS and unix X window platforms.
The convenience of not accidentally having caps lock on, far outweighes the few times a year I would actually want it!

Resources