Option 1
you can just do o<esc>p
to
- make a new line below the current one
- exit insert mode without removing the auto-indentation (as opposed to
- paste the word at the cursor position
Just map that to something like ]P or whatever.
Option 2
You can remap ]p to add a line-break at the end if there isn't one already
nnoremap <buffer> ]p :let original=@*<CR>:let @* = substitute(@*, "\\n\\?$", "\n", "")<CR>]p:let @*=original<CR>
How does this work?
- Save the current value of register
*
into the variable original
- Substitute the empty match at the end of the string and optionally a single preceding newline with a newline in the string in register
*
- Save the modified string into the register.
- Perform the original
]p
action (because nNOREmap
was used)
- restore the original value of the
*
register from the variable
Note that your vim may not use the register *
for the clipboard; if the property clipboard
(:help 'clipboard') is not set, vim uses the register "
, on linux it may use +
. In those cases you'd have to modify the mapping accordingly.
Option 3
nnoremap ]p :let [content, type] =
\ [getreg(v:register), getregtype(v:register)] \|
\ call setreg(v:register, content, "V")<CR>]p
\:call setreg(v:register, content, type)<CR>
This should be even more universal than option 2, though also a bit longer and uses more functions that may seem unfamiliar to new users.
What it does is:
- Save the content and type of the register that vim uses for yank/put operations (
:help v:register
) into the variables content
and type
(:help let-unpack
)
- Set the register to its previous text, but with mode
"V"
(line-wise)
- Do the default
]p
operation
- Reset the register to its original mode
P.S.: I'm adding option 3 to my own vimrc now :)