29

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.

2
  • 2
    What does "opening a line" mean? Jun 1, 2010 at 18:41
  • "opening a line" means going form normal mode to insert mode, which is what happens when you press "O" (oh).
    – tmadsen
    Jun 1, 2010 at 19:14

5 Answers 5

11

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>

37

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.

4
  • 2
    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
    Jun 1, 2010 at 19:16
  • 3
    Just map it, m`O+Esc`
    – Josh K
    Jun 1, 2010 at 19:33
  • 1
    Good idea, hadn't considered that. Did a :noremap <leader>O O<ESC>
    – tmadsen
    Jun 1, 2010 at 20:05
  • 2tmadsen: You can map this <yourkey> O<ESC> Jun 2, 2010 at 4:01
10
: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, '')
3
  • yah, that is +1 neat! /me changes his vimrc
    – akira
    Jun 2, 2010 at 6:52
  • Well, it got a few advantages when it comes to scripting. It's bit overkill to type otherwise. Jun 2, 2010 at 7:53
  • 1
    +1 for a solution that works regardless of how one has set formatoptions Oct 12, 2015 at 1:39
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.

2
  • so the command to map this to Alt-O would be :map <M-o> m`O<ESC>``
    – K Robinson
    Jun 3, 2010 at 16:47
  • That's correct. Jun 3, 2010 at 19:05
2
:nmap <CR>_i<CR><ESC>

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .