3 ms·
Reason 8: Vim keystrokes can be incorporated into ex scripts, either alone or in "here" scripts. These commands are already useful, they can do stuff like re
by chernevik 15y ago
Reason 8:
Vim keystrokes can be incorporated into ex scripts, either alone or in "here" scripts. These commands are already useful, they can do stuff like replace strings in lines that are N lines above or below lines holding another string, and they can be leveraged further via scripting.
For example, a search and replace across files under the working directory:
tgt_string=$1
repl_string=$2
for file_name in $(grep -rl "$tgt_str")
do
ex - $file_name << END_HERE_SCRIPT
%s/$tgt_string/$repl_string/g
wq
END_HERE_SCRIPT
done
Yes, the indentation of the "here" script is ugly, white space before line texts can bork execution.
Yes, sed or awk can do very similar things, maybe better. My point is, it's nice to get some of these capabilities just by knowing your editor.
(Ed: Sorry about the formatting, if I don't double-space the script it turns into one large text clump.)