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. 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:

#!/bin/bash

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 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 and send the process to the background in the shell. 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 statistics. 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 it can also run 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. That is how I found the command to open a document:

libreoffice -o &

Then I added the path to the document with an ampersand to send the process to the background. 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/script.sh'

Then I closed my shell, opened it again and typed 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.

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. It's 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),
Enter
mw (to run the script from the terminal)
Enter

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. 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 why I use Linux. Once I found a life with Linux, I got bored with Windows, and I get what I want done with greater ease.
Post a Comment