Vim copy and concatenate the lines - vim

I got a file that looks like this:
G:\some_folder
file1.avi
file2.wav
E:\some_folder2
fileABC.avi
fileDEF.wav
I would like to transfer the file into:
G:\some_folder
G:\some_folder file1.avi
G:\some_folder file2.wav
E:\some_folder2
E:\some_folder2 fileABC.avi
E:\some_folder2 fileDEF.wav
So in other words this might work like this:
look for ^[A-Z]: copy whole line and add it at the beginning to next lines till you find ^[A-Z]:
is it possible to do that in VIM? If yes, how.
thank you
Radek

I would iterate over all lines with :global; Vim will position the cursor on the beginning of each line. Depending on which kind of line it is, I'd either yank the folder path, or paste it in front:
:%g/^/execute 'normal!' getline('.') =~ '^\S' ? 'y$' : 'P'

Related

why is vim's delete command so slow

I have a file that contains about 5000 lines and I want to delete all lines that have 'some_string' so I first search for /some_string then I execute :g//d. This takes over 5 minutes to delete ~90% of the lines. What gives?
In comparison, if I run sed -i '/some_string/d' some_file it takes 46ms.
Add an underscore to your command.
I experienced a similar problem and it turned out to be each line being copied to my system clipboard. By adding a _, you tell vim to use the blackhole register.
:g//d_
The help gives the following syntax for :d
:[range]d[elete] [x] Delete [range] lines (default: current line) [into register x].

How to copy multiple lines one under each line separately?

Sorry for the bad english, see this example:
original
a
line01
line02
line03
line04
b
want to become:
a
line01
line01
line02
line02
line03
line03
line04
line04
b
a and b are irrelevant.
I can copy one line and paste, and repeat. Is there any simple solution? like one command?
Using a global command, this could easily be done like this
:g/^/t.
Breakdown
:g start a global command
/^ search for a begin of line (every line matches)
/t. copy the current line
You could also write a vim macro-
With your cursor at line 0, column 0; record a macro, store in register a
qa
Copy the current line; paste it below; move your cursor down to the next line
yypj
Save the Macro
q
Now run the a macro N number of times (it will stop at the bottom of the file regardless)
3#q
For funsies a sed solution:
$ sed 'p' input.txt > output.txt
Using filter, :!, inside of Vim:
:%!sed p
Obligatory awk solution: awk '1;1' input.txt > output.txt.

How do you replace a line with forward slashes in it in EX?

I'm running a script for vim EX mode I've tried every escape character and word identifier I can find.
it needs to find the string "/etc/walker" and replace it with "/etc/runner"
% s/\</etc/walker\>/\</etc/runner\>/g
wq
same issue with a script to append at the end of the file. It doesn't do anything. I'm trying to append "/etc/walker"
$
a
\</etc/walker\>
.
wq
what I've tried on regex editors seems to work there but not in EX
Thanks for your help
Try this:
:s#/etc/walker#/etc/runner#
Notice the use of # as a delimiter, that way you don't have to add back slashes.
You could also use:
:s#/etc/walker#/etc/runner#
For appending at the end of the line:
:s#$#/etc/walker#
In EX mode just remove the : at the beginning.

vim replace all lines after a given line

I want to replace using vim command, right now I'm doing
:num1, num2 s /word/newWord/g
However, how can I do something like all lines after a number
something like
:num1, infinity s /word/newWord/g
:num1, s /word/newWord/g
This recognize only the num1 line.
$ is used to mark the last line of file so you can do this with this command:
:num1,$ s/word/newWord/g

vi, find line and output/output to separate file

I'm looking for a command in VI/VIM to search for particular text in the file and grab the whole line and output in either on the screen or separate file. e.g.
This is some line with this _word_ and some other text.
This is some line with this some other text and some other text.
so this would output only the first line..
:redir > output.txt
:g/_word_/p
:redir END
The line will be output to the screen and to output.txt. See :h :redir.
EDIT: I agree with others who suggest using plain old *nix grep if you can. Situations where you might not be able to use grep:
You're searching buffer text that doesn't exist in a file on the filesystem.
You're using Vim-specific regex extensions, like cursor location, column number, marks, etc.
You want do this in a cross-platform way, and grep might not exist on the system you're using.
redir can be useful in these situations.
use the g (global) command:
:g/_word_/y
will yank all lines containing _word_
having mentioned the DOS find command, you probably want to use grep:
grep -h '_word_' * > results
in vi, from command mode
search for _word_
/_word_
yank the line
yy
paste the line
p

Resources