Notes about using the vim editor, especially useful regular expressions (regex, regexes).
-
:g/pattern/.!commandIt's important that the dot command (
.) appear between the search pattern and the bang (!) beginning the external command.For example, to run the
revshell command on every line that begins with the string "reverse"::g/^reverse/.!rev -
:g/^\(.*\)$\n\1$/d -
:g/^\#.*$\n#/dThis is similar to removing contiguous duplicate lines, except that the whole contents of the lines don't need to match, they only need to begin with
#. This was helpful when editing a.bash_historyfile. Removing some commands from history left a lot of contiguous history timestamp comment lines, like#1499897624. -
:%!awk '\!a[$0]++'awkprints the first occurrence of each line by keeping an (associative) array of counters with the text of the line as the index. Whenever the counter for a line is zero (or unset), this expression evaluates to true, which causesawkto print it. -
:%s;^\(\s*/\*\*\)\n.*\* \(@.*\)\n.*\*/;\1 \2 */;For example, a PHPDoc like this:
/** * @return bool */
Becomes:
/** @return bool */This helps save screen space, allowing more code to be seen at a time. It's great for declaring the return type of a function (as shown here) or the types of variables (with
@var).This will probably work with document comments for other languages, too, like JavaDoc, etc.