Vim determine what mappings are assigned to a key - vim

My 'O' command (enter a new line above this one and enter insert mode) is slow to take effect. It seems like this is because there is some other mapping, such as 'OP' , and that vim is waiting when I hit 'O' to see if I want 'O' or 'OP'. I don't know what set this mapping, it's not anything in my .vimrc. I'd like to find it and change it so I can go back to having snappy 'O' commands, but I don't know how to find it.
Is there a way to see a list of what mappings a key is involved with? Note: I do not want to reduce my timeout setting.
Thanks

:verbose map O
tells you which mappings start with O. That usually gives you the right hint about mappings that execute only after a short delay. For the O mapping in particular, you may also suffer from the problem described in delay before o opens a new line.

Related

why vnoremap a A takes seconds to complete?

i have this in my config file:
nnoremap a a
vnoremap a A
and pressing a in visual mode takes 2 seconds to do the action
why is this happening? is this a bad practice?
This problem is being caused by the fact that you have something else mapped (either in visual mode or in all modes) that starts with 'a'.
Imagine I map 2 different commands in my .vimrc (or as you called it, config file)
vnoremap a A
vnoremap ab D
Here, when I press 'a' in visual mode, I want it to append text.
When I press 'ab' I want it to delete a row for me instead.
I now reach over to my keyboard and I press 'ab'. How does vim know I wanted to delete a line and not just append the letter 'b' to the text? Both require the same keypresses.
So to tell the difference, when I hit the 'a' key, vim waits a second to see which command I choose, If I press 'b' quickly it will realize that this is actually the instruction 'ab' which means 'D' which means delete.
If I press 'a' and wait a second, vim will accept that I was issuing the instruction 'a' which means 'A' which means append. I then hit 'b' and the letter 'b' is appended to the text.
If you want it to stop, you will have to go through your .vimrc and change your mappings to not overlap (start with the same letters) as much, or you can type
:h leader
in vim and learn about mapleaders, which will make it much easier for you to plan your mappings. I have my mapleader set to space personally but many people also like to use commas or some other key of their choosing.
tldr: Vim is waiting a second to see if you are going to press another key and issue a different command

Translucent Vim map for short manual about keys after it

In (Neo)Vim, I want to configure a key as if it passed through one-way mirror.
For example, in normal mode, when I type <Leader>, the command :echo "w: Separate Window f: Open File c: Configure Settings" run (so I can get help from at the bottom of the screen), but the <Leader> still has influence on following keys --- such as w, f, c, and so on --- and <Leader><KEY> works properly.
I mapped lots keys with <Leader>, so it will be very helpful for me to display a short manual about keys follow <Leader> at the bottom of the screen when I type <Leader>.
Thanks.
First, <leader> is not a special key at all. It's a placeholder expanded by Vim to the current value of mapleader whenever it is used. Assuming your mapleader is , (as instructed in :help mapleader), the mapping below:
nnoremap <leader>f :find *
will be registered as:
nnoremap ,f :find *
Second, when Vim detects that the key you just pressed is part of several custom or built-in commands it waits a bit for you to press more keys in order to decide what to do. With , as your mapleader, pressing , will always puzzle Vim because , is an actual command in its own right and you have a bunch of custom mappings starting with ,. In this situation, Vim waits for a full second before deciding you actually wanted , and not ,f.
Third, you would almost certainly need to write a completely separate mapping mechanism for achieving you idea. A mechanism that would:
listen to key presses,
trigger a specific function when you press <leader>,
that prints something helpful in the command-line,
and waits indefinitely for another key to be pressed.
This doesn't sound trivial at all. Did you take a look at the "scripts" section of http://www.vim.org?
Four, the whole point of a mapping is to map a common action to an easy to remember shortcut. You definitely have too many mappings if you can't remember all of them. Maybe it's time to reconsider the root issue instead of looking for a workaround?
You can do that with the following mapping:
:nnoremap <Leader> :echo "w: Separate Window f: Open File c: Configure Settings"<Bar>call feedkeys((exists('g:mapleader') ? g:mapleader : '\'), 't')<CR>
This uses feedkeys() to send the <Leader> again after the help has been shown. However, this solution has the key timeout downsides already mentioned by #romainl: The message will only appear after the 'timeoutlen' delay, and then you have to press the correct mapping key within 'timeoutlen'.
alternative
I would rather print the help on <Leader><Leader>. There are no timeout issues here. If you have pressed <Leader> and then fail to remember what's the next key, just press <Leader> again, read the help, and then start again with the (now memorized) full mapping!

In Vim, how can I switch the functionality of 'i', 'j', 'k' and 'h'

I am 5 or so weeks into learning Vim for development, and while things are starting to sink in, I have one personal issue that is frustratingly still tripping me up.
I am constantly using 'i' to move 'up.' 'k' is down, 'j' and 'l' are left and right respectively.
I know this all comes from playing too many years of PC games where these were direction movements. Honestly, I don't want to fight it anymore, and I would rather do what is extremely natural for me. How can I remap these so the above are the direction movements and 'h' is insert?
2 issues: How can I permanently store these in my local settings for development on my own machine, and how can I also quickly switch these when I am on someone else's machine for the time I am working on it. I mean, is there a set of commands that I can invoke and then they be erased when I shut Vim down?
DON'T DO THAT MAPPING! It is even worse than using arrow keys
first of all, I would suggest you learning the vim way to move your cursor.
Back to your question, remapping h,j,k,l is easy. However, it makes no sense to do those mapping (and you shouldn't do it), it won't make you work faster, it just drives you crazy when you use your vim. You know, editing with vim doesn't mean
in NORMAL mode, just do cursor moving
entering INSERT mode, do actual text editing
back to NORMAL mode to save the file
There are a lot of vim magic you could do in NORMAL mode (that's why it was called NORMAL). with your mapping, you will really have headache when you want to apply those. Let's take an example. Say you have already set mappings for your needs. i,j,k and h. We have a line:
function foo (String arg)
when your cursor is between (and ), you could type ci( to change the stuff in brackets. However your i means k. What would happen? the current line and the line above it will be removed!! Well you could say, you will remember to type chw, dhw,ch(,dhw... So you have to remap those keys also in your mind. :)
Also when you want to "borrow" someone else's .vimrc, or use some plugins, you will have problem too.
You see the point by the simple example above. So please think it twice before you remap those basic keys.
Put this in a file called ~/.vimrc:
noremap i k
noremap k j
noremap j h
noremap h i
If you're using vim on someone else's computer, you can type the same four commands at the prompt (type : before each, and enter after each), and the settings will apply for 1 session only.
Also, no is short for noremap, so you can shorten the whole sequence to
:no i k<enter>:no k j<enter>:no j h<enter>:no h i<enter>
when you're using someone else's computer.

How can I map these five keyboard actions to a single key in Vim?

I want to map a key so that it will do the following actions in Vim. Suppose I am editing a file; I want it set up so that if I press F2, I will accomplish the same thing I would if I did the following:
press ESC
type colon (:)
type w
press Enter
press ESC again
type i to go back to insert mode
Is this possible?
Yes it's possible, but it doesn't do what you want if the cursor is at the end of line.
To get file saved on F2 in insert mode, use the following mapping:
:imap <F2> <C-O>:w<CR>
Literal answer: Yes. You can use this:
:inoremap <F2> <Esc>:w<CR>I
but it won’t do exactly what you want (the cursor will be at the wrong place).
Anton beat me to the less literal (but correct) answer.
The best answer, though, is this: Don't use Vim incorrectly. You should never spend so much time in insert mode that you need a shortcut to get out of it, save the file, and then get back in. With all other editors, you’re in “insert mode” all the time, and only temporarily pop into a menu or dialog or whatever; in Vim, you should learn to reverse this. Only pop into insert mode to edit or add something; never use arrow keys to move the cursor while in insert mode; spend the majority of your time in command (normal) mode, and after a bit of adjustment to the new paradigm, you will find that your editing speed has increased.
Writing the mapping is almost easier than your description.
First, you need to determine from which mode the mapping will be used, because that determines what :map variant you will use. You’ll probably want to use this in insert mode, so you’ll use :inoremap.
The format of the mapping is:
:..noremap {keys} {rhs}
You want <F2> (see :help key-notation) for keys. For {rhs}, just concatenate the keys listed in your description.
To persist the mapping, add it to ~/.vimrc. (See :help vimrc.)
P.S. The alternative given by Anton Kovalenko is probably better for what you’re trying to do, but here I’ve given you the general recipe for future key mappings.

In some moments vim works noticeably slowly

In some moments vim works noticeably slow. When I'm in normal mode in 100 line file, type "O" (uppercase letter o) it appears about 1-2 seconds and only then above of current line new empty line is created in insert mode (that is normal behavior). And I want to know possible reasons why this happens...
I have quite powerful computer, So the problem is not in computer.
Are you hitting <Esc> then O in very rapid succession? If so, you are seeing the delay due to certain terminal escape sequences beginning with <Esc>O. Vim has to wait to see if you are actually typing one of those sequences.
To see this for yourself, in insert mode type <Esc>OA and your cursor should move up. Pressing <Ctrl-v><Up> in insert mode will show you the escape code generated.
Type :map O
If you have a normal mapping starting with a capital O, it might be possible that Vim is waiting for a timeout to be sure that you are not starting to type a complex command.
Typically, the timeout default is of 1 second.
See :help timeout and :help timeoutlen.
If you do have a mapping starting with O, you can find where it is defined with :verbose map. You can then disable it or modify it (or remove the plugin defining the mapping).
May be you have a redefined key binding that starts with "O"... so VIM must wait to see if you are going to type the following keys

Resources