Vim replace two words with one another - vim

How would one replace all instances of 'foo' with 'bar' and 'bar' with 'foo' in vim?

Take a look at this: how to write only one pattern to exchange two strings in two-ways in vim
:s/foo\|bar/\={'foo':'bar','bar':'foo'}[submatch(0)]/g

Aside from using a temporary word for the change, you could also use abolish plugin like this:
:%SubVert/{foo,bar}/{bar,foo}/g

:%s/foo/bbaarr/g
:%s/bar/foo/g
:%s/bbaarr/foo/g
It must exist an smartest way to do it, but this one will work for sure !

You can do it using temp word. Just be sure that it doesn't exists in the current document.
/\<asd123\>
:%s/\<foo\>/asd123/g
:%s/\<asd123\>/bar/g
:%s/\<bar\>/foo/g

Related

How to change var[foo] to foo using Vim substitution?

Today I came across this situation where I need to change var[name] to var [name] and a lot of other place where name could be description or other string. Can I do something like the following command:
%s/var[$variable]/$variable/g
Based on the title of the question, the following expression should change var[foo] to foo:
:%s/\vvar\[(.{-})\]/\1/g
But in the details you mentioned that you need to change var[name] to var [name], following should do that:
:%s/\v(var)(\[.{-}\])/\1 \2/g
Other ways of doing the second substitution from comments:
:%s/var\zs\ze\[.*\]/ /g
or
:%s/var\zs\ze\[[^]]*\]/ /g
Related: (g)vim replace regex
The thing to know, if you already know about regex and captures in general, is in this context you apparently have to escape the group characters, rather than it being the other way around:
%s/var[\(variable\)]/\1/g
Without more info, I can't be more helpful.

Deleting specific patterns without deleting the whole lines

Say I want to remove all comment blocks in my source code without deleting the whole lines they are on.
It's possible to achieve this using the
:%s/\/\*.*\*\//
command. I was wondering, is there a specific delete command for this, or is replacing the matched pattern the best approach? The difference most likely wouldn't be much, I'm just curious.
Replacing with nothing really is the idiomatic 'delete this pattern' operation.
:%s/pattern//g
If you want to blank all lines that contain a pattern, like in your example, the obvious solution is to add wildcard matches around the pattern.
:%s/.*pattern.*//
An alternative is to use :global with a normal mode or Ex command. These two achieve the same thing:
:g/pattern/normal! S
:g/pattern/delete|put! _
By the way, while I don't recommend using abbreviated command names in scripts or in code that other people might see, I think it's fine to use them interactively. Thus I tend to abbreviate such commands as :g/pattern/norm! S and :g/pattern/d|pu!_.

vim short for search replace with space in between

What is the vim short for this case?
A BCD_x 1.pdf A BCD_x 2.pdf
Desired output:
A BCD_x 1.pdf:::A BCD_x 2.pdf
You mean something like this?
:s/\s\(A\s\)/:::\1
Or how about this?
:s/\sA\#=/:::
Of course, as always with regexp search-and-replace everything depends on the particulars of your data. Of which you haven't given us much to work with.

Vim - How to move the result of a search to the beginning of the file?

I want to search some text and move the entire line where the text belongs to the beginning of the file. Just that.
How about the simple move command?
:g/^C/m0
:g/^B/m0
:g/^A/m0
:g/regex/norm dd1Gp
Well, what I'm gonna suggest is a primitive answer as primitive it can get. But nothing else springs to mind currently.
:g/A ... some text not including A, B or C.../d
(will tell you how many lines it has yanked)
and then you go to the beginning of the file and, for example
5P
Although, if cases are as simple as this, maybe sorting lines by first letter .... I've never done anything similar but look for older questions.

Advanced searching in Vim

Is there a way to search for multiple strings simultaneously in Vim? I recall reading somewhere that it was possible but somehow forgot the technique.
So for example, I have a text file and I want to search for "foo" and "bar" simultaneously (not necessarily as a single string, can be in different lines altogether).
How do I achieve that?
/^joe.*fred.*bill/ : find joe AND fred AND Bill (Joe at start of line)
/fred\|joe : Search for FRED OR JOE
Actually I found the answer soon after I posted this (yes I did google earlier but was unable to locate it. Probably was just searching wrong)
The right solution is
/(foo\|bar)
#Paul Betts: The pipe has to be escaped
Vim supports regular expressions by starting in command mode with a '/'.
So using something like "/(foo\|bar)" (as was stated before) would solve the problem. It's good to know why that works and what you are using (regular expressions).
/(foo|bar)

Resources