Search code examples
bashnano

Shortcut in nano editor for adding quotation marks to every word beginning with $ in a bash script?


I am new to writing in bash and I just finished this long script but I made the mistake of not adding quotation marks to all the variables beginning with the unary operator $. Adding all the quotation marks by hand is going to take a while. Is there a short cut I can use so all the words in the text file beginning with $ get quotation marks around them? So if a line in the file looks like:

python myProgram.py $car1 $car2 $speed1 $speed2

Then after the shortcut it will appear as

python myProgram.py "$car1" "$car2" "$speed1" "$speed2"

I am writing the script using nano.


Solution

  • Use global search and replace with the expression (\$\w+).

    1. Switch to search and replace mode with C-\.
    2. Switch to regex mode with Alt-R.
    3. Type the expression (\$\w+). Hit Enter.
    4. Type in the replacement expression "\1" replace the captured expression with quotations. Hit Enter.
    5. On the match, hit A for All.