Basic Emacs Tutorial

Introduction

This is a guide on how to perform some basic tasks using emacs along with some neat and helpful ones that I think will make for a much better editing experience than someone will get with basic tools such as nano.

Notes on Formatting and Syntax

Commands with a dollar sign ($) in front of them are meant to be run from the command line as a normal user, while commands with an octothorp (#) in front are meant to be run as root. All others are meant to be run from within emacs.

Emacs command syntax is as follows:

Guide

Install emacs

# yum install emacs-nox

This will install the Non-X version of emacs, as in, it has to be run from within a terminal.

Note that this is for Fedora. Installing in other distributions should replace yum install with the appropriate command, such as apt-get install emacs for Debian and Ubuntu or zypper install emacs for SUSE.

Open a file from the command line with emacs

$ emacs test.cpp
File Edit Options Buffers Tools C++ Help                                        

#include <iostream>
using namespace std;

void example(int num) {
  cout << "Answer: " << (num + num) << endl;
  return;
}

int main() {
  int num = 5;

  example(num);
  return 0;
}







----:**-F1  test.cpp       All (2,0)      (C++/l Abbrev)------------------------

Close emacs

C-x C-c

A look at the Information Bar

----:**-F1  test.cpp       All (2,0)      (C++/l Abbrev)------------------------
    

The ** means that the file has been edited and is unsaved. When saved, they will change to two hyphens (--).

test.cpp is the file name.

(2,0) is the row number and column number of the cursor position.

(C++/l Abbrev) is the mode that emacs is in for the currently open file. For this example, it's telling us that emacs is in the C++ mode, therefore it will be applying any known formatting and syntax highlighting rules.

The blank second row of the information bar is the minibuffer. When you perform an action in emacs, information about it or your current focus can appear here. For example, if when you open a file from within emacs (as you will see below), the phrase Find file: ~/ will appear here and when you start to type a file name, it will appear here instead of in the file buffer.

Save a file

C-x C-s
File Edit Options Buffers Tools C++ Help                                        
#include <iostream>
using namespace std;

void example(int num) {
  cout << "Answer: " << (num + num) << endl;
  return;
}

int main() {
  int num = 5;

  example(num);
  return 0;
}







-uu-:---F1  test.cpp       All (1,0)      (C++/l Abbrev)------------------------
Wrote /home/lewisrj/test.cpp

Notice the Wrote statement in the minibuffer.

Open a file from within emacs

C-x C-f
File Edit Options Buffers Tools Minibuf Help                                    
#include <iostream>
using namespace std;

void example(int num) {
  cout << "Answer: " << (num + num) << endl;
  return;
}

int main() {
  int num = 5;

  example(num);
  return 0;
}







----:---F1  test.cpp       All (2,0)      (C++/l Abbrev)------------------------
Find file: ~/ 

Note that the focus of your cursor is now in the minibuffer, thus allowing you to type in the location of a file to open. Tab completion works here.

Cancel an action

C-g

This will cancel whatever action you've started. If, for example, you typed C-x C-f to open a file but then decide that you don't want to, you can use C-g to abort and continue editing your current file.

Copy and Paste

Since you can't use your mouse in emacs, selecting a region of text that you want to copy involves using what are called "marks". The idea is that you set a mark at the beginning of where you want to copy, move your cursor to the end of where you want to copy, and then perform the copy action. It will put the region that you've slected into a copy buffer, and then you can paste it as many times as you want.

Set a mark

C-spacebar

You should see Mark set in the minibuffer.

Copy

M-w

Paste

C-y

You can use the command over and over to keep pasting.

Undo

C-_

You should see Undo! in the minibuffer.

Multiple Buffers

When you open a single file in emacs, you've actually opened a single buffer and it's taking up are the screen area. This is can be annoying if you want to edit multiple files. To solve this problem, emacs can open multiple buffers by splitting the screen.

Each buffer is like it's own instance of emacs. This means that whichever buffer has focus, all actions performed only affect it (except actions such as copy and paste, which can be moved between buffers). This is how you can have multiple files open.

Vertical split

C-x 3
File Edit Options Buffers Tools C++ Help                                        
#include <iostream>                    |#include <iostream>
using namespace std;                   |using namespace std;
                                       |
void example(int num) {                |void example(int num) {
  cout << "Answer: " << (num + num) <<$|  cout << "Answer: " << (num + num) << $
  return;                              |  return;
}                                      |}
                                       |
int main() {                           |int main() {
  int num = 5;                         |  int num = 5;
                                       |
  example(num);                        |  example(num);
  return 0;                            |  return 0;
}                                      |}
                                       |
                                       |
                                       |
                                       |
                                       |
                                       |
                                       |
-uu-:---F1  test.cpp       All (1,0)   |-uu-:---F1  test.cpp       All (1,0)    

Horizontal split

C-x 2
File Edit Options Buffers Tools C++ Help                                        
#include <iostream>
using namespace std;

void example(int num) {
  cout << "Answer: " << (num + num) << endl;
  return;
}

int main() {
  int num = 5;
-uu-:---F1  test.cpp       Top (1,0)      (C++/l Abbrev)------------------------
#include <iostream>
using namespace std;

void example(int num) {
  cout << "Answer: " << (num + num) << endl;
  return;
}

int main() {
  int num = 5;
-uu-:---F1  test.cpp       Top (1,0)      (C++/l Abbrev)------------------------

Switch focus

Since there are now two buffers, there has to be a way to switch your cursor's focus between them.

C-o

More Reading

Created by Ryan Lewis