Sed strings make VIM very powerful. You can use them to do many things. The changes will not be written to the file until you save the file, so you are safe to experiment a bit.
To delete lines 4-12:
:4,12 d
To delete the current line:
:d
To replace the word foo with the word bar starting at the line where the cursor is through the end of the file:
:.,$s/foo/bar/
To add the word foo at the end of lines 24 – 34:
:24,34 s/$/foo/
To remove the spaces in the middle of a pip list output line and replace it with == so it can be used to install (if you have to recreate your pyenv):
1.) remove the header lines:
:1,2 d
2.) replace the spaces with double equals:
:%s/ +/==/g
To remove == and everything after from a pip list so that pip will install the newest version:
:%s/==.*//
To remove all lines that contain the strings error, warn, or fail (remove the /d
to show the lines that the command will delete):
:g/error\|warn\|fail/d
To remove all lines that don’t contain the strings error, warn, or fail (remove the /d
to show the lines that the command will delete):
:g!/error\|warn\|fail/d
v
can replace the g!
if you prefer:
:v/error\|warn\|fail/d
To reformat a paragraph in vim:
1. Use <CTRL-J>
to join all lines in the paragraph
2.
:gq
To remove all commented and blank lines from a file (remove the /d
to show the lines that the command will delete):
:g/\v^(#|$)/d
To change the last character in a line to a semi-colon:
:%s/.{1}$/\;/
To replace all single quotes in a file with double quotes:
:%s/'/\"/g
To remove all extra spaces in all lines of a file:
:%s/ +//g
Use g to execute a cmd on regex to delete blank lines:
:g/^$/d
Substitute starting string to eol with newstring followed by comma:
:% s/<string>.*/<newstring>,/
Substitute invalid non-printable characters (U+00A0) with spaces:
%s/\%u00a0/ /g
Remove everything on line from the comma to eol:
:%s/,.*//
Remove all lines not beginning with IPv4 IP:
:%v/^\(\d\+\.\)\{3}\d\+/d