0

I'm not a vi/vim user - yet. I'm learning an older programming language whose compiler insists on uppercase keywords. One learning resource suggests having vi/vim inhale a list of keywords, e.g.

%s/\<import\>/IMPORT/g
%s/\<module\>/MODULE/g

using the command :so

which then gets applied to the current buffer.

I'm looking for a *nix console utility that will do the same.

Does such a utility exists? TIA

6
  • I would convert it on file basis, i.e., cat lower_case_file | tr [:lower:] [:upper:] > upper_case_file – Cinaed Simson Apr 8 at 8:43
  • @CinaedSimson that is going to upcase the whole file .. Isn't the OP wanting just say import (or whatever of the 37 other words they chose upcased)?? – Mr R Apr 8 at 12:22
  • @Mr R Yes! That's what I'm looking for. The vim command is :so caps (caps being a text file containing %s/\<import\>/IMPORT/g etc - a whack of them). The current buffer contains code all in lowercase. After sending the above command - bingo bango all keywords are in uppercase. – duke Apr 8 at 15:10
  • @CinaedSimson please post an actual answer instead of a comment. – terdon Apr 8 at 18:47
  • It sounds like what you want is sed's -f file option, to read a list of s/pattern/replacement/g (or s/pattern/\U&/g) commands from file – steeldriver Apr 8 at 19:19
0

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.

7
  • @duke You don't need that. You just need a file with the keywords. If you want to apply your existing file to a document using vim. you could just use vim -S script program (I'll add this to the answer). What I have shown is what you asked for, using Unix console commands. – Kusalananda Apr 8 at 20:07
  • I know how to do it within vim while in a code buffer => :so <filename> where <filename> has all the replacement patterns – duke Apr 8 at 20:10
  • works like a charm, BUT I want to be able to do it from CLI. – duke Apr 8 at 20:11
  • @duke See updated answer. – Kusalananda Apr 8 at 20:11
  • Right on! vim -S blah blah !!!! Got to give it a try! Thx – duke Apr 8 at 20:14
0

You can similarly use a sed file that has your substitution patterns and then apply that sed file to your target text.

Here are contents of an example sed file named my_custom_substitutions.sed based on your substitutions above:

s/\<import\>/IMPORT/g
s/\<module\>/MODULE/g

You can see how those custom substitutions will look before actually applying it to the file like so:

sed -f my_custom_substitutions.sed targetfile.txt 

To apply those custom substitutions to the target file using sed:

sed -i -f my_custom_substitutions.sed targetfile.txt 


Another way can be to use tr:

echo "lower case text" | tr '[a-z]' '[A-Z]'

If you want to do that from within vim for the line 2 in your file, you can do:

:2 ! tr '[a-z]' '[A-Z]'   

If you wanted to apply that to several lines in a range like line 2 to line 4, you can do:

:2,4  ! tr '[a-z]' '[A-Z]'
6
  • The issue in the question, the way I understand it, is that they want to read in a list of keywords that should be upper-cased, not that they need to upper-case the whole file or particular lines. – Kusalananda Apr 8 at 19:39
  • @Kusalananda yes! – duke Apr 8 at 19:59
  • ok, I updated my answer – EWJ00 Apr 8 at 20:00
  • @EWJ00 Your sed -f my_custom_substitutions.sed targetfile.txt works, but it only prints to screen. I want the targetfile updated as well. > targetfile doesn't work. >> targetfile appends which I don't want. – duke Apr 8 at 20:36
  • This works BEST! sed -i -f my_custom_substitutions.sed targetfile.txt The modified sed command (added -i option) edits the targetfile as well! – duke Apr 8 at 20:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.