Sub

sub v0.1.2 Usage: sub COMMANDLINE – (PATTERN/SUBSTITUTION)+ (/GLOBAL_FLAGS)?

sub substitutes the matching pattern in every word in the command line with the substitution. Only the first matched pattern in the word is substituted unless the ‘g’ flag is given. There are various options that change the way substitutions are performed or the way matches are made. Patterns can have regular expression wildcards (special characters) in them, and the regular expression engine used is Ruby’s. Special characters are interpreted as special unless the ‘L’ flag is given.

See [docs.ruby-lang.org/en/master/Regexp.html#class-Regexp-label-Special+Characters] for more information on the special characters that can be used inside patterns.

The sub utility is useful mainly when editing long commandlines, so you don’t have to <ctrl-b> and <ctrl-f> (or <left> and <right>) through the commandline to edit it. The examples given here don’t feature long commandlines for the sake of being brief and explanatory.

Ex: sub ld -m elf_i386 ex4.o -o ex4 – ex4/ex5

This will output the following, and then execute it:
ld -m elf_i386 ex5.o -o ex5

Multiple substitutions:

Multiple substitutions can be given as well, such as:

Ex: sub ld -m elf_i386 ex4.o -o ex4 -- ex4/ex5 i386/arm64
    ld -m elf_arm64 ex5.o -o ex5

If multiple substitutions are given, each subsitution pattern is matched with
each word, then the next substitution pattern does the same.

Global flags:

Global flags can be given as the last argument:

ex: sub find . | grep '.txt' -- txt/md /p
In this example, the print flag is given as a final argument.
See flags information below.

Flags:

Substitution flags: -f: Substitute first matching word only.

	  ex: sub wget https://wget.com -- wget/curl/f #=> curl https://wget.com

-l: Substitute last matching word only

	  ex: sub ls -al -- l//l #=> ls -a

-L: Interpret wildcards in pattern as literals

	  ex: sub cp here.txt there.txt -- ./_/L #=> cp here_txt there_txt

-i: Set pattern to ignore the case of the match

	  ex: sub cp here.txt there.txt -- CP/mv/i #=> mv here.txt there.txt

-g: General substitution: substitute all matches in the word, not just the first

	  ex: sub cp here.txt there.txt -- ./_/g #=> __ ________ _________

-e: Expand * in commandline after replacements are made

	  ex: sub ls mydir -- mydir/*/e #=> ls bin README

Global flags: -p: Print the command instead of executing it (adds newline) -P: Print the command instead of executing it (doesn’t add newline) -c: Copy the command to clipboard instead of executing it -I: Set interactive mode: shows the command and asks if you want to execute it -v: Set verbose mode -D: Set debug mode Other flags: -h,–help: Show help (-h shows short help, –help shows full) -v,–version: Show version followed by a newline

Install: gem install cmdline-sub or: cd $HOME git clone [email protected]:luke-gru/sub.git cd sub sudo rake install # creates /usr/local/bin/sub # or # INSTALL_PREFIX=“/bin” sudo rake install

Uninstall: gem uninstall cmdline-sub or: cd $HOME/sub sudo rake uninstall # removes /usr/local/bin/sub # or # INSTALL_PREFIX=“/bin” sudo rake uninstall # removes /bin/sub

License: MIT