Saturday, August 29, 2015

Re: Copy text and Paste at multiple lines

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












--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments: