OnWorks Linux and Windows Online WorkStations

Logo

Free Hosting Online for WorkStations

< Previous | Contents | Next >

Using A Text Editor

Text editors can be invoked from the command line by typing the name of the editor fol- lowed by the name of the file you want to edit. If the file does not already exist, the editor will assume that we want to create a new file. Here is an example using gedit:


[me@linuxbox ~]$ gedit some_file

[me@linuxbox ~]$ gedit some_file


This command will start the gedit text editor and load the file named “some_file”, if it exists.

Graphical text editors are pretty self-explanatory, so we won't cover them here. Instead, we will concentrate on our first text-based text editor, nano. Let's fire up nano and edit the .bashrc file. But before we do that, let's practice some “safe computing.” When- ever we edit an important configuration file, it is always a good idea to create a backup copy of the file first. This protects us in case we mess the file up while editing. To create a backup of the .bashrc file, do this:


[me@linuxbox ~]$ cp .bashrc .bashrc.bak

[me@linuxbox ~]$ cp .bashrc .bashrc.bak


It doesn't matter what we call the backup file, just pick an understandable name. The ex- tensions “.bak”, “.sav”, “.old”, and “.orig” are all popular ways of indicating a backup file. Oh, and remember that cp will overwrite existing files silently.

Now that we have a backup file, we'll start the editor:



[me@linuxbox ~]$ nano .bashrc

[me@linuxbox ~]$ nano .bashrc


image

Once nano starts, we’ll get a screen like this:



GNU nano 2.0.3

File: .bashrc

GNU nano 2.0.3


# .bashrc


# Source global definitions if [ -f /etc/bashrc ]; then

. /etc/bashrc

fi


# User specific aliases and functions

# .bashrc


# Source global definitions if [ -f /etc/bashrc ]; then

. /etc/bashrc

fi


# User specific aliases and functions


[ Read 8 lines ]

^G Get Help^O WriteOut^R Read Fil^Y Prev Pag^K Cut Text^C Cur Pos

^X Exit ^J Justify ^W Where Is^V Next Pag^U UnCut Te^T To Spell

[ Read 8 lines ]

^G Get Help^O WriteOut^R Read Fil^Y Prev Pag^K Cut Text^C Cur Pos

^X Exit ^J Justify ^W Where Is^V Next Pag^U UnCut Te^T To Spell



image

Note: If your system does not have nano installed, you may use a graphical editor instead.


image

The screen consists of a header at the top, the text of the file being edited in the middle and a menu of commands at the bottom. Since nano was designed to replace the text edi- tor supplied with an email client, it is rather short on editing features.

The first command you should learn in any text editor is how to exit the program. In the case of nano, you type Ctrl-x to exit. This is indicated in the menu at the bottom of


the screen. The notation “^X” means Ctrl-x. This is a common notation for control characters used by many programs.

The second command we need to know is how to save our work. With nano it's Ctrl-

o. With this knowledge under our belts, we're ready to do some editing. Using the down arrow key and/or the PageDown key, move the cursor to the end of the file, then add the following lines to the .bashrc file:


umask 0002

export HISTCONTROL=ignoredups export HISTSIZE=1000

alias l.='ls -d .* --color=auto' alias ll='ls -l --color=auto'

umask 0002

export HISTCONTROL=ignoredups export HISTSIZE=1000

alias l.='ls -d .* --color=auto' alias ll='ls -l --color=auto'


image

Note: Your distribution may already include some of these, but duplicates won't hurt anything.


image

Here is the meaning of our additions:


Table 11-4: Additions to our .bashrc


Line Meaning

Line Meaning

umask 0002 Sets the umask to solve the problem with shared directories we discussed in Chapter 9.


image

export HISTCONTROL=ignoredups Causes the shell's history

recording feature to ignore a command if the same command was just recorded.


image

export HISTSIZE=1000 Increases the size of the command history from the usual default of 500 lines to 1000 lines.


image

alias l.='ls -d .* --color=auto' Creates a new command called

l.” which displays all directory entries that begin with a dot.


image

alias ll='ls -l --color=auto' Creates a new command called

ll” which displays a long format directory listing.


image


As we can see, many of our additions are not intuitively obvious, so it would be a good idea to add some comments to our .bashrc file to help explain things to the humans. Using the editor, change our additions to look like this:



# Change umask to make directory sharing easier

umask 0002


# Ignore duplicates in command history and increase

# history size to 1000 lines export HISTCONTROL=ignoredups export HISTSIZE=1000


# Add some helpful aliases

alias l.='ls -d .* --color=auto' alias ll='ls -l --color=auto'

# Change umask to make directory sharing easier

umask 0002


# Ignore duplicates in command history and increase

# history size to 1000 lines export HISTCONTROL=ignoredups export HISTSIZE=1000


# Add some helpful aliases

alias l.='ls -d .* --color=auto' alias ll='ls -l --color=auto'


Ah, much better! With our changes complete, press Ctrl-o to save our modified

image

.bashrc file, and Ctrl-x to exit nano.


Why Comments Are Important

Whenever you modify configuration files it's a good idea to add some comments to document your changes. Sure, you'll probably remember what you changed to- morrow, but what about six months from now? Do yourself a favor and add some comments. While you're at it, it’s not a bad idea to keep a log of what changes you make.

Shell scripts and bash startup files use a “#” symbol to begin a comment. Other configuration files may use other symbols. Most configuration files will have comments. Use them as a guide.

You will often see lines in configuration files that are commented out to prevent them from being used by the affected program. This is done to give the reader suggestions for possible configuration choices or examples of correct configura- tion syntax. For example, the .bashrc file of Ubuntu 14.04 contains these lines:

# some more ls aliases

#alias ll='ls -l'

#alias la='ls -A'

#alias l='ls -CF'



image

The last three lines are valid alias definitions that have been commented out. If you remove the leading “#” symbols from these three lines, a technique called un- commenting, you will activate the aliases. Conversely, if you add a “#” symbol to the beginning of a line, you can deactivate a configuration line while preserving the information it contains.


Top OS Cloud Computing at OnWorks: