Vim: insert empty line above current line (not open, i.e. without entering Insert mode)

21

5

is there a way of inserting a new line above the current, without opening the line (so without entering Insert mode)? That is, like "O", but without opening.

tmadsen

Posted 2010-06-01T18:16:16.757

Reputation: 1 079

2What does "opening a line" mean? – Matteo Riva – 2010-06-01T18:41:04.237

"opening a line" means going form normal mode to insert mode, which is what happens when you press "O" (oh). – tmadsen – 2010-06-01T19:14:38.230

Answers

9

I use Tim Pope's plugin unimpared which has a command specifically for this purpose:

[<space>

And if you want to add a space after the line you are on:

]<space>

Eric Mathison

Posted 2010-06-01T18:16:16.757

Reputation: 296

29

The definition of your request is O.

Logically when inserting a new line above would be O.

To insert before the curser: i
After: a
Before the line (home): I
Append at the end of line: A

The closest I think you'll come is I which will put the cursor at the beginning of the line.

Josh K

Posted 2010-06-01T18:16:16.757

Reputation: 11 754

Maybe I wasn't clear. The scenario is this: I'm in normal mode and want to insert a blank line above the one I'm currently on, while staying in normal mode. Pressing "O" puts me in insert mode. Maybe there's no way to do it and that's ok, just thought I would ask :) – tmadsen – 2010-06-01T19:16:58.120

3Just map it, m\O+Esc`` – Josh K – 2010-06-01T19:33:11.190

1Good idea, hadn't considered that. Did a :noremap <leader>O O<ESC> – tmadsen – 2010-06-01T20:05:15.007

2tmadsen: You can map this <yourkey> O<ESC> – Dzung Nguyen – 2010-06-02T04:01:56.410

9

:h append()

It won't move the cursor, it will let you insert as many lines (empty or not) as you wish, and it won't modify the previous position mark.

call append(line('.')-1, '')

Luc Hermitte

Posted 2010-06-01T18:16:16.757

Reputation: 1 575

+1 for a solution that works regardless of how one has set formatoptions – Micah Smith – 2015-10-12T01:39:26.530

yah, that is +1 neat! /me changes his vimrc – akira – 2010-06-02T06:52:33.157

Well, it got a few advantages when it comes to scripting. It's bit overkill to type otherwise. – Luc Hermitte – 2010-06-02T07:53:48.280

5

You can map whatever key or key sequence you like to

m`O<ESC>``

this inserts a blank line above the current one keeping you in normal mode and without changing cursor position.

Matteo Riva

Posted 2010-06-01T18:16:16.757

Reputation: 8 341

so the command to map this to Alt-O would be :map <M-o> m`O<ESC>`` – K Robinson – 2010-06-03T16:47:28.833

That's correct. – Matteo Riva – 2010-06-03T19:05:29.563

2

:nmap <CR>_i<CR><ESC>

akira

Posted 2010-06-01T18:16:16.757

Reputation: 52 754