Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to escape in g command's norm mode of Vim

Tags:

vim

I want to add []( to the beginning of a line and ) to the end of a line that starts with http in Vim.

To do this I am using the following g command:

g/^htt/norm I[](

Now, I want to press Esc key now inside g and put A). How can I do that?

like image 831
Mert Nuhoglu Avatar asked Dec 10 '12 10:12

Mert Nuhoglu


People also ask

How do I use Esc in Vim?

If you have an American English keyboard, pressing Ctrl-[ (control plus left square bracket) is equivalent to pressing Esc.

How do I get out of insert mode in vi editor without escape?

To exit from 'insert' mode, hit ESC (the escape key). If you want to force a quit (i.e. quit without saving) type: :q! You should by now have a file called data.

How do I exit insert mode?

Pressing ESC quits from insert mode to normal mode, where you can press : to type in a command.

What are the motion commands in normal mode?

Normal mode is where one should spend most of their time while using Vim. Remember, this is what makes Vim different. In normal mode, there are multiple ways to move around an open file. In addition to using the cursor keys to move around, you can use h (left), j (down), k (up), and l (right) to move as well.


3 Answers

You can escape the Esc key by using ctrl+vfollowed by esc.

ctrl+v will insert the next non-digit literally.

Note: You may have to use ctrl+q depending on your system. (Nice mnemonic is quote)

See help for more information

:h c_Ctrl-V
like image 148
Peter Rincker Avatar answered Oct 19 '22 00:10

Peter Rincker


To be able to use two :normal commands in sequence, you have to wrap them in :execute. When you use double quotes, you can then write the Escape as \<Esc>, like this:

:g/^htt/exe "norm I[](\<Esc>" | norm A)

Notes:

  • Unless you need mappings to apply, using :normal! (with the bang) is preferred, because it's more robust.
  • You could have also used :substitute here.
like image 26
Ingo Karkat Avatar answered Oct 19 '22 00:10

Ingo Karkat


This is easier to do with :s:

:%s/^htt.*/[](&)/
like image 1
Thor Avatar answered Oct 19 '22 00:10

Thor