VIM: exit insert mode with :normal command - vim

When I go into insert mode with the :normal command (:normal i) for example, how do I exit insert mode?
If I press <Esc>, or <c-c>, or <c-[>, VIM exits command mode and I can't run my :normal command.
I put imap <c-e> <Esc> in my .vimrc but when I type <c-e> in command mode, nothing gets inserted. I can't figure out how to enter a "control e" in command mode.
<c-o> works, for example :normal Ihello<c-o>Aworld but sometimes I want to do more than one command in normal mode.
I know I can use a macro, but I want to know how to do it with :normal.

To add a literal <ESC> to your command, while in insert mode, press CTRL+V then <ESC>.
See :help i_CTRL-V.

The maintainable solution would be:
exe "normal! Ihello\<c-o>Aaworld\<esc>"
... :h :normal

:imap will not trigger in command mode. Use :cmap or better, :cnoremap.
And as too much php said, CTRL-V makes it possible to insert raw characters in insert mode or command line editing.

Related

Remap Esc in Command Line Mode for Vim

I want to use <leader>e to return to normal mode from whatever mode I am in. For insert mode I use
inoremap <leader>e <esc>
I wanted the same thing to exit command line mode, so I wrote
cnoremap <leader>c <esc>
However, when it exits command line mode, it tries to execute whatever what written past the command line. However, when I just use <esc> normally, it exists command line mode without executing anything. I usually have to press <enter> to execute the command.
Can anyone help me understand what is going on here, and what I can do to get the behavior I want.
Thanks!
According to help:
CTRL-[ *c_CTRL-[* *c_<Esc>* *c_Esc*
<Esc> When typed and 'x' not present in 'cpoptions', quit
Command-line mode without executing. In macros or when 'x'
present in 'cpoptions', start entered command.
It seems "in macros" applies to right-hand side of a mapping too (well, it's not "typed"). You can use CTRL-C instead as it will never execute the command:
*c_CTRL-C*
CTRL-C quit command-line without executing
So just do:
cnoremap <leader>c <c-c>

Why does the <C-n> syntax for using control key work with :command command but not with :normal command?

I am trying to execute a normal mode command Ctrl-n or j from Ex mode. Normally, one would do this using the :normal command. For example, the following command moves the cursor one line down.
:normal j
So does the following command. Note: ^N is typed by pressing Ctrl-v Ctrl-n.
:normal ^N
But the following command does not work. This command seems to have no effect on the buffer.
:normal <C-n>
However, when I create a new Ex command for Ctrl-n using the following command, it works!
:command Down <C-n>
Even this works, although normal is redundant here.
:command Down normal <C-n>
Now, I can use the Ex command :Down to move the cursor one line down.
My question is why does the <C-n> syntax not work with the :normal command but works with the :command command?
use :exec and escape the <c-x>:
for example:
:exec "normal \<c-n>"
in fact the instruction you can find in :h :normal help doc:
to use |:execute|, which uses an
expression as argument. This allows the use of
printable characters to represent special characters.
Example: >
:exe "normal \<c-w>\<c-w>"
Your question is probably academic (or you are trying to solve another problem) but, for what it's worth, you can already do :+ and :join.

How to input ^M in vim when ctrl-v is used as paste

My vim used ctrl-v as paste short-cut, which conflict with commands need ctrl-v as shortcut prefix.
For example, ^M need ctrl-v ctrl-m. I can do that in vim command line, and vim without intialization. But the symbol won't go to buffer from vim command line. :s/aaa/^M/g doesn't work.
Although, I can use echo -e "\r" or no-initialized vim to work around it.
But how to type this symbol in my current vim configuration?
to input ^M you can try followings:
Ctrl-V Enter
Ctrl-V Ctrl-M
use digraph Ctrl-K C R, C R means CR.
You could try using a mapping to the original function of ctrl-v:
noremap! <C-Q> <C-V>
But as explained on :h CTRL-V-alternative and noted by ZyX on the comments, your issues may be that your terminal is capturing the ctrl-q (more details here). If the mapping above doesn't works you could try mapping to something else:
noremap! <leader><C-V> <C-V>
Then hitting leader (usually \) followed by ctrl-v ctrl-m should insert ^M on both insert and command mode.
try Ctrl-Q Enter
(This solution also works when you try to enter digraphs by number in Vim)

Move forward/backwards one word in command mode?

Lets say I am copying a file with the vim command mode and my cursor is at the end of the line.
:!cp path/to/original/file path/to/new/file
Is there a way I can jump back a word like I can in the shell by typing Esc b?
You cannot use "Esc b" because, obviously, that would discard the command you where typing. However you can bind some keys to move around.
The question as already be answered here : Navigating in Vim's Command Mode
The easy way is just to add :
cnoremap <C-a> <Home>
cnoremap <C-e> <End>
cnoremap <C-p> <Up>
cnoremap <C-n> <Down>
cnoremap <C-b> <Left>
cnoremap <C-f> <Right>
cnoremap <M-b> <S-Left>
cnoremap <M-f> <S-Right>
In your .vimrc
For entering and editing complex commands, you may like working directly in the command line window which is accessed with the normal mode command q:. See :h 20.5 and :h q:. Or if you are already in command mode, you can access the command line window with C-f.
For example, in normal mode type q: to get into the command line window. (or type C-f from command line mode.
You can move around previous commands using standard motions and you can edit as usual.
When you want to execute a command that you just edited, press enter in normal mode in
this command line window. The line your cursor is on will be executed as a command in
the window you were in before you opened the command line window.
Another option to consider is to edit/yank the command from another buffer. You can do this by yanking the desired text and pasting it in command mode by typing C-R n, where n is the register you yanked to.
BTW: I like the mappings that #rks provided. But if you don't have these mappings, you can use the out of the box commands. Look up :h c_<S-Left> and :h c_<S-Right> and :h 20.1.
A nice vim feature is ctrl-f. Typing ^f (or whatever key is specified in the cedit option, with ctrl-f being the default) from command line mode has the same effect as typing q: from normal mode; it pulls your entire command history into a window and lets you edit it as a buffer. Try :help cmdwin for more details.
In vim's command mode, I just use ctrl-left and ctrl-right arrows. The same works in bash - I wasn't aware of the esc-b method there.
No editing of the .vimrc file is required for this on my Ubuntu and Debian systems, but YMMV on others. It's presumably based on the standard configuration that's packaged for the OS

Why doesn't "map! <C-q> :q <CR> " work in vim?

I input :map! <C-q> :q <CR> in command line mode, then return to normal mode, and press ctrl-q, but vim does not quit. Why?
As Johnsyweb suggested, Ctrl-Q does not reach Vim in the first place. This holds for the popular Ctrl-S as well. Both can be fixed by adding
silent !stty -ixon > /dev/null 2>/dev/null
to your .vimrc as it forces these control sequences to reach the application.
Why anyone would want to add yet another way of quitting Vim to the plethora already available is beyond me. However...
On Fedora 15, Ctrl-Q is likely being captured by your terminal as XON (resume) and therefore not reaching Vim to quit the application.
You can check if there's an error in your mapping or a clash like so:
:verbose map! <C-Q>
:map! is for specifing mappings in insert and command-line modes.
normal-mode mappings are (usually) specified like so:
:nmap <C-Q> :q<CR>

Resources