Remove until end of the line after the same character from different locations in multiple lines [duplicate] - vim

This question already has answers here:
Delete all characters after "." in each line
(5 answers)
Closed 17 days ago.
I want to remove anything from : to the end of line in all of the following lines in Vim:
key1: A
key2_long: B
key3_longerrr: C
key: D
So the result would be:
key1
key2_long
key3_longerrr
key
For a single line I use f:d$. What are better ways to do this for all lines, wihtout repeating f:d$ for all lines?

:%s/:.*//g
It searches and replaces what matches the regex between the first and second / with what is between the second and third in all the file.
you can find more on this in the documentation, look for substitute

If you want to stick with f:d$ (which should be f:D), you can use :normal for a one-off macro:
:,+3normal f:D
or:
vjjj:normal f:D
See :help :range and :help :normal.

Related

How to search and substitute inside Vim? [duplicate]

This question already has answers here:
How can I replace a pattern only on lines that do or do not contain another pattern?
(2 answers)
Closed 1 year ago.
Is there a way to specifically search for a pattern first and then replace using %s
Why I specifically chose vim is because I can easily make change using :wq
Consider I got a file abc.txt where I need to replace all two's with three:
cat abc.txt
one two
two two
one two
But I only need to replace only those lines that is having "one".
Is there a way to first grep or filter lines having "one" inside vim and then replace using
:%s/two/three/g.
I need a one-liner command that can filter out all lines having "one" and perform substitution.
Sample output
cat abc.txt
one three
two two
one three
Many thanks in advance!
Use a Capture Group or a Look Behind Pattern to Solve Substitution
This ex command should meet your needs:
:`%s/\(one\) two/\1 three/
This is also a classic look behind pattern matching problem (:help /#<=).
:%s/\(one\)\#<= two/ three/
Another approach might be to use \zs, \ze pattern matching technique. Look up :help \zs.
%s/one \zstwo\ze/three/

How to delete to the last blank characters on the line [duplicate]

This question already has answers here:
How can you automatically remove trailing whitespace in vim
(14 answers)
Closed 9 years ago.
I have:
int x = 1;______
(underscores means spaces)
and I would like to get:
int x = 1;
My naive solution is $bld$, is there a quickest way?
In Emacs I use M-\ (delete-horizontal-space)
For the current line:
:s/\s\+$
For all lines:
:%s/\s\+$
The substitution text can be omitted if blank, so we don't need to write s/\s\+$//.
I do this with a search and replace mapping:
map <leader>W :%s/\s\+$//<CR>:let #/=''<CR>
:%s/\s\+$// deletes all trailing white space and then :let #/='' clears the search register.
:%s/\s\+$//
What it does is it searches for white spaces at the end of the line and replace them by nothing.
Source: http://vim.wikia.com/wiki/Remove_unwanted_spaces

vim - Append next line to end of current [duplicate]

This question already has answers here:
How do I join two lines in vi?
(9 answers)
Closed 9 years ago.
I'm sure this has been asked before, but I haven't had much luck with a search. Is there an easy way to append the next line to the end of the current line?
Example:
this.thing = that
.getThing();
I am looking for a one-stroke command that turns it into:
this.thing = that.getThing();
Shift-J joins the next line with the current one. It removes indentation but usually adds a space, unfortunately.
These commands, except "gJ", insert one space in place of the <EOL> unless
there is trailing white space or the next line starts with a ')'. These
commands, except "gJ", delete any leading white space on the next line.
Use J which is short for join.
Vim doc copied below
*J*
J Join [count] lines, with a minimum of two lines.
Remove the indent and insert up to two spaces
Adding to the other answers, if you want to keep the cursor position you can use a mark:
:nnoremap J mzJ`z
mz - add mark to register z
J - join as usual
`z - go to the mark set

Adding words in front and at the end of line in VIM [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Replace while keeping certain “words” in vi/vim
Let's say I have several lines like
$adv_id;
$am_name;
$campaign_ids;
$repeat_on;
$opt_days;
$opt_time;
$am_or_pm;
Let's say I use visual mode to select all the lines.. how can I add characters at the start and the end of each line so it looks something like
$_REQUEST($adv_id);
$_REQUEST($am_name;
$_REQUEST($campaign_ids);
$_REQUEST($repeat_on;
$_REQUEST($opt_days);
$_REQUEST($opt_time);
$_REQUEST($am_or_pm);
Pretty similar to your other question, so the explanation there should help you to understand this substitute. With the lines selected as a visual block, use this substitute command:
:'<,'>s/\$\(.*\);/$_REQUEST(\1);
As before, the '<,'> will be auto-filled for you in the command-line if you have a visual selection.
The difference here is that we're using \(\) to make a capturing group, which will capture part of the regex, and use it again in the replacement, using \1, which refers to the first capturing group.
Also, since this regex uses $ literally to position the replacement, it needs to be escaped: \$, since it has a special meaning in the regex: end of line.
If you'll need to have multiple replacements on a single line, you'll need to add the g flag, and you may want to remove the semicolon:
:'<,'>s/\$\(.*\);/$_REQUEST(\1)/g
The reverse regex, e.g. replacing $_REQUEST($adv_id); with $adv_id;, is pretty similar:
:'<,'>s/\$_REQUEST(\(.*\))/\1
Here we capture everything between the parens in a $_REQUEST(...); in a capturing group, and that capturing group is the entire replacement.
In visual mode, hit : and use this in the command line:
:'<,'>s/^\(.*\);$/$_REQUEST(\1);/g
The \( and \) capture the matched expression for the line and the \1 recalls the captured group in the substitution.
Using the :'<,'> tells Vim to filter the current selection through the following command (which is s in this case).

Vim: Convert hard wrap to soft wrap [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Unwrap text in vim
How to convert hard wrap into soft wrap in a text file, using vim or (if simpler) some other standard GNU/Linux tool? And this while preserving paragraphs as such. It would be easy to remove all line breaks, but not serviceable.
By hard wrap I mean a document where each line ends at most at (for example) column 80.
I noticed that the program aquamacs has a function to do this, but I don't know what its output looks like, and it is Mac OS X only.
You can do this:
:%norm vipJ
It will unwrap all the paragraphs in you text. However, if you want to do it manually, simply do ipJ inside visual mode on each paragraph you want to unwrap.
I also found another way to achieve this
:g/^\s*\n.*\S$/+norm vipJ
which means:
:g "Execute command when pattern matches
Pattern:
^\s*\n "A line with only spaces or tabs (or none)
\n.*\S$ "A line with anything but ending with a non-space character
Command:
+norm vipJ "Join the lines in the paragraph
Please note that you'll need an empty line before the first paragraph too.

Resources