34

In vi, I can use o or O to add a blank line and go into insertion mode. But what if I want to stay in command mode, is there a command for this?

In googling, I'm seeing suggestions to add stuff to my vimrc, but it seems like there should be an easier way (that will always work.)

1
  • nnoremap O O<Esc>
    – aderchox
    Commented Jul 21, 2020 at 0:41

9 Answers 9

36

According to the VIM FAQ you can use the :put command:

12.15. How do I insert a blank line above/below the current line without entering insert mode?

You can use the ":put" ex command to insert blank lines. For example, try

:put =''
:put! =''

For more information, read

:help :put

but then really it's easier to add:

map <Enter> o<ESC>
map <S-Enter> O<ESC>

to your .vimrc. This way you can press Enter or Shift-Enter in normal mode to insert a blank line below or above current line. Of course substitute <Enter> and <S-Enter> with your preferred keys.

19

Part of the allure of using VI is having a command mode for manipulating the text, and an edit mode for adding text. This keeps you from having to hit CTRL-whatever to accomplish things.

The other suggestions may work - I haven't tried them - but they seem to require quite a few characters or manipulating your VI environment too much.

Try using o[ESC] followed by a . for each new line you want to add, or 5. to add 5 lines. This is much faster, and will always work on any install of VI.

2
  • 3
    You can use . to repeat,but you can also do <number>o<esc>,example: 5o<esc>
    – bollovan
    Commented Jul 11, 2011 at 18:42
  • 2
    That would certainly work. But I like seeing how the command will actually impact the document before I have it do it multiple times. I can't tell you how many times I've o'd append a new paragraph in VI, only to find out that the pretty paragraph was actually six carefully broken/word-wrapped lines.
    – Glorytoad
    Commented Sep 6, 2011 at 19:48
3

In vim, there's also :normal o or :normal O.

2

To insert a blank line after the current line while staying in command mode, use:

: r !echo

This is the "read in a file" command, but instead of giving it a filename, we tell it to read the output of a shell command (!); that shell command, echo, simply prints a blank line. After the blank line is inserted, you remain in command mode.

0
2

This will add a blank line after the current line in Vim:

:put _ 

Alternatively, specify a line at the start.

1

Both Tim Pope's unimpaired.vim - Pairs of handy bracket mappings and my own LineJuggler - Duplicate and move around lines plugin offer handy short ]<Space> mappings for this.

1

To insert a blank line below the current line use :put, or :put! to insert one above.

To specify where to insert the blank line (nth line) use :[n]put.

e.g.1: Add a blank line below line number 23 while in command mode.

:23put

e.g.2: Add 5 blank lines above line number 23 while in command mode.

:23put! =repeat(\"\n\",5)

Notes and warnings:

  • For the output of a single empty line you do not need to use the = feature unless there is a value stored in the unnamed register ", in which case you should use :put ='' which contains two single quotes as the expression.  

  • To clear the unnamed register ", type :let @"=''.  

  • Check the contents of the Vim registers with :dis or :registers.

  • :pu is the equivalent shortcut command.

  • Type :help put or :help registers for more information on this topic.

  • Vim version 7.2 was used in my testing.

  • @Mr Shunz's answer has a caveat.  If you use the double quotes as the expression i.e. :put =", then the previous expression will be used.  E.g. when you enter :put =test and :put =" in succession then the output below the current line will be:

    test    
    test
    
  • This answer is an improvement upon that of @Mr Shunz and @justerman, the former omitting some important caveats; and the latter having an equivocal instruction on how to specify an insertion point: "... specify a line at the start".

  • @Steven Pritchard's and @Jeff Schaller's answers using :normal o and :r !echo respectively are good alternatives.

  • Using a script as suggested by @Ingo Karkat is overkill.

  • @Glorytoad and @Hakim use o[Esc] which puts Vi into edit mode. 

1
  • Sorry for the mangled command, but that's one of the reasons why code formatting should be used for commands - quote formatting often either mangles commands when given as is, or needs extra escaping to come close to what code formatting does without all that. BTW, you can always address editors to your post by commenting with @username.
    – muru
    Commented Oct 30, 2020 at 5:10
0

On Vim 7.3: <number>o<esc> creates only one empty line.

2
  • Well, by adding <esc> you go again from insert mode to command mode. I think he is looking for an answer that do not go to insert mode at all.
    – Bernhard
    Commented Apr 7, 2013 at 19:40
  • Oh! I forget to quit insert mode with <esc>. In this case, it will work. It will even duplicate the line edited on all the following lines.
    – Hakim
    Commented Apr 9, 2013 at 8:11
-2

I'm surprised no one else mentioned this:

Use 'r' followed by 'Enter key' in command line mode.

Late to the the party by 7 years though :)

4
  • 1
    Except that this takes you out of command mode and replaces the current line with a blank one.
    – Jeff Schaller
    Commented May 17, 2018 at 20:41
  • I’m not sure what @Jeff is saying.   My observations regarding this command are (1) it doesn’t work if you’re currently on a blank line, and (2) otherwise, it deletes the character under the cursor. Commented May 17, 2018 at 21:53
  • I misspoke; r indeed replaces the current character. Still takes you into command mode.
    – Jeff Schaller
    Commented May 17, 2018 at 22:30
  • Agreed. This is a limitation: It has to be done on a non-empty line + on a blank space before the line. Did not think of this. Thanks. Commented May 19, 2018 at 4:45

You must log in to answer this question.

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