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

StackOverflow https://stackoverflow.com/questions/23594702

  •  20-07-2023
  •  | 
  •  

Question

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?

Was it helpful?

Solution

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>"

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top