Replace α=ß with α = ß
Got it! If α
and ß
are variables (i.e., any characters), and you want to replace occurrences of X=Y
(where X
and Y
can be any characters) with X = Y
(i.e., adding a space around the =
sign), but leave X = Y
as is, you can use the following Vim command:
:%s/\(\S\)=\(\S\)/\1 = \2/g
Explanation:
:%s
applies the substitution across all lines in the file.\(\S\)
matches a non-whitespace character (the variableX
).=
matches the equal sign.\(\S\)
matches another non-whitespace character (the variableY
).\1
and\2
are backreferences to the characters matched by the first and second\(\S\)
groups, respectively.\1 = \2
replaces the matched text with the first and second characters with spaces around the equal sign.- The
g
flag ensures the replacement happens globally on each line.
Example:
Before:
α=ß
var1=var2
a=b
α = ß
After running the command:
α = ß
var1 = var2
a = b
α = ß
This command will add a space around the equals sign ( =
) only when there is no space already.
Delete everything after the occurrence of a certain keyword till end
Here’s how to do it:
- Open the file in Vim.
- Enter command mode by pressing
Esc
. - Type the following command to remove lines containing
AND |
and everything after it:
:%s/AND \|.*//g
Explanation:
:%s
tells Vim to perform the search and replace on all lines in the file (%
means all lines).AND \|
matches the literal string “AND |”. The backslash\
escapes the pipe symbol (|
) to treat it as a literal character..*
matches everything following “AND |”.//g
tells Vim to replace the matched text with nothing (i.e., delete it).
If you want to remove only the content following AND |
and keep the rest of the line intact, you can use:
:%s/AND \|.*//g
This will remove the part starting from AND |
till the end of the line without removing the whole line.
Remove lines not containing a pattern
%v/pattern/d
scroll simultaneously in 2 windows
:windo set scrollbind
Undo the scrollbind
:windo set noscrollbind
Toggle the scrollbind
:windo set scrollbind!