If you are on a POSIX compliant machine (OSX or Linux) you can make use of Shell Scripts to do day-to-day donkey work. I use Shell Scripts for backups, cleaning out temporary folders, profiling and logging. Writing these scripts is pretty straightforward and if you are completely new to it I recommend getting a copy of Classic Shell Scripting.
Recently I came across a couple of shortcuts that have greatly reduced the time it takes to write a script in the vi editor.
Using :! from within vi you can run a shell command. This is great as you don't need to exit vi to test your code. Let's say for example your script is called myscript.sh. You can run your script from within the vi editor by using.
Furthermore you can make more changes in vi, save the file and then run the script again by using
This is a shortcut to the last command issued to the shell, allowing you to retest your shell script without even typing the filename.
Within vi the % variable refers to the file that you are currently editing. So to run the script you are editing from within vi you can also use:
Have an update or suggestion for this article? You can edit it here and send me a pull request.