Vim: paste multiple lines after multiple lines - vim

In vim, how can I paste multiple lines of code after every line of a visual block?
What I have is:
foo
bar
1
2
3
What I am trying to do is:
1
foo
bar
2
foo
bar
3
foo
bar
Is there a way to easily accomplish this?

You can cut it to the default register and do a global replacement for the rest of lines, like this:
Go to first column of first line of the file:
gg0
Cut data:
3dd
Do a global repeat for every line of the file and paste it:
:g/^/put
It yields:
1
foo
bar
2
foo
bar
3
foo
bar

assume that you want to copy and paste line number 1 2 3, run this command:
:g/^\S/1,3t.
then the text foo bar would be copied to right place. then you can remove the two lines.
You can also change the 1,3 to other range.

Not fully automated but almost there:
Visually highlight desired rows to copy with Shift+V
Delete with d
Move to first line ("1") and start recording with q followed by some letter, i.e., a.
Paste with p and then move down to the next line ("2"). Stop recording with q.
Now just repeat the last recorded command with ##, or + the designated letter, i.e., #a.

Related

Vim: unindent multiple lines to the beginning

Suppose I have something like this:
line 1 with text
line 2 with text
line 3 with text
line 4 with text
I want to unindent all of these lines to the beginning, like this:
line 1 with text
line 2 with text
line 3 with text
line 4 with text
Shift + V < gives me ONE level of un-indentation. How can I get them all to the beginning? Sorry, I'm having trouble phrasing this...
There are two different ways you could do this:
Visually select all of the lines, press <, and then press . as many times as you need until there is no indent left. Or if there are a specific number of lines you would like this on, you could do something like
5<< (unindent 5 lines)
<j (unindent this line and the next)
<ip (unindent inside this paragraph)
followed by as many . as you need.
Select all of the lines, and then type either :norm d^ or :s/^\s*
Also, Shift-V + V + < is basically the same as <<.

Append string to column on command line

I have a 3 column file. I would like to append a third column which is just one word repeated many times. I tried the following
paste file.tsv <(echo 'new_text') > new_file.tsv
But the text 'new_text' only appears on the first line, not every line.
How can I get 'new_text' to appear on every line.
Thanks
sed '1,$ s/$/;ABC/' infile > outfile
This replaces the line end ("$") with ";ABC".

What's the meaning of empty last line in jump list in Vim?

I saw the bottom jump list in Vim (ju in normal mode) could be in 2 different forms as below. The current pos (> sign) points to empty line in the first output, but not the case for the second output. I am wondering how to enter such state of jump list separately and what's the effect on navigation?
2 1 2 Some line in file
1 2 2 Another line in file
>
2 1 2 Some line in file
> 1 2 2 Another line in file
The > shows your current position in the jumplist.
The empty line means that the cursor is currently not at a position in the jumplist.
No empty line means that you jumped to some position in the list.

How to extract out lines with a pattern and put them in the end of the file

Suppose I have the following text.
a test1
b test2
a test3
b test4
what is the command to use to extract out the lines that start with the letter a and put them in the end of the file like this?
b test2
b test4
a test1
a test3
when I used :g/^a/d and p, the only last match is pasted:
b test2
b test4
a test3
You're only seeing a test3 at the end, because :d sets (not appends) the default register. Since :g executes the given command once per line that matches the pattern, only the last line is in the default register when you paste.
The canonical way to do this would be to use the :move (abbreviated as :m) command -- :g/^a/m $. For every line that matches ^a, move it just past the last line ($).
A slight modification to your initial approach would be to have :d append to a register, and then paste that register afterward.
:let #a='' " Clear register a
:g/^a/d A " For every line that matches ^a, delete it
" and append the contents to register a
:$put a " Paste the contents of register a after the last line
The last part could also be done using the normal mode command "ap.

what is the best way to edit similar strings?

I need to make next task:
file:
string1 data1 data
I need to create more strings with only changes in digits
string2 data2 data
string3 data3 data
string4 data4 data
How can i do it fast ?
select that input line, yank it into buffer a: "ayy.
Then record the following sequence: qq
"ap Paste the line below
ctrl-a Increment the digit
w Move forward one word
ctrl-a Increment the digit
"ayy Yank this line in as your new baseline
End your macro with q
Then repeat it as many times as you need (let's say 42), with 42#q
Several plugins manage that: visincr.vim, increment.vim.
I use visincr and that is very easy: Visual block, :I.

Resources