Steps

Tutorial 2: Beginner's guide to pagers

Step1 of 3

Step 1 - Opening and closing less

We are going to learn how to open and read the contents of files.We will be doing that in this tutorial using less, a terminal pager
program. It is used primarily to read the contents of a file one screen
(or page) at a time. less is installed on most unix and unix-like systems
so is almost always available to use.

Just like before, let's try and run less on it's own:

less

You should see the following error message:

Missing filename ("less --help" for help)

We could find out how to run less by running it with --help but let's just
try following the suggestion of adding a filename.

Hint: If you don't want to type out a filepath, you can tap tab to
complete the command. Just type less ada_l and then click tab (Tab ↹) this
should auto-complete the filename. We will touch on this in more detail
another time.

less ada_lovelace_bio.txt

This will do something different to the terminal than you've experienced
before. You can use the directional arrow on your keyboard ⇩ to scroll
downwards through the file. Tap the ⇩ arrow until you reach the end of the
file. You will be able to see you've reached the end when you see at the
bottom of the screen:

(END)

You can also use the ⇧ to scroll up through the file.

When you feel comfortable scrolling up and down use

q

to quit the program - you will see that you are back on the command line
again.

Step 2 - How to learn how to use less

Let's do something more interesting with less! You already know how to open,
close and scroll through a file. Let's check out what else we can do with less.

First, let's open the same file again:

less ada_lovelace_bio.txt

NOTE
Remember you can quit this file anytime using q

Ok, now you're in the file type:

h

Like almost everything in unix, this software comes with help - you just need
to know how to access it and work out how to interpret it - this is something
you will gain a feel for over time.

The first thing the help file explains is how to read the help - we'll touch on
that in a minute.

First, note that it says that h brings up the help message
and q causes less to exit. Along with q and h are a bunch of other
symbols. These are just alternatives. So instead of using q you can type
:q (typing : then q) and it would have the same effect.This way of writing alternatives is continued down the page.

The two main sections we are going to look at are the first two sections:
MOVING and SEARCHING in this tutorial.

MOVING in less

Let's check out the first two commands given in the
MOVING SECTION along with the help intro.

Let's interpret these together and try them out on the ada lovelace file.Click q to quit the help so we can try these commands out.

Now what do these commands do? These commands allow you to move one line
forward (down) or backward (up) in a file. Let's try that out:

ey

You should see the file move forward then backward by one line. The other
alternatives should also work for example '^E'.

Remember according to the
beginning of the help file '^' stands for the ctrl key. So to move forward
one line you would need press ctrl and the E key on your keyboard at the
same time. This can also be written as: ctrl+e, ctrl+E,
ctrl-e or ctrl-E.

Both commands have also been marked with *, this is not an alternative key
for moving forward and backward in the file. This was described in the
first part of the help:

SUMMARY OF LESS COMMANDS
Commands marked with * may be preceded by a number, N.

Commands marked with the star can be changed from their default behaviour by
typing a number (N) before typing the command e.g. typing 5 THEN e. The
default behaviour of e is to move forward one line - adding a number just
increases how many lines you're moving forward in the file:

Quit the help page so that you're looking at the Ada Lovelace biography file
again. Just like exiting you can quit the help page by clicking q.

q

The first command says that it'll look at all future lines in the file for a
particular pattern - this pattern can be a word:

/Ada

You should see all mentions of "Ada" are now highlighted in the file and you
can page through the file as normal. If you wish to search for something else
you can just type / followed by your search term:

/Legacy

This should take you to the bottom of the file. Now let's search for "born":

/born

You will get an error message:

Pattern not found (press RETURN)

If you now scroll upwards to the top of the page you will see that 'born' has
been highlighted at the top of the document - you got the error message
because there were no mentions of "born" after the point in the file you were.As the description says - you are searching forward for a matching line.

Now, if we search for "Ada" again and fill the terminal up with highlights:

/Ada

these can be removed by using ESC-u - pressing the ESC key on your keyboard
followed by the u key.

Debugging Scenarios

Help

Katacoda offerings an Interactive Learning Environment for Developers. This course uses a command line and a pre-configured sandboxed environment for you to use. Below are useful commands when working with the environment.

cd <directory>

Change directory

ls

List directory

echo 'contents' > <file>

Write contents to a file

cat <file>

Output contents of file

Vim

In the case of certain exercises you will be required to edit files or text. The best approach is with Vim. Vim has two different modes, one for entering commands (Command Mode) and the other for entering text (Insert Mode). You need to switch between these two modes based on what you want to do. The basic commands are: