Assuming you have a file with Vim commands, you can apply that to a file using the Vim editor's -S
option:
$ cat edits
%s/\<import\>/IMPORT/g
%s/\<module\>/MODULE/g
%s/\<bumblebee\>/BUMBLEBEE/g
%s/\<please\>/PLEASE/g
%s/\<for\>/FOR/g
%s/\<over\>/OVER/g
%s/\<do\>/DO/g
%s/\<done\>/DONE/g
wq
$ cat program
module top.
import "library".
for i over <zoidberg> -> do
bumblebee i:th, please.
done
$ vim -S edits program
$ cat program
MODULE top.
IMPORT "library".
FOR i OVER <zoidberg> -> DO
BUMBLEBEE i:th, PLEASE.
DONE
Note that the last wq
in the edits
file will save the document with the same filename as the original.
Note also that this is an unusual way to edit files from the terminal and that it's more common to use a stream editor such as sed
(see below). The main issues with doing things this way is that Vim is not a non-interactive editor, and that it would read the whole document into memory before executing the editing script (which is inelegant when it's not needed). It's also cumbersome for you to have to maintain an editing script rather than a simple list of keywords.
Assuming you have a file with a list of lowercase keywords that needs to be turned into uppercase, one keyword per line.
A simple Perl program would turn the list into a sed
editing script:
$ cat keywords
import
module
bumblebee
please
for
over
do
done
$ perl -pe 's{.*}{s/\L\\<$&\\>/\U$&\L/g}' keywords
s/\<import\>/IMPORT/g
s/\<module\>/MODULE/g
s/\<bumblebee\>/BUMBLEBEE/g
s/\<please\>/PLEASE/g
s/\<for\>/FOR/g
s/\<over\>/OVER/g
s/\<do\>/DO/g
s/\<done\>/DONE/g
What the Perl code is doing is simply to take each line and insert it into the sed
expression
s/\<word\>/WORD/g
where word
and WORD
is the string on one of the lines in the keywords
file, lower-cased and then upper-cased.
This could then be applied to your program's source code:
$ cat program
module top.
import "library".
for i over <zoidberg> -> do
bumblebee i:th, please.
done
$ perl -pe 's{.*}{s/\L\\<$&\\>/\U$&\L/g}' keywords | sed -f /dev/stdin program
MODULE top.
IMPORT "library".
FOR i OVER <zoidberg> -> DO
BUMBLEBEE i:th, PLEASE.
DONE
With -f /dev/stdin
, sed
is able to read the editing script from the output of Perl, and then apply it to the given file (here, the file called program
).
Note that this simplistic substitution of words would not know about the quoting rules of your language, so it would replace any keywords found in strings etc. that may not need to be replaced.
-f file
option, to read a list ofs/pattern/replacement/g
(ors/pattern/\U&/g
) commands fromfile
– steeldriver Apr 8 at 19:19