I am trying to do a remapping when I'm in insert mode to insert a comment but am having a tough time figuring out what all the keys map to. What I am trying to do is:
:inoremap leadercspace ==> escleadercspacei
Basically, if I'm in insert mode I want to get out of insert mode to insert the comment (leader+c+space) and then go back into insert mode.
What would the correct :inoremap mapping for this be? What I have right now is:
:inoremap <leader>c<space> <Esc><Leader>c<space>i
But this doesn't seem to work (at least the latter half of it -- it does seem to be executing the mapping command). Note: the plugin I'm trying to remap is:
https://github.com/preservim/nerdcommenter
[count]<leader>c<space> |NERDCommenterToggle|
Toggles the comment state of the selected line(s). If the topmost selected line is commented, all selected lines are uncommented and vice versa.
From vim doc (:help nore):
Disallow mapping of {rhs}, to avoid nested and recursive mappings
In other words, the nore part forbids mapping to be applied to the rhs (right hand side).
So in your case, the <Esc><Leader>c<space>i doesn't trigger the VimCommenter mapping for that reason.
To allow recursion, you can take off the nore:
:imap <leader>c<space> <Esc><Leader>c<space>i
My recommendation is that, instead of creating an insert-mode mapping for this purpose, just use the native Ctrl+O mapping to run a single Normal mode command from Insert mode.
Assuming your leader key is set to the default \, you can use:
Ctrl+O, \, c, Space
You'll be left in Insert mode at the end of this sequence.
The advantages of this approach over an insert mode mapping are:
You don't need any extra configuration, since Ctrl+O is a native Vim command.
This works for any Normal mode command, so you don't need to add extra mappings for other commands you might want to be able to access from Insert mode.
Adding a multi-character mapping in Insert mode starting with <Leader> means Vim will always pause and hold if you insert the leader character. In this case, it will also pause when you insert <Leader> and c. I find that avoiding this kind of mappings of otherwise printable characters is usually best.
Related
Sometimes I want to replace some text in multiple lines but not in the whole line, so I toggle visual mode on and highlight the text I want to replace. But then, I need to add this annoying \%V that I always forget for it to replace the text just in the selection and not in the whole line
:'<, '>s/\%Vold/new/g
Is there a way to map the normal replace command
:'<, '>s/old/new/g
To the one shown above so that I don't need to remember that V every time?
Thank you
I tried using cmap command in the vimrc file in the following way:
cmap '<, '>s/* '<, '>s/\%V*
But this of course doesn't work, because instead of the asterisk I should input the text I want to search and replace to end the search and replace command.
There are three basic parts to a mapping:
the mapping command,
the "left-hand side" (LHS),
and the "right-hand side" (RHS).
The LHS is the key sequence/combination that you want to press and the RHS is a different key sequence that you want Vim to "press" instead of those in the LHS. You "map" one key sequence to another.
The RHS is, fundamentally, a macro: a sequence of keystrokes, "just" like if you pressed them yourself but without any delay between them. So, the RHS is really just a bunch of keystrokes you are tired of making over and over again.
The main use of mappings is to reduce repetitiveness. You figure that you often press the imaginary sequence sdlusdrosydtsodsd and you map it all to <F5> to be more efficient.
In this case…
The current mode is visual mode so the mapping has to be a visual mode mapping.
This makes :xmap or :xnoremap the proper mapping command. Let's make it :xnoremap.
The repetitive key sequence you want to abstract away is:
:s/\%V
That's your RHS.
(Note that the range '<,'> is automatically inserted for you so it can be ignored.)
The key sequence you want to press instead is, let's say… <F5>.
That's your LHS.
Now that you have your mapping command, your LHS, and your RHS, building the mapping is extremely easy:
:xnoremap <F5> :s/\%V
The following items are useful to me in editing text, and I was wondering if vim had something for this built out of the box (though I didn't see it on the https://vimhelp.org/index.txt.html#index.txt page), or I had to create mappings for it:
Forward-delete a character. This is X in normal mode.
Forward-delete all text to the right of the cursor on the line. This is the inverse of ctrl-u.
Are either of these mappings available? And if not, are there 'standard' mappings for this that are common (for example, how it might be done in another unix program).
Note that this is the keyboard I have -- there is only one delete key (which acts like a normal backspace key) and there is no backspace key:
Note: for forward-delete, I am currently mapping ctrl-d as:
"Ctrl-d to forward-delete when in insert or command mode
noremap! <C-d> <Delete>
However, this interferes with the tab in insert mode (which I don't use) and the help-options in command mode (which I do use!) so I may have to modify this later, or hopefully someone suggests a better solution.
though I didn't see it on the https://vimhelp.org/index.txt.html#index.txt page
If you can't find it in the documentation, then it doesn't exist.
You can use fn+delete for "Forward-delete a character".
"Forward-delete all text to the right of the cursor on the line" is ctrl+k in MacOS, but Vim has its own use for that combo, :help i_ctrl-k so it is up to you to create a mapping for it.
Something like:
inoremap <key> <C-o>ld$
Why I ask:
I use to enter code for example if(condition){}, in following step:
if(){
}
move cursor back into () to complete condition
move cursor into {} to add task
I have read Traversing text in Insert mode, and I add follow code into my $HOME/.vimrc
" key mapping
inoremap <A-h> <C-o>h
inoremap <A-j> <C-o>j
inoremap <A-k> <C-o>k
inoremap <A-l> <C-o>l
now I can use Alt+h and Alt+l, but the rest of two new map had no effect, then I test: Ctrl+oj and Ctrl+ok, both of them work.
Is there any mistake when I do the key mapping?
How to check if my new mapping is conflicted with other or not?
UPDATE: 2nd/Nov/2016
I buy a new keyboard with cursor key...
Install auto pair
However, I found one interesting thing, when I in Linux, there is ok for all above mapping just except Alt+h, because it conflicted with the ubuntu current open window help menu. I only meet my problem when I use ssh via MobaXerm application.
I have read Traversing text in Insert mode, and I add follow code into
my $HOME/.vimrc
You should carefully read the accepted answer for that answer, specially this part:
The right way is to press Esc, go where you want to do a
small correction, fix it, go back and keep editing. It is effective
because Vim has much more movements than usual character
forward/backward/up/down. After you learn more of them, this will
happen to be more productive.
The answer where you borrowed the mappings also mentions this:
Notwithstanding what Pavel Shved said - that it is probably more
advisable to get used to Escaping Insert mode - here is an example set
of mappings for quick navigation within Insert mode: (...)
Anyway, if you want to understand the problem with the Alt+j and Alt+k, you should first ensure that the mapping is still defined in Vim (they could have been erased or overwritten). You can use :imap to list them; try these:
:imap <A-j>
:imap <A-k>
If your mappings are correctly defined each one will list its target (e.g.: * <C-O>j). In this case you should check if Vim is receiving these combinations correctly; try inserting then in the text (insert mode) by using Ctrl+V (or Ctrl+Q if you mapped that to paste from clipboard) and the Alt combinations. You can get more details at the Vim FAQ "I am not able to create a mapping for the key. What is wrong?".
Edit:
If your issue is mainly related with closing parenthesis, then there are several other options, which I believe that are more practical. I quick internet search returned the following:
SO - Automatic closing brackets for Vim
Vim wiki - Making Parenthesis And Brackets Handling Easier
plugin - Auto Pairs
I also think that you misuse Vim.
I know that the question was about something else but here is my idea of how you should move around in vim.
You have 3 steps:
1. Insert some empty loop / condition
2. Insert a condition
3. Insert a body of the loop / condition
This should represent 3 changes, each separated by leaving the insert mode.
To do it properly you can perform step 1 and then leave insert mode by using either Esc or Ctrl+[ (with the second one- which is also vim default- you do not have to reach for escape key).
Then you should navigate to the place where you want to insert your change using h,j,k or l and follow it by starting insert mode.
There are several ways to start insert mode:
I - start insert mode at the beginning of the line (omitting whitespaces at the beginning)
i - start insert mode before the cursor
a - start insert mode after the cursor
A - start insert mode at the end of the line
s - change the sign under the cursor (can be combined with visual mode)
c - change text from under the cursor until place you have specified with the movement (e.g. ce - change until the end of the word, cl - the same as "s")
C - change everything from cursor until the end of the line
S - replace the whole line
o - start insert mode in the new line below
O - start insert mode in the new line above
In my .vimrc I created the following mapping. Basically I want VIM to insert some text when pressing <leader>c.
The mapping is defined as follows:
map <leader>c iHELLO WORLD<Esc>;w<CR>
The second part of the map works (it exits insert mode correctly), but it takes about a second for it to actually go from normal mode to insert mode and insert the text.
I imagine this might be related to setting in my .vimrc. You can take a look at that here.
You probably have another mapping that shares a common prefix (<leader>c), and so Vim is waiting to see if you are going to type out any disambiguating characters before assuming you meant "just <leader>c".
You can avoid this by picking a non-ambiguous mapping, changing the other mapping, or reducing 'timeoutlen' from its default value of 1000 (ms).
To find out what the conflicting mapping might be, try :map <leader>c and inspect the output.
You must have multiple <leader>c-mappings. Vim has to wait to disambiguate for timeoutlen ms to see if you’re going to type <leader>cx (where x is anything`).
You can see your related mappings with verbose map <leader>c. Then either remove the others or extend yours to be something like <leader>ch (h as “Hello” mnemonic).
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.