1

I'm trying to make a convenience function to fix issues when I accidentally have my caps locks on and am trying to run some case-sensitive tools.

e.g. I occasionally find myself typing MAKE DEBUG instead of make debug.

What I have now is pretty straightforward: alias MAKE="make" and editing the makefiles to duplicate the rules, e.g. DEBUG: debug.

I'd prefer a solution that works on the arguments, without having to modify the tools involved.

3
  • 1
    you actually use that caps lock key? impressive ;-) Commented Jun 22, 2012 at 22:35
  • I find myself using a lot of libraries that really love to DEFINE_EVERYTHING_IN_CAPS. Commented Jun 22, 2012 at 22:41
  • That really is impressive. Me, I just hurt my hands a lot by contorting my fingers to hold down shift while typing REALLY_LONG_NAMES_OF_VARIABLES_OR_WHATEVER. Commented Jun 22, 2012 at 22:46

5 Answers 5

2

Using GNU sed

If you just want everything in your makefile to be in lowercase, you can use GNU sed to lowercase the whole thing:

sed -i 's/.*/\L&/' Makefile 

You could also build a sed script that's a little more discriminating, but the \L replacement escape is your friend.

Using tr

Since you tagged your question with tr, you might also want a tr solution. It's a little more cumbersom, since tr won't do in-place translations, but you could shuffle temp files or use sponge from moreutils. For example:

tr '[[:upper:]]' '[[:lower:]]' < Makefile | sponge Makefile 
Sign up to request clarification or add additional context in comments.

Comments

1

This involves a script, but avoids the Ctrl-D issue of my earlier attempt:

For each command, an alias like

alias MAKE="casefixer make" 

And then the following file, which I've created at /usr/local/bin/casefixer:

#!/bin/bash command=`echo $1 | tr '[:upper:]' '[:lower:]'` # convert 1st arg to lowercase, it's the command to invoke shift # remove 1st arg from $* $command `echo "$*" | tr '[:upper:]' '[:lower:]'` # convert arguments to lowercase, and invoke the command with them 

Comments

1

Playing on @Clayton Hughes' casefixer solution, here's a solution that'll handle funny things like spaces in arguments (which $* messes up):

casefixer() { eval "$(printf "%q " "$@" | tr '[:upper:]' '[:lower:]')"; } alias MAKE='casefixer make' 

Note: eval is a fairly dangerous thing, with a well-deserved reputation for causing really bizarre bugs. In this case, however, the combination of double-quoting and encoding the command and its arguments with %q should prevent trouble. At least, I couldn't find a case where it did anything unexpected.

Comments

0

Here's one solution, though it's not perfect:

alias MAKE="make `tr '[:upper:]' '[:lower:]`" 

it works, but has the unfortunate problem that I need to press Ctrl-D to send an EOF before anything starts executing.

Comments

0

The readline command "downcase-word" (bound to M-u by default) is worth mentioning here. Suppose you typed "MAKE DEBUG". If you catch it before hitting return, you can move the cursor to the beginning of the line with C-a. (Otherwise, bring the command back first, using the up arrow). Then, each time you hit M-u, the word immediately after the cursor will be changed to lowercase, and the cursor will move to the beginning of the next word.

It's a little laborious, and I don't see a way to lowercase the entire line at once. Perhaps someone can improve on this.

2 Comments

M-9M-l (letter ell) will downcase the next 9 words. M-99M-l will downcase the next 99. M--M-l will downcase the previous one word. M--3M-l (release Alt before pressing 3 if you're using it instead of Esc) the previous 3. M-u is bound to upcase-word by default at least on my system.
I have to admit my ignorance here. What does the X-x syntax mean? How would I enter it?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.