Copy text and Paste at multiple lines

41 views
Skip to first unread message

naaj_ila

unread,
Aug 28, 2015, 10:36:43 PM8/28/15
to v...@vim.org
Say i had a text like this
baseline string a;
baseline b;
baseline c;
baseline d;

I need to have like this
baseline string a;
baseline string b;
baseline string c;
baseline string d;

Q1.I wanted to copy "string" and Paste across different lines at a single
shot .How can i do this.?
Q2.I wanted to copy a text from "unix terminal/from different Vim file".How
can i paste that string in multiple lines at a single shot?

I dont want to use "string find and replace".




--
View this message in context: http://vim.1045645.n5.nabble.com/Copy-text-and-Paste-at-multiple-lines-tp5725641.html
Sent from the Vim - General mailing list archive at Nabble.com.

John Little

unread,
Aug 29, 2015, 2:04:31 AM8/29/15
to vim_use, v...@vim.org
On Saturday, August 29, 2015 at 2:36:43 PM UTC+12, ali jaan wrote:
> Say i had a text like this
> baseline string a;
> baseline b;
> baseline c;
> baseline d;
>
> I need to have like this
> baseline string a;
> baseline string b;
> baseline string c;
> baseline string d;
>
> Q1.I wanted to copy "string" and Paste across different lines at a single
> shot .How can i do this.?

If, as in your example, the lines are contiguous, read on.

visual block put. See :help v_p. It's a bit tricky, but will do the job. First yank the text you want to copy, then start a visual block down the spaces at the end of "baseline", and press p. The pasted string will *replace* the visual block, and you can't have a zero width block. If you want to keep the contents of the block, press p again.

> Q2.I wanted to copy a text from "unix terminal/from different Vim file".How
> can i paste that string in multiple lines at a single shot?

Again tricky, the setting of the 'clipboard' or 'guioptions' settings interfere.
You probably have "a" for "autoselect" in guioptions, which means when you start the block mode, the contents of the block becomes the X selection, forgetting the text you want to paste. If you have "autoselect" on:
- You can use the X "clipboard"; at the source, after selecting do an explicit "Copy" operation (like you have to on Windows). Then, after making your visual block, use "+p to paste from the clipboard.
- You can copy the text to be pasted before starting the visual block. One way is to paste it (using "*p or middle-mouse) and immediately delete it. Another,
:let @" = @* "copies the selection to the unnamed register

HTH, John Little

Tim Chase

unread,
Aug 29, 2015, 2:13:32 PM8/29/15
to John Little, vim_use, v...@vim.org
On 2015-08-28 23:04, John Little wrote:
> On Saturday, August 29, 2015 at 2:36:43 PM UTC+12, ali jaan wrote:
>> Say i had a text like this
>> baseline string a;
>> baseline b;
>> baseline c;
>> baseline d;
>>
>> I need to have like this
>> baseline string a;
>> baseline string b;
>> baseline string c;
>> baseline string d;
>>
>> Q1.I wanted to copy "string" and Paste across different lines at
>> a single shot .How can i do this.?
>
> If, as in your example, the lines are contiguous, read on.
>
> visual block put. See :help v_p. It's a bit tricky, but will do
> the job. First yank the text you want to copy, then start a visual
> block down the spaces at the end of "baseline", and press p. The
> pasted string will *replace* the visual block, and you can't have a
> zero width block. If you want to keep the contents of the block,
> press p again.

As an alternative, if you don't want to overwrite the visual
selection, you can append to the visual block with "A" and then paste
the contents of the selection with

<c-r>"

and then hit <esc> which will paste the register after each line. So
the entire sequence would look something like

«visually select text you want to copy»
y "yank that text
«move to the top of where you want to paste»
<c-v> " enter visual-block mode
«move to the other end of the range»
A<c-r>"<esc> " paste the yanked text

>> Q2.I wanted to copy a text from "unix terminal/from different
>> Vim file".How can i paste that string in multiple lines at a
>> single shot?

You can use the same trick, only with your system selection register
("*" or "+") instead of using the default register (")

For reading it from a file, it gets a little more complex because the
standard EOL marker would also come in, so it would appear as if
you're also adding blank lines to your text. But you could do this
with the expression register. Inefficiently, that would be

A<c-r>=readfile('somefile.txt')<cr><esc>

which reads the file's contents fresh each time. Alternatively, you
could read it in once into a variable (say, `content`), and then
append the resulting contents:

:let content=readfile('somefile.txt')
«do most of the above»
A<c-r>=content<cr><esc>

This all assumes that your destination locations are column-aligned.
If they're ragged, you'd have to do a search-and-replace. Which also
isn't that hard. For your (OP's) purposes, it's pretty
straightforward and can be done in any of the ways above:

:'<,'>s/baseline\zs\ze b;/\=' '.@"
:'<,'>s/baseline\zs\ze b;/\=' '.@*
:'<,'>s/baseline\zs\ze b;/\=' '.readfile('somefile.txt')
:'<,'>s/baseline\zs\ze b;/\=' '.content

The "\zs" sets the start of where the replacement will occur and the
"\ze" set the end of where the replacement will occur, so by having
them back-to-back like in my example, they do an insertion rather
than a replacement. But notice that you also have to add another
space before (or after, depending on where the space occurs in
relation to the "\zs\ze") the content that you want to paste.

-tim












Reply all
Reply to author
Forward
0 new messages