33

I'm so sure that this must exist, but if it doesn't maybe there is a macro for it...

One of my most favourite features of vim is the insert before when in visual mode (<C-v>, select the lines, <C-I>, type a little, then Esc).

My issue is that I want to paste the clipboard contents before, not 'insert'. I tried <C-P> but no love.

I have read cheat sheets, searched everywhere, looked through the questions on here and haven't found it, but I could definitely be searching for the wrong thing.

kenorb
  • 155,785
  • 88
  • 678
  • 743
Christian
  • 3,917
  • 2
  • 23
  • 40
  • Related: [How to paste a line in a vertical selection block?](http://vi.stackexchange.com/q/4774/467) at Vim SE – kenorb Dec 23 '15 at 14:23
  • Because there are no existing question for pasting before the cursor, searching for "vim paste before cursor" on both [duckduckgo](https://duckduckgo.com/?q=vim+paste+before+cursor&ia=qa), [bing](https://www.bing.com/search?q=vim+paste+before+cursor) and [google](https://www.google.com/search?q=vim%20paste%20before%20cursor) returns this as the top result, and it happens to have the answer to that question. – user202729 Oct 18 '18 at 10:38
  • 1
    Note that: in this case, the clipboard content is 1 line long, and OP wants to paste the *same* content on *all* selected lines. That's what OP really want but not explained in the question. – user202729 Oct 18 '18 at 10:50

5 Answers5

176

Generally, the P command (upper case, different from p) pastes the contents of the clipboard before the cursor position. Is that what you're looking for? (I'm not quite sure what you mean when you say you press Command+I, as my keyboard doesn't have a Command key.)

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
  • ok, sorry, to do insert before. pastes once only. – Christian Nov 01 '10 at 23:13
  • 12
    Well, this answer may not be what the original asker was looking for, but it seems to be the solution for everyone else. – vastlysuperiorman Apr 04 '16 at 18:50
  • 1
    They should have a rule that says "dismiss the rules about accepted answer if the non accepted answer has literally 10X the votes of the first, in a 3 digit number". Just turn it accepted or at least first to be seen. – j riv Oct 15 '19 at 12:59
20

I assume you mean using I in visual block mode to insert the same text on multiple lines, where hitting p simply pastes on the current line rather than all the selected lines.

In insert mode, you can hit C-r followed by a register to insert the contents of that register, so if you wanted to paste the unnamed buffer, you'd enter

C-r"

Similarly, to paste from the clipboard

C-r* 

By entering insert as you normally would, then using C-r, you'll get the text on all of the selected lines.

Take a look at :h registers to see what registers are available to you.

Alligator
  • 1,359
  • 11
  • 8
1

May seems late, but I faced the same problem.

e.g. A file having two lines:

foo
bar

  1. Press v: visual mode select foo in virtual mode
  2. Press y: copy
  3. Move the cursor to "b"ar

4-1. Press p => bfooar

4-2. Press P(Shift+P) => foobar Which is what I expected.

TLDR: Upper case P (shift + p)

peteeelol
  • 33
  • 4
0

Based on Alligator solution, using maps.

E.g. to paste the string _s_ in 3 cases:

" |-- original --|-- pasted after C   --|-- pasted before C  --|-- pasted replacing C   --|
" |--------------|------------------------------------------------------------------------|
" |--          --|-- select column C  --|-- select column B  --|-- select column C      --|   --> step 0 (after copying/yanking a string) 
" |--          --|-- press <leader>py --|-- press <leader>py --|-- press p              --|   --> step 1 if yanked
" |--          --|-- press <leader>pc --|-- press <leader>pc --|-- press "+p            --|   --> step 1 if copied to clipboard
"       ABCD              ABC_s_D               AB_s_CD                 AB_s_D 
"       ABCD              ABC_s_D               AB_s_CD                 AB_s_D 
"       ABCD              ABC_s_D               AB_s_CD                 AB_s_D 

Where the mappings for each register (yanked or copied respectively) are:

xnoremap <leader>py A<C-r>*<Esc>
xnoremap <leader>pc A<C-r>+<Esc>

In previous examples I pasted after column (even pasting before column C, I workaround to paste after column B). If desired to paste before just change A to I in the 2 mappings.

Instead of "+p, one could use plain p if previously: copied + to * register (needed in last case of table):

nnoremap <leader>z :let @*='<C-r>+'<CR>
Xopi García
  • 359
  • 1
  • 2
  • 9
  • With _copy_ I mean to clipboard (`+` register). Not the Vim `:copy` command. See [http://vimcasts.org/episodes/long-range-line-duplication/](http://vimcasts.org/episodes/long-range-line-duplication/) – Xopi García Jan 01 '21 at 15:26
0

for example, we need insert My in 4 lines before word Fish:

def EatMyFish(x):
    return x - 1

a = EatFish(1)
b = EatFish(2)
c = EatFish(3)
d = EatFish(4)
  1. copy My at def EatMyFish(x):
  2. put cursor on F at line EatMyFish(1)
  3. Ctrl-v vertical select
  4. 3j select all F in column
  5. I go edit mode for all 4 lines (it's big i)
  6. Ctrl-o normal mode for one action (to remember: o = one or once)
  7. P insert before cursor (it's big p, not small)

or

  1. Ctrl-r" to insert from register in insert mode, instead steps 6-7 above

anyway, now we have:

def EatMyFish(x):
    return x - 1

a = EatMyFish(1)
b = EatMyFish(2)
c = EatMyFish(3)
d = EatMyFish(4)