Friday, February 27, 2015

How to create an alias to open and edit text documents in Linux

Several years ago, I got in the habit of writing in two documents every morning. A morning page, just like The Artist's Way says and a gratitude list. I write a gratitude list to take note of 10 things I am grateful for each morning. I write a morning page to say everything I want to say before I encounter another human being for the day.

For awhile there, each morning I would open the file manager in my Ubuntu Linux workstation, navigate to the folder where the documents could be found and opened them, manually. Then I got smarter and just opened LibreOffice and found the recent documents list and opened them there. But that still required the mouse. I knew there was something better, something easier. I had been playing with the Bash shell and marveled at it's utility. Hey, if you had spent decades on Windows, you would marvel at Bash, too.

I began to consider how to write a script that would open my documents. So I developed something like this in my favorite text editor, vi:


libreoffice -o "/path/to/document/gratitude list.odt" &
libreoffice -o "/path/to/document/Morning Page.odt" &

Every Bash script starts with a shebang (#!) and the path to the shell (or programming language, i.e. python or java) that will be used to interpret the script. That's what line 1 is about. Line 2 is blank. Lines 3 and 4 contain the commands that open the documents I want to write in every morning. So lets break down line 3 and 4 since they are the same, but they point to different documents. Each line is a command to run LibreOffice, open the document specified and send the process to the background in the shell (that's what the ampersand does in Bash scripting).

LibreOffice is my productivity suite of choice. It's free, open source, and does everything I need to do to create documents like correspondence and spreadsheets. There is a nice presentation application too, if you're into public speaking with illustrations. Oh yeah, it's Microsoft Office compatible, too, so you can share documents with your friends.

LibreOffice can be opened by clicking the icon for it in a menu in Linux or Windows, but you can also run it as a command in Linux. There is even a man page for Libreoffice in Bash, just type:

man libreoffice

With the above command, the options at the command line will be revealed, and we call the output of that command a "manpage". The Libreoffice manpage is how I learned how to write a command to open a new document:

libreoffice -o "/path/to/document" &

I start with the name of the program, followed by the option -o. Then I added the path to the document with an ampersand to send the process to the background. Quotes are required if there are any spaces in the path name or file name. When I run that command, the document opens and I can edit the document, save it, and then close it. When I close the document with ctrl-w, the background process is terminated in the shell, too.

Once I had the script, then I created an alias. The Linux environment has configuration files for everything and Bash is no exception. To create the alias, I added the following line to my .bashrc file:

alias mw='~/path/to/document/'

Then I closed my shell, opened it again and executed the command alias to get a list of aliases that have been loaded in the shell. In this case, I was looking for 'mw' for "Morning Writing", for me. The name of the alias is arbitrary and can be anything you want it to be, excluding special characters that are interpreted by the shell.

I use Gnome 3, the desktop environment for Linux distributions like Ubuntu. It is also known as the Gnome Shell and It's a dream to work with. Its minimalist simplicity and style make it easy for me to navigate to where I want to go. Now with the script and alias in place, from the desktop, I can open my morning documents like so:

Windows key (I know, it's ironic, but it works)
te (for terminal with Bash),
mw (to run the script from the terminal)

No mouse, no hunting around, just seven keystrokes and I'm up and running in a few seconds. You can do this for any document you want, so long as the application you run to open the document has a corresponding command in Bash. As far as I know, every Linux application has a Bash command line option to run it.

Sure, I could create a functional equivalent in Windows, but it's not as easy as Linux. Why? Word doesn't like being called from the command line. I know, I've tried. I could record a macro, but that is recording the mouse movement. In Linux, I got it done with far less effort than in Windows because Linux doesn't hide the motor from me like Windows does.

This is one reason why I use Linux. Once I found a life with Linux, I got bored with Windows, and I never looked back.
Post a Comment