Preparing text editors for scripting
Throughout the book, I will be working on the command line of Raspberry Pi and this will include the creation and editing of the scripts. You, of course, can choose the way you wish to edit your script and may prefer to make use of a graphical editor and I will show some settings in gedit. I will make one excursion to a Red Hat system to show screenshots of gedit in this chapter.
To help make the command line editor easier to use, we can enable options and we can persist with these options through hidden configuration files. The gedit and other GUI editors and their menus will provide similar functionality.
Configuring vim
Editing the command line is often a must and is a part of my everyday life. Setting up common options that make life easier in the editor give us the reliability and consistency you need, a little like scripting itself. We will set some useful options in the vi or vim editor file, $HOME/.vimrc
.
The options we set are detailed in the following list:
showmode: Ensures we see when we are in insert mode
nohlsearch: Does not highlight the words that we have searched for
autoindent: We indent our code often; this allows us to return to the last indent level rather than the start of a new line on each carriage return
tabstop=4: Sets a tab to be four spaces
expandtab: Converts tabs to spaces, which is useful when the file moves to other systems
syntax on: Note that this does not use the set command and is used to turn on syntax highlighting
When these options are set, the $HOME/.vimrc
file should look similar to this:
setshowmodenohlsearch setautoindenttabstop=4 setexpandtab syntax on
Configuring nano
The nano text edit is increasing in importance and it is the default editor in many systems. Personally, I don't like the navigation or the lack of navigation features that it has. It can be customized in the same way as vim. This time we will edit the $HOME/.nanorc
file. Your edited file should look something like the following:
setautoindent settabsize 4 include /usr/share/nano/sh.nanorc
The last line enables syntax highlighting for shell scripts.
Configuring gedit
Graphical editors, such as gedit, can be configured using the preferences menu and are pretty straight forward.
Enabling tab spacing to be set to 4 spaces and expanding tabs to spaces can be done using the Preference | Editor tab, as shown in the following screenshot:
Tip
Downloading the example code
You can download the example code files from your account at http://www.packtpub.com for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.
Another very useful feature is found on the Preferences | Plugins tab. Here, we can enable the Snippets plugin that can be used to insert code samples. This is shown in the following screenshot:
For the rest of the book, we will be working on the command line in and in vim; feel free to use the editor that you work with best. We have now laid the foundations to create good scripts and although whitespace, tabs, and spaces in bash scripts are not significant; a well laid out file with consistent spacing is easy to read. When we look at Python later in the book, you will realize that in some languages the whitespace is significant to the language and it is better to adopt the good habits early.