lab 0: getting started with linux and emacs

EN47/COMP9, Fall 2009

Out: September 10, 3:00pm
Due: September 17, 3:00pm

overview

This lab introduces you to the Linux operating system and the Emacs text editor, which you will be using to create, edit and run your programs this semester. You can find the electronic version of this handout linked from the course website at http://www.cs.tufts.edu/comp/9.

welcome to linux

logging in

The computers should already be on. If the screen is blank, hit a key on the keyboard and wait a few seconds. If the screen is still blank try a different computer or notify the instructor. If the screen is locked, either move to another machine, or if there are no more computers free, notify the instructor.

Now type in your user name and press enter. In the next screen enter your new password. This is a security measure to help you keep others from learning your password. Never reveal your password to anyone!

Also, please do not turn your workstation off when you are done. Do log off from Linux before you leave; otherwise someone else could gain access to your account.

the linux shell

Right click on the desktop and select Terminal from the popup menu. In a moment, a window labeled 'Terminal' will appear. This window is running a Linux shell. The shell takes commands typed on the keyboard and executes them. You should see a prompt followed by a blinking cursor which looks something like this:

lab116s{username}:

This prompt indicates that Linux is ready to execute your next command.

creating your comp9 directory

Unlike Windows, in which the file hierarchy starts with a drive letter and a :\ (for example C:\), the Linux file system always starts with a /, which is called the root of the file system. When you login to your account, you should start in your home directory.

You should create a directory within your home directory (a subdirectory) to store all of your COMP9 files. To create ("make") this new directory, type this at the command prompt:

mkdir comp9

Next change your current working directory to comp9 by typing:

cd comp9

For more information on the mkdir and cd commands, see the brief summary of Linux and Emacs commands at the end of this document.

using emacs

GNU Emacs is a text editor you will use to create and edit your C++ programs. For the most part, you can use Emacs like any other document editor such as Microsoft Word or Notepad; the characters you type will be inserted into the file you are editing.

starting emacs

Type emacs& in the terminal window (don't forget the '&' sign). The Emacs window should appear in front of the terminal window. To bring a window back to the foreground you can click in the middle of its title bar. To move a window, you can click in the title bar and drag the window with the left mouse button. To open a file, click on the Open button in the top toolbar. At the bottom of the dialog box, you will see a prompt: Find file:. Just type in the name of the file you wish to edit followed by the Return key. For example, try typing:

~/comp9/hello.txt

The first part of the file name might appear already. Emacs knows it should look for a file in the current working directory. In general, you can use any filename you want. If the file does not exist, Emacs will create an empty file for you with that filename. In other words, to create a new file, just pick a filename that doesn't already exist. Now that you are in Emacs, you can type anything you wish. Enter a few lines to try it out.

saving a file and exiting

When you are finished editing a file, save it by clicking on the Save button in the top toolbar. At the bottom of the screen you will see a message Wrote /comp9/hello.txt. You can now close Emacs using the top menubar: File->Exit Emacs.

getting comfortable with linux and emacs

The best way to learn these systems is to use them. In this part of the lab, you will practice some common Linux commands and edit the lab report using Emacs.

Using Emacs, create a new file called lab0.txt. In this new file, type the following lines:

// File: lab0.txt
// Name: [put your name here] 
// Date: [put the date here]
// Overview of Linux commands

Save this file. Then bring the terminal window to the foreground and type the following:

ls

You should see your file lab0.txt. If not, ask the TA for help.

navigating the filesystem

For each of the following steps, you will need to switch back and forth between the terminal window and the Emacs window. Enter commands at the Linux prompt and record your findings in the file lab0.txt. For each question, you should record both the command you used and the output you see on the screen.

  1. What is your current working directory?
  2. What are the contents of your current directory?
  3. What are the contents of your home directory?
  4. What are two ways of displaying the contents of the file hello.txt?
  5. Make a new directory called greetings. Make a copy of the hello.txt and move both files into greetings. What sequence of commands did you use?
  6. What are two ways (two sequences of commands) to list the files in the greetings directory?
  7. What happens when you try to delete the greetings directory and its contents? How did you complete this task?
  8. What does cal do?
  9. How do you use the -y option to cal?

handing in

When you are done with the assignment, submit it to be graded by doing the following. First make sure your current working directory is the one containing lab0.txt. Then, submit the file by entering the following command:

provide comp9 lab0 lab0.txt

This semester, we will use the provide program for all homework submissions.

the desktop environment

You've had a chance to practice navigating the Linux filesystem by typing at the command prompt. Many Linux machines (including the ones in 116) also have a desktop environment, a graphical user interface that runs on top of the operating system. The windows, icons, and menus might look familar to you, as this interface is similar in many ways to that of Windows and Mac.

On your desktop, you should see an icon for your home directory. Clicking on it brings up a graphical file browser. This is another way to navigate the Linux filesystem. You should be able to see the files and directory structure that you created earlier using commands in the terminal window. With the time left in the class, feel free to continue exploring the programs and utilities installed on the Linux machines.

brief summary of some linux and emacs commands

Below is a very brief summary of Linux and Emacs commands.

some linux commands

  • pwd : Prints the current working directory or where you are in the file system.
  • ls : List the files and subdirectories in the current directory.
  • cd dirname : Changes the current working directory.
  • cd .. : Changes the working directory to the parent directory. This is also called moving up one directory. Remember to include a space between the cd and the two dots.
  • cd ~ : Changes the working directory to your home directory.
  • mkdir dirname : Creates a new directory called dirname in the current directory.
  • more filename : Displays the contents of a file.
  • rm filename : Deletes a file. Warning: there is no recycle bin or trash can in Linux. When you delete a file it's gone for good.
  • rmdir dirname : Deletes a directory. rmdir only works for directories that are empty (that contain no files or subdirectories).
  • cp filename new-filename : Copies a file to a new location.
  • mv filename new-filename : Moves a file to a new location.
  • emacs filename : Call the Emacs editor on the file named filename.
  • man utilityname : Displays the "help" file for a particular utility. The view the next page of the file, hit the spacebar. The stop viewing the file, type 'q'.

some emacs commands

Many Emacs commands use special character combinations with modifier keys. For example, Control-X, usually written C-x, is typed by holding down the Control key and typing 'x'. This is similar to using the Shift key to capitalize letters.

Another modifier is the Meta key, which used to look like a black diamond next to the Alt key on Unix keyboards. On the lab machines, the Alt key can be used instead for Emacs commands. For example, M-x is typed by holding down the Alt key and typing the 'x' key. The Esc key can also be used instead of the Alt or Meta key: to type M-x, first press the Esc key, then press the 'x' key (do not hold the Esc key).

Many of the commands in Emacs require two or more keystrokes. C-x tells Emacs that you are about to press another control sequence, and causes Emacs to wait for you to do so. If you change your mind, and don't want to finish a command, you can type C-g. C-g makes Emacs abort the current command. Occasionally, Emacs will get stuck. Pressing C-g often solves the problem. You may have to press C-g several times to clear mistyped instructions.

  • C-x C-f : Find file
  • C-x C-s : Save File
  • C-x C-c : Quit Emacs(with save)
  • C-g : Abort the current command.
  • The arrow keys can be used to move around.
  • TAB : When editing C++ files, indents the current line appropriately.
  • C-j : The same as typing RETURN followed by TAB.
  • M-g : Prompts you for a line numbers and takes you to that line.
  • C-k : Cuts all characters from the cursor to the end of the line.
  • C-y : Pastes characters from the clipboard into the document.