I know in VIM how to search a string and delete the text till the start/end of line but I would like to know if it is also possible to delete all text in line before or after highlighted search pattern.
If you want to do this across all lines and don't want to retype your search term I'd suggest the following:
:%s/.*\ze<Ctrl-r>///
What this does is:
%s/: substitute across all lines in a file
.*: match any character
\ze: end matching so the rest of the pattern is not substituted
<Ctrl-r>/: insert text from the '/' register (which is the search register)
//: replace with nothing
Edit: Forgot about the after part. My suggestion to remove both at the same time would be:
:%s/.*<Ctrl-r>/.*/<Ctrl-r>//
To delete the text before FOO on the same line:
:s/^.*\(FOO\)/\1/
From beginning of line to the beginning of highlighted search pattern: 0dn
From after end of highlighted search pattern to the end of line: $N//e<Enter>lD
These will work in most of the cases.
I can't comment on other answers, so I answer here, but I am referring to the answer from xofon:
Just add a '%' in the command line, which would make do for all lines in a file.
delete all chars after ']' in all lines
:%s/\(\]\).*$/\1/
delete all chars before ' -- ' in all lines in a file
:%s/^\( -- \).*/\1/
To delete all text in the line line both before and after the search match you could also do:
:g//norm gnd0PlD
This executes normal mode commands on all lines that match the last search pattern. The commands are gn to select the match, d to delete it, 0P to paste it at the beginning of the line, l to move to the left (after the text that was just pasted) and D to delete until the end of the line. I'm given to understand gn is a fairly recent addition to vim, so YMMV.
Related
I have a list of products to place on a rails seed and I would like to instead of put brackets one by one on the list with a command place the brackets on the whole list?
for example:
1. Dakine
2. Dale of Norway
3. Dan Post
1. ["Dakine"],
2. ["Dale of Norway"],
3. ["Dan Post"],
I searched on the help but did not find any about. Thanks.
You can record a macro in Vim and repeat that.
If you are on number 1, you can do following:
qqf a["Esc$a"],Esc0jq
Explanation:
qq: Start recording macro in register q
f: Go to first space character
a: : Insert after (the space character from above)
\[": Insert those characters
Esc: Back to normal mode
$: Go to end of line
a: Insert after (end of line)
"],: Insert the characters
Esc: Back to normal mode
0: Jump to start of line
j: Go down one line
If you have 100 such lines, you can do 100#q to achieve your result.
With vim substitute command:
:%s/.*/["&"]/
If you don't want to operate on all lines, then select the ones you want to transform or note the related line numbers, and then type :s/..... without the %. You'll see actually :'<,'>s this range represent the visually selected lines, and vim adds it automatically in visual mode.
On Atom you can enable the find to use Regex in the search(there is a button next to the search field)
Then you can search for something like (^.*$) to get every line separated by groups and in the Replace field you use ["$1"],. The $1 represents the value matched by the Regex.
Then just do a Replace All and remove the last comma in your list if needed.
I have a text document that has blank lines and lines that start with capitalized months.
I want to add "- " to the beginning of each non-blank line.
Tried this:
1,$s/^[A-Z]/- /
and it removes the first letter of the month (e.g. "- une" when it should be "- June")
How can I add that letter back? Or is there a "better" way of doing this for a large document where I need to keep the blank lines untouched.
try this, it should give what you want
%s/^[A-Z]/- &/
or use :g:
:g/^[A-Z]/s/^/- /
You have an answer that would either add the matched text back in, or avoid matching the text altogether during the substitution. Another method is to specify the match as zero-width, or set the ending of the match, so that the text is matched but not replaced:
%s/^[A-Z]\#=/- /
or
%s/^\ze[A-Z]/- /
See :help zero-width, :help /\#=, :help /\ze.
I want to comment all lines that match a particular string. I am doing assembly programming, so lines are commented using ";" character.
The string (pattern) may be present anywhere within the line. The comment should be added to the beginning of the line (obviously).
Alternatively:
:g/pattern/s/^/;
I'd say
:g/pattern/norm I;
(assuming ; is the comment character, and pattern is what you're looking for)
See also: |I| in insert.txt docs
:s command add a ; to the beginning of each matched line:
:%s/.*pattern/;&
Use the visual mode to select the lines on which you want to comment.
Go to the line from which you want to start the comment from.
Press shift+V - string pattern matching this will select the whole line.
Now press j if you want to select lines down below from the current line otherwise press k if you want to select current line + lines above.
I have a large amount of text that is formatted like:
123123|abcabc|text
text
text
123124|abcabc|text
textext
text
The goal is to get the text to by arranged like this:
123123|abcabc|texttexttext
123124|abcabc|texttext text
So that each entry is place onto its own line followed by a carriage return separating it from the next line.
I've tried to do something like search for the | symbol, and then I made a macro to search for every 3rd | symbol, go to the previous word, and insert a carriage turn.
nnnbi<CR><Esc>
but I couldn't get it to repeat. I'm a very new user, any direction would be hugely helpful.
Thanks very much!
I think this command could help you:
g/|/j3
if you don't want to have the spaces after the join, add a "bang[!]".
g/|/j!3
see :h :join for details
EDIT
then this line should work
:v/|/-1 j!
or another option would work too:
:v/|/normal! I^U
note that the ^U you should press Ctrl-vCtrl-u
One way could be to use :v with pattern | that will execute next command for every line that doesn't contain a pipe. The command will be X that deletes character previous to cursor position, and because cursor always will be in first line it deletes previous newline character that will join both lines, and so on until the end:
:v/|/normal X
It yields:
123123|abcabc|texttexttext
123124|abcabc|texttextexttext
I have a substitute command that captures and displays submatch() values in the replacement string. But I have another line of information that I want to parse below this line. That line is always the first line after an empty line, though the number of lines TO that empty line varies. For example:
The first important line I want to capture is here
Stuff I don't want.
A few more lines of stuff I don't want...
Second line I want to capture.
This pattern repeats a hundred or so times in a document. I can substitute "The First Important Line" fine, but shouldn't that search pattern include a way to jump down to the first empty line and then pick up the next "Second line I want to capture." ?? I could then place the contents of that second line into submatch parenthesis and substitute them where needed (right?).
If so, I cannot discover the way to extend the first search pattern to capture the "Second line" Suggestions or correcting my approach would be greatly appreciated.
Someone has already dealt with a similar problem. Below I provide their solution and the detailed description.
/^\nF\d\_.\{-}\_^\n\zs.*/+
It means "Find a block of lines that start with F and a digit,
then scan forward to the next blank line and select the line after that."
Part of regex
Meaning
^\n
Matches the start of a line, followed by a newline - i.e a blank line
F\d
The next line starts with an F followed by a digit
\_.\{-}
\_. is like ., but also matches newline. \{-} matches the minimum number of the preceeding \_.. (If I were to use * instead of \{-}, it would match to near the end-of file.)
\_^\n
Matches a blank line. \_^ is like ^, but ^ only works at the start of a regular expression.
\zs
When the match is finished, set the start of match to this point. I use this because I don't want the preceding text to be highlighted.
.*
Matches the whole line.
The + after the regular expression tells Vim to put the cursor on the line after the selection.
I think I read about offsets, but I can't find the bit in the help that is relevant right now. As such, my other solution would be to record a macro to do what you want:
qa/[Your pattern]<CR>jddq
You could then execute this macro with #a and repeat with ##; or run it a lot of times (e.g., 999#a).