Kaign
Kaign

Reputation: 353

vim, create new line after or before selected line

In vim I can jump to line number, for example, 14 like this : "14gg"; Then I can use "o" or "O" to create a new line after or before this line. Is there a way to create a new line before or after 14th without jumping to it? Something like "14O" or "14o" (these commands don't work)

Upvotes: 0

Views: 165

Answers (3)

MohitC
MohitC

Reputation: 4781

You can create a mapping in your .vimrc file for this, to map a single character to what you want to achieve. (Create a .vimrc in your home directory if you dont have one already i.e /home/user)

For example, put following lines in your .vimrc file

map e ggo
map E ggO

Now whenever you will do something like 14e, it will go to line 14 and insert a new line below and take you in insert mode. Likewise, E for inserting line above

Please note that this may override whatever functionality key e and E might have in vim.

I strongly recommend simply using 14ggo in a go to achieve what you want

Upvotes: 3

Luc Hermitte
Luc Hermitte

Reputation: 32926

Your question is a duplicate of this superuser question: https://superuser.com/questions/147715/vim-insert-empty-line-above-current-line-not-open-i-e-without-entering-inser

append() is the standard answer to your question:

:call append(14, '')

The cursor will not get moved, nor the position marks.

Upvotes: 2

romainl
romainl

Reputation: 196476

You can do:

14Go<Esc>
14GO<Esc>

or:

:14norm o
:14norm O

or:

:14put=
:14put!=

You'll need to jump back to where you come from anyway.

Upvotes: 1

Related Questions