Lab 02: More Linux and Emacs

Jan 9, 2024  β”‚  m. Jan 26, 2024 by Charlotte Curtis

Objectives

Log in to INS

Follow the instructions from lab 1 to log in to the INS server. Remember, each time you log in to a new session your current working directory is your home directory - not the directory you were last working in. You should have a 1633 directory in your home directory - if not, create it now:

$ mkdir 1633

Then, change into this directory:

$ cd 1633

Git

Configuration

We will be using git for version control and assignment submission in this course. On any new system where you are using git, you must configure your user name and email address. This is done with the following commands:

$ git config --global user.name "Your Name"
$ git config --global user.email youremail@mtroyal.ca

Clone the lab repo

In this course, you will be using a single git repository for all of your labs. To clone your initial copy, run the following command:

$ git clone /library/students/comp1633/labs.git labs

This will create a copy of your lab repository in a directory called labs in your home directory. You can now cd into this directory and see the contents:

$ cd labs
$ ls

For now, there isn’t a lot to see - just a .gitignore file that tells git what kinds of files should not be tracked. We’ll be adding to this directory as the course progresses, and for each lab with an assessment component, there will be new starter code that you can get by running git pull.

Configure your upstream “dropbox”

Do not skip this step! Right now, you have a private copy of the labs repository. In order to submit your work, you need to configure your repository to “push” to a shared repository that your instructor can access. This is done with the following command:

$ git-asg-config

When prompted, enter the following information, choosing the appropriate values for your instructor and section:

FieldValue
Instructor’s user nameccurtis or mkarimifatemi
Course namecomp1633
3-digit section name001 or 002

This can also be stored in a text file in your home directory named .submit.cnf with the following contents and you won’t need to enter it every time:

[student]
instructor=ccurtis
course=comp1633
section=001

Select labs as the assignment to configure. This will create an “upstream” repository to connect your working folder to a submission box. Every time you run git push your submission will be updated; you can continue to push changes until the deadline (1 week from the lab date).

Recall that the git workflow is as follows:

  1. Make changes to your code and save your work
  2. git add the files you want to commit
  3. git commit your changes (with a descriptive message, i.e. git commit -m "Add a descriptive message here")
  4. git push your changes to the submission box

Create a new directory

Create a new directory in the labs directory called intro and change into it:

$ mkdir intro
$ cd intro

This is where you will be working for the rest of this lab.

Introduction to Emacs

So far we’ve done the equivalent of opening up a file browser and navigating the file system. All very useful skills, but Emacs is a surprisingly powerful text editor that we will be using in this course and beyond. It’s got quite the learning curve, so we’re going to start with the basics.

Launch Emacs

First, make sure you are working in your labs/intro directory. Then, run the following command:

$ emacs first.txt

This is running Emacs and telling it to open a file named first.txt. If this file doesn’t exist (and it shouldn’t), emacs will create it when you save the file for the first time.

At this point you should see a mostly empty window. Emacs is interactive and full-screen, meaning the program will respond to input you enter and the entire screen will be used to display your file. At this point, the input buffer is empty and the cursor is at the upper-left. The bottom two lines of the screen are status lines. The last line is an action status bar – it displays messages that relate to actions you enter. Notice it currently states “(New file)” because you created a new file.

The second last line is a file status line. It tells you about the file and should currently display something like:

  ! first.txt  1:0 All                                           LF UTF-8  Text

This tells you a few bits of info:

Edit

Start typing! Write whatever you like into the buffer - you should see the 1:0 change to reflect the number of characters you’ve typed, while the ! should change to a *. This indicates that the buffer has been modified and not yet saved.

Save

To save the contents of the buffer to first.txt, type control-x followed by control-s (we will abbreviate such commands as “C-x C-s”). Notice that x and s characters do not appear in the buffer. Input prefaced by the control key are commands to Emacs.

The Emacs editor should respond in the action status line that the file was written, and the * should clear.

Quit

The command to quit Emacs is C-x C-c. If the buffer hasn’t been saved, Emacs will first ask you if you would like to save the file. If you type “n” for “no”, it will ask you to confirm a second time. In this case answer “y” for yes.

Once you are back at the command line, try:

$ ls -al
$ cat first.txt

The file first.txt should be in your directory, and its contents should be displayed as entered earlier. Consult with your instructor if this isn’t the case.

To resume editing the file, just launch Emacs again with first.txt as the argument.

Experiment!

The best way to learn Emacs is to try things. You can run the built-in tutorial with the command C-h t – that is, hold down the CTRL key and hit “h”, then release CTRL and hit “t”. This will open a new buffer with the tutorial, and you can switch back to your first.txt buffer with C-x b, then using the arrow keys to select first.txt and hitting <enter>.

There are also many Emacs reference guides online, such as this printable reference card or this online list of commands . However, our IAs have made a few modifications to the default Emacs configuration to make things a bit easier. Notably, try typing C-x on its own, then wait. You’ll see a list of possible commands. Similarly, typing M-x (“Meta”-x, or holding ALT and hitting x) will display a list of commands that can be scrolled using the arrow keys and even filtered by typing the first few letters of the command you want.

Take some time now to play around with Emacs, getting used to navigating a text document without using your mouse. Eventually you’ll find it’s faster to do things without having to take your hands off the keyboard.

Important: Emacs does not perform automatic “word wrap”. When you create a file that contains long sentences or program instructions, it may look as though Emacs is starting a new line each time the right hand edge of the screen is reached. In reality though, the line is just getting longer; this is indicated by a backslash, “\” as the last character in a line.

To ensure that your files are readable, remember to press the <enter> key to manually begin a new line. Try to limit each line to 80 characters or less, about the maximum comfortable width for easy readability.

Commit your work

Now that you’ve made some changes to your first.txt file, commit them into your git repository by following these steps:

$ git add first.txt
$ git commit -m "My first commit"
$ git push

(You don’t need to use the message “My first commit” - you can use whatever you like, but it should be descriptive of the changes you’ve made.)

To verify that your commit was successful, you can run git status to see the current state of your repository. You should see something like:

$ git status
On branch main
Your branch is ahead of 'teacher/main' by 1 commit.

nothing to commit, working tree clean

If you get a different message or encounter an error, consult with your instructor.

More on Linux commands

Recall from lab 1 , the general format of a Linux command is:

command-name <options> <argument> ... <argument>

where the options are a set of switches that alter the behaviour of the command, and the arguments are the objects on which the command acts.

Recursive copying

In this course, you may find you need to recursively copy directories. For example, if you try to copy a directory with stuff in it, you will get the following message:

$ cp labs lab_copy
cp: -r not specified; omitting directory 'labs'

The error message gives us the solution!

$ cp -r labs lab_copy

Wildcards

Sometimes, when typing in arguments, you want a quick way of specifying multiple files or directories. If their names all match a common pattern, a wildcard can be used.

Common wildcards:

For example:

To demonstrate this try each of the following command, after each ls command carefully observe the output:

$ cd /usr/bin
$ ls --l l?
$ ls --l b*

Explain what the following command does:

$ ls --lt b*

Hint: compare the output of the two ls commands with the b* argument. Output that has scrolled off the window can be view using the side slider bar or the size of the window can be enlarged.

Control Key Combinations

Special functionality has been assigned to a few control key combinations. In all cases, the command is run by holding down the control key and typing a letter. In documentation, this notion of “holding down the control key” is written as ^ or CTRL. Here are a few combinations that you might find useful:

Let’s look at a couple of these key combinations in action. Try typing the following command

$ cat

… and hit <enter>. What do you observe? Why do you suppose Linux is not responding, even when you hit <enter> again? You will have to “abort” the cat command using one of the control key sequences listed above. Do that now.

At the end of today’s lab, try typing CTRL-d to log out from your INS session.

Did you CTRL-d right now? That’s okay, just log back in again.

Fancy Linux stuff (Optional)

Job control

In Windows or macOS, you don’t usually run one program at a time - it would be pretty annoying to have to shut down your browser every time you want to type in a Word document. You can do the same thing in your Linux shell: at any time during a session, one program is in the “foreground” and all others are in the “background”. The foreground program is the one that is currently interacting with the user.

CTRL-z is the command to suspend a program from the foreground to the background. Try this out by launching Emacs, adding some text to the buffer, and then hitting CTRL-z. You should see a message like:

[1]+  Stopped                 emacs

This says that Emacs is suspended in the background and is job #1. You can see a list of all background jobs by typing:

$ jobs

To bring a job to the foreground, type:

$ fg

And you’ll see your text in the buffer, even though you haven’t saved to a file yet.

Caution: if Emacs is suspended in the background, don’t launch a second copy to edit the same file! If you do this, you will end up with two buffers containing incompatible data. You can, however, edit multiple files in the same Emacs session (just like opening multiple browser tabs).

Linux will not let you log out if you have suspended jobs in the background. For example, you might see something like:

$ exit
logout
There are stopped jobs.
$

Resume these programs and quit them properly, and then log out.

Account Customization

Your account is your own, and you can (within reason) customize it how you like!

Any attempts to do weird hackery or sudo commands will be logged and reported to your instructor, and more importantly, to Jordan and Steve. Such behaviour is considered non-academic misconduct and will not be tolerated.

Now that that’s out of the way, the alias command can be used to redefine an existing command to save some typing. For example:

$ ls -al
(a long listing of all files in the current working directory)
$ alias ll='ls -al'
$ ll
(does the same thing)
$

Aliases defined in this way disappear when you log out. To make an alias persistent, add it to your .bashrc file (which is in your home directory). This file is processed each time you log in. You’ll see some aliases already defined, such as g++ - do not modify these!

How do you think you can open and edit the .bashrc file?

The format of your prompt is also set in the .bashrc file via the PS1 item. You can change this, however, it should be done in the lab where an IA can assist you if you make a mistake! Information about setting the prompt can be found by the man bash command and searching for PROMPTING.

Lab Assessment

To earn your lab mark for this lab, your instructor will verify that a file name first.txt exists in an intro directory of your labs repository. You have 1 week to complete this lab.



Previous: Lab 01: INS Linux Server
Next: Lab 03: C++ Basics