VIM - Combine line from multiple files into single file - vim

I'm trying to walk through my buffers list, select a single line from each buffer, and to have them all concatenated into a single file (or other buffer). As in:
file1
...
line2
...
file2
...
line2
...
file3
...
line2
...
and so on.
all into:
myfile
line2 (file1)
line2 (file2)
line2 (file3)
I can't seem to get my registers working, and bufdo is causing me heartache for some reason...
[clarification]
I was hoping I could use bufdo to walk through all my buffers, yank the second line from each, and append it into a register.
Then on another file, just paste the register contents into it (containing the second line from all of my buffers).

You should be able to do this with something like:
bufdo normal 2G"Ayy
which iterates through the buffers and runs the given command in normal mode. 2G jumps to the appropriate line, and "Ay yanks into register a, appending instead of overwriting (since the A is capitalized). Make sure register a is empty before you start!
You can use windo or tabdo if you have windows or tabs instead of buffers.

Related

New lines ignored when copying from vim

My vim has developed a strange behavior. I'm running vim 8.0 on WSL.
When I type code in vim, in vim it shows a \n character is present, but when I copy it to another program, the new line is lost and a very long blank line is inserted in its place. After I save the file and reload it, the problem goes away. I've tried changing the file type between dos and unix, in case something weird was going on \r, but that did not fix the problem. When I paste it into a new program most of the time it actually looks fine, but things like the python interpreter take issue, as they see these as all one line.
I should note this is using the windows clip board, not vims. Vims copy and paste, puts the \n in correctly, but I can't use that between programs.
right after edit
line1 line2 line3 line4
after save
line1
line2
line3
line4
even on stack overlfow it looks right when pasted, but as you can see above it is pasted without newlines.

How to copy (yank) from file 1 and paste (put) in file 2 in vi using buffer?

I want to know the process for copying data from file 1 and paste the data into file 2 in vi editor using a buffer.
Can you please tell me the step by step process.
How to do that?
From vi manual:
6.5.6.3 Using Named Buffers
To repeatedly insert a group of lines in various places within a document, you can yank (or delete) the lines
into a named buffer. You specify named buffers by preceding a command
with double quotes (") and a name for the buffer. For example, to yank
four lines into the named buffer a, type "a4yy. You can use several
different buffers. For example, you might also delete text from one
location and add it to several others. To delete 12 lines into the
named buffer b, type "b12dd.
To insert the text, precede the p or P command with n, where n is the
named buffer. For example, to insert the lines saved in buffer b, type
"bP.
You can overwrite named buffers with new lines. The buffers are saved
until you exit vi.
When you use named buffers, you can safely delete and yank other text
without affecting the lines you have already saved in the named
buffers -- unless, of course, you purposely overwrite the named
buffer.
Just in case you don't really need the buffer contents...
then you can also just directly do something like this to overwrite file2:
:4,12w file2
or append to it with something like:
:5,20w >> file2
Of course other standard ex line addressing options are also available.

Vim: issue regarding copy and paste across different buffer

I met a problem when copy some lines in one buffer to any buffer.
Here is the details.
For example, I use command '3yy' to copy 3 lines in buffer file1 and want use command 'p' to paste these lines in buffer file2. However, if I do a 'd' command between this two commands to delete some lines in either buffer, the 'p' command will not work anymore. It cannot paste the content I copied using '3yy' before.
I am on Vim Window 7.
The unnamed register contains what you yank and what you cut. You can…
use the 0 register which always contains the last yank: "0p
delete "for real" with the "black hole register": "_d.
See :help registers.
Just like DOOM said in the comment, 'd' will yank to the default register overwriting what you yanked previously. If you want to preserve what you yanked, place it in a register. Eg:
"13yy
will place 3 lines into register 1. You can paste them like this:
"1p

Join non-matching lines

I'm trying to join all lines who do not have a match
example text:
text Like
This is text of Line2
This is text of Line3
This is text of line4
Like text
This is text of line6
This is text of line7
Like
This is text of line9
All lines where "Like" is NOT present must join (with space between them)
end result:
text Like
This is text of Line2 This is text of Line3 This is text of line4
Like text
This is text of line6 This is text of line7
Like
This is text of line9
Can anyone help me?
First get rid of your trailing spaces:
:%s/\s\+$
Now here's an idea to join these lines:
:v/Like/normal VnkJ
Which should be self-explanatory. On every line that does not contain "Like",
enter visual line-wise mode, search for the next "Like" line (it reuses the
previous pattern), go one up and join.
:%v/Like/.,/Like/-1j
If you only want lines that start with Like, use ^Like instead.
And if you want to get rid of trailing spaces, do as sidyll wrote.
The code means:
% for all lines
v that do not match /Like/
., do from the current line (aka the (not) matching line)
/Like/-1 To the line bevor the next line matching /Like/
j join.
Since this is easier to understand and looks much nicer, I just add the compleate Version here:
$s/$/^MLike/|exec '%v/Like/.,/Like/-1j'|$d
It has the following addition:
$s/$/^MLike/
with ^M being an actual return (done via ^Vreturn)
this line adds a "Like" at the end, just in case you don't have one
exec '...'
Execs the v-line and protects the last | from being included in the repetition
$d
deletes the added 'Like' again.
I would use the following command.
:v/Like/,/\n.*Like\|\%$/j

In vim, how can I delete all lines in a file except the last 100 lines?

In vim when my cursor is on the first line I can press:
100dd
to delete the first 100 lines.
But how do I delete all lines except the last 100 lines?
In normal mode:
G100kdgg
In other words:
G -> go to last line
100k -> go up 100 lines
dgg -> delete to top of file
In ex mode:
:1,$-100d
Explanation: ":" puts the editor in "ex mode". The d command of ex mode deletes lines, specified as a single line number, or a range of lines. $ is the last line, and arithmetic can be applied to line numbers.
An alternative general purpose solution:
:%!tail -100
You can use any shell command after the ! to arbitrarily modify the current buffer. Vim starts the command and feeds the current file to stdin, and reads the new buffer from stdout.

Resources