I am using Vim 7.4 in Windows 7.

I am trying to map F2 to the following ex-command,

execute "normal ggf\<tab>"| execute "normal \<c-q>G$d"| %s/ \+//g

using the following mapping command

:nmap <F2> :execute "normal ggf\<tab>" \| execute "normal \<c-q>G$d" \| :%s/ \+//g

but when I press F2, the following shows up

:execute "normal ggf\^I" | execute "normal \G$d" |%s/ \+//g

The <tab> changes to ^I and <c-q> disappears. How can I prevent those changes to execute the original ex-command?


Note: <c-q> is the default visual block selection in Vim 7.4 in Windows.

Input and the result, enter image description here

As you can see the image below <c-q> disappears, enter image description here

  • 1
    I'm not sure what is your problem: ^I is the key code of <tab> so it is normal that it appears like this (see :h i_CTRL-V) and I think the same happens to <c-q>. Now if your problem is that your command appears but is not executed, it is because you didn't put <CR> at the end of the mapping which correspond to pressing Enter after you wrote your command. – statox Nov 2 '17 at 8:37
  • Thank you @statox I realized that <tab> is same as ^I but I was unaware of the <CR>. Unfortunately it still doesn't work. I get a E488 Trailing Error when executing :nunmap <F2> :execute "normal ggf\<tab>"|execute "normal \<c-q>G$d"|%s/ \+//g<CR> and when I execute nmap <F2> :execute "normal ggf\<tab>"|execute "normal \<c-q>G$d"<CR> I get E121: Undefined variable: CR E15: Invalid Expression "normal\<c-q>G$d" Note: <c-q> is used for visual block selection – Stephen Jacob Nov 2 '17 at 9:21
  • 1
    The trailing error is because you only need nunmap <F2> if you want to remove the mapping from the key, no need to specify the expression of the mapping. For the second error I'm not sure but why do you use <c-q> for visual block? You can use <c-v> and if you use <c-q> because you remapped <c-v> then use normal! in your expression so that your mapping are not used. – statox Nov 2 '17 at 10:06

I have figured out the reason based on another vi stackexchange answer. Link.

Apparently the character < in <c-q> needs to be replaced by <lt>, though I do not know why but it solves the current problem. So the new mapping would be:

:nmap <F2> :execute "normal ggf\<tab>" \| execute "normal \<lt>c-q>G$d" \| :%s/ \+//g

Input: enter image description here

Result: enter image description here

And as per @statox's comment, <cr> needs to be added to the end of the mapping to actually execute the command.

So the final mapping would be:

:nmap <F2> :execute "normal ggf\<tab>" \| execute "normal \<lt>c-q>G$d" \| :%s/ \+//g<cr>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.