Set up and get started Wiki
Line 152: Line 152:
   
 
A solution to this can be to use C-_ instead of C-x u, since C-x u more often fails, or by creating a macro which undoes one step, and then repeating the macro n times using M-n C-x e.
 
A solution to this can be to use C-_ instead of C-x u, since C-x u more often fails, or by creating a macro which undoes one step, and then repeating the macro n times using M-n C-x e.
  +
  +
===Compiling===
  +
  +
When compiling a project from Emacs, the best thing is to learn how to use a [[makefile]], and write one.
  +
  +
When compiling, enter a buffer that contains a file which is in the same directory as the makefile (maybe the makefile it self’s buffer), and type M-x compile. A standard shell line will now be displayed in the minibuffer ("make -k"), which can be modified, and when return is pressed the content of the minibuffer is run as a shell command.
  +
  +
To use [[make (software)|make]], the program [[MSYS]] must be installed and set up properly, otherwise [[MinGW]] contains a program called mingw32-make.exe which works as make.exe. On the other hand, MSYS contains many programs which are common [[Linux]] shell commands, so it is good to have [[MSYS]] installed too. Then make.exe will call the compiler in question, for which you need to have [[MinGW]] installed and set up properly. You will need to have both MinGW's and MSYS' bin folders added to your [[system path]].

Revision as of 19:15, 24 September 2009

GNU Emacs start up window

The window that is displayed after GNU Emacs has started

Setting up GNU Emacs in Windows

Installation

1. Go to http://ftp.gnu.org/pub/gnu/emacs/windows/ and download the latest zipped binary version of GNU Emacs (currently emacs-23.1-bin-i386.zip).

2. Extract the folder inside of the archive to a directory with a path name without spaces.

3. In the Emacs folder, the program that you want to run is bin\renemacs.exe. Create a shortcut to it on the desktop or in the start menu if you feel like it.

Setting up the variables

The variables that stores the settings you wish to use are stored in the elisp source file called .emacs (located in your home directory). You can either change them manually directly in the source file or you can edit them through GNU Emacs by clicking (in the menu) Options->Customize Emacs->Top-level Customization Group. If you choose to change them manually you may have to restart Emacs after modyfying the file for the changes to take effect.

Indentation

When programming in C/C++ you may want to adjust the width which the indentation has, or indent with tabs instead of spaces. The variables you want to change then are

  • c-basic-offset: sets the width of the indentation when writing C/C++ files (I have it set to 4)
  • indent-tabs-mode: tells Emacs if it can replace multiple spaces in the indentation with a tab, should be set this to t (for true) or nil (for false)
  • tab-width: the width of tab in number of spaces (I have this set to 4, and indent-tabs-mode set to t, so one indentation is equal to one tab).

This can be changed by browsing for the appropriate options in Options->Customize Emacs->Top-level Customization Group, or by setting the variables in your .emacs file directly

Other options

There are also some options you can change directly in the Options menu: You may want to activate Active Region Highlighting if you are not used to that the active region is not highlighted. If you are irritated by the blinking cursor there is an option for this too. If you want the scroll bar on the left side of the window you can change this by clicking Options->Show/Hide->Scroll-bar->On the Left.

Getting started with GNU Emacs

Keyboard commands

There are a number of Emacs commands that it is good to know about. There are a number of good pages on the web listing many useful Emacs commands, and here are some of them:

Otherwise, the most common of the commands are listed in the table bellow (here, C = Control, and M = Meta (alt or escape in windows)):

Command What it will do
Files
C-x C-f Find file (open/create file in, this will create a new buffer)
C-x C-s Save file
Editing
M-w Copy
C-w Cut
C-y Yank (paste)
C-x u OR C-_ Undo (this undo will not go back in history, but rather repeat it)
C-space Start active region (start selection, if you want to remove selected text you have to cut it (C-w), or select it with the mouse and delete it)
C-x h Select the entire text
C-s Search/search forward
C-r Search backward
tab Indent current line
M-C-\ Suposed to indent every row in the active region (like M-x indent-region) but I can't make it work.
Buffers
C-x right/left arrow Switch between different buffers
C-x C-b Show buffer list
C-x k Kill buffer
Window
C-w 0 Close window (if more than one is open)
C-x 1 Close all windows but the one the cursor is in
C-x 2 Split the window vertically
C-x 3 Split the window horizontally
C-x 5 2 Create new frame
Commands
M-x Go to the minibuffer (write command)
C-g Quit running/entered command (cancel)
Macros
C-x ( Start recording macro
C-x ) Finish recording macro
C-x e Run last recorded macro
M-n C-x e Run macro n times (useful for repeating something many times)
Lisp
M-: Evaluate single Lisp expression written in the minibuffer and print the result in the echo area
C-x C-e Evaluate the Lisp expression before where the cursor is and print the result in the echo area

Note that the keyboard commands differ notably from most other text editors; copy, paste and cut don't have the same commands, text can't be selected by holding down shift and moving the cursor and selected text isn't replaced if new text is entered or if text from the clip board is pasted. These are the differences that are the most notable, but one will get used to them after a while.

Minibuffer commands

There are also a number of useful minibuffer commands, which you enter in the minibuffer by first pressing M-x. Minibuffer commands are used in more special occasions, hence the longer names.

Command What it will do
Files
recover-session Recover files being edited before crash
Editing
indent-region Indent every row in the active region
comment-box Make a box around the region which is commented (looks good when writing C files)
Compiling
compile Display a standard line for you to edit in the minibuffer, which is then run as a shell command
Lisp
eval-region Evaluate all Lisp expressions in the active region
eval-current-buffer Evaluate all Lisp expressions in the buffer

A list of commands that can be entered can also be shown by pressing tab when entering a command in the minibuffer.

Undoing

When undoing, what it does is that it will repeat (copy and flip) the latest sequence in the line of history, rather than stepping back into it. In most text editors, what happens when undoing is that it will step back in the line of history, and if one happens to enter something else into the text, everything that lies forward in the history line will be cut off so that the history line can continue to grow from there. I.e. it will forget about everything that happened after where one currently is in history.

In Emacs on the other hand, what happens when one happens to enter something into the text is that everything in the line of history from where one currently is, to the end of the history line, will be copied and flipped, and then attached to the end of the history line. Hence nothing is cut off; the history line is instead extended. Then the location in history is moved all the way to the end of the history line. This means that the number of steps that has to be undone to get to the desired location in history will be even higher than it was from the beginning when the undoing started.

A solution to this can be to use C-_ instead of C-x u, since C-x u more often fails, or by creating a macro which undoes one step, and then repeating the macro n times using M-n C-x e.

Compiling

When compiling a project from Emacs, the best thing is to learn how to use a makefile, and write one.

When compiling, enter a buffer that contains a file which is in the same directory as the makefile (maybe the makefile it self’s buffer), and type M-x compile. A standard shell line will now be displayed in the minibuffer ("make -k"), which can be modified, and when return is pressed the content of the minibuffer is run as a shell command.

To use make, the program MSYS must be installed and set up properly, otherwise MinGW contains a program called mingw32-make.exe which works as make.exe. On the other hand, MSYS contains many programs which are common Linux shell commands, so it is good to have MSYS installed too. Then make.exe will call the compiler in question, for which you need to have MinGW installed and set up properly. You will need to have both MinGW's and MSYS' bin folders added to your system path.