0

The easiest way to explain this question is to show you my code and what I am trying to do.

Here is some code, before any edits are made:

// some block of code here
// blaa blaa
// please imagine these comments are code

// another block of code after some whitespace break between
// this block and the previous
// blaa blaa
// please imagine these comments are code

Now imagine I want to edit "in-between" the 2 blocks of code above. I enter insert mode when the caret is between the blocks (on the whitespace line) I add some lines of whitespace (an extra 2), go up a line and enter a new variable name.

// some block of code here
// blaa blaa
// please imagine these comments are code

double a_very_long_variable_name = 0.0;

// another block of code after some whitespace break between
// this block and the previous
// blaa blaa
// please imagine these comments are code

Now I escape from insert mode (back to normal mode) and yank the variable name "a_very_long_variable_name" using "yiw" (yank in word) command.

I go down 1 line, to the blank line and press "p" to put. I get this.

// some block of code here
// blaa blaa
// please imagine these comments are code

double a_very_long_variable_name = 0.0;
a_very_long_variable_name
// another block of code after some whitespace break between
// this block and the previous
// blaa blaa
// please imagine these comments are code

What I want is to "put", but to add a new whitespace line (a newline character) after the put command.

  • Is there already a command to do this? It seems like something that would be frequently required.

I believe I can accomplish this by creating a new command like this in my vimrc. (I am not sure yet what the nmap syntax is... Will search for this now.)

nmap <S-p> p$i<Enter><Esc>

Edit: The command above should actually be:

nnoremap <S-p> p$a<CR><Esc>
4

There is indeed a single command that will do this:

:put

This can be abbreviated to :pu.

You can also add a ! to insert the new line of text above the cursor:

:put!

Or add a [line] to insert the text after/before any line you specify in the buffer:

:4put
:4put!

See :help :put for more details.

If you wanted to map it, you could write:

:nnoremap <S-P> :put<CR>

Although, personally, I'd use a different trigger: P in normal mode is a command I use all the time.

  • 1
    If you use unimpaired.vim, ]p will be forced to be line-wise which will have a similar behavior to :put. Unimpaired also has [<space> / ]<space> mappings which make adding whitespace more pleasant. – Peter Rincker Jan 24 '18 at 15:09
2

What you are looking for is o in normal mode see :h o:

Begin a new line below the cursor and insert text, repeat [count] times.

Note there is also O which adds a newline above the cursor.

For example you may want a mapping like this:

nnoremap <S-p> po<Esc>

As a side note I use these mappings to insert a newline without leaving normal mode:

" Quickly insert an empty new line without entering insert mode
nnoremap <Leader>o o<Esc>0"_D
nnoremap <Leader>O O<Esc>0"_D

The 0"_D delete the content of the new line using the black hole register to avoid having a newline containing some whitespaces.


Also it is not part of your question but your mapping can be nnoremap P instead of nnoremap <S-p>

  • Is there a reason to use po<ESC> rather than o<ESC>p ? – user3728501 Jan 24 '18 at 12:19
  • @user3728501 Well it depends on what you want to do. Your mapping p$a<CR><Esc> put your yanked text and add a new line under this text. po<Esc>does the same while o<Esc>p will add a newline and then put the yanked text on it. – statox Jan 24 '18 at 12:27
  • Ah ok, but the result is always the same? – user3728501 Jan 24 '18 at 12:32
  • @user3728501 No po<Esc> will give you yanked text[newline] while o<Esc>p will give you [newline]yanked text I think you will understand it better if you test it by yourself: create a buffer with two lines containing 1 and 2 respectively, yank something and then put your cursor on the 1 and try both of the mappings you should see the difference pretty easily. – statox Jan 24 '18 at 12:46
  • 1
    Ah - yes I can see the difference now thanks. – user3728501 Jan 24 '18 at 13:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.