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>
user3728501
  • 111
  • 3

2 Answers2

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.

Rich
  • 31,891
  • 3
  • 72
  • 139
  • 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
3

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>

statox
  • 49,782
  • 19
  • 148
  • 225
  • Is there a reason to use po rather than op ? – 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
  • po<ESC> is the one I want I think because it more closely resembles how p works? P should be o<ESC>p ? – user3728501 Jan 24 '18 at 13:06
  • @user3728501 Once again it all depends on what you like better. Personally I wouldn't do a mapping for that I would simply use "vanilla" p or P and use my <leader>o and <leader>O mappings but it is only my personal preference :) – statox Jan 24 '18 at 13:07
  • What is / what does o do? – user3728501 Jan 24 '18 at 13:45
  • @user3728501 see this question you could replace <leader>o by whatever key you want to map. – statox Jan 24 '18 at 14:13