Basic Emacs Usage

From Interactive System for Ice sheet Simulation
Jump to: navigation, search

GNU Emacs is a very versatile text editor which we recommend you use when editing code. It gives you much better support than, for example, gedit. Emacs may look a little intimidating at first, but it's really easy to use--honest!; these notes will help you get started.

Contents

Launching Emacs

Working from the command-line, it's easy to start emacs:

emacs &

You can also give the name of a file. This can be an existing file, or a file you want to create:

emacs myfile.F90 &

You can also tart emacs from a file browser window: right-click on the file you want to open, and select Open with -> Emacs 22 (client).

Fortran 90 mode

If you open a file whose name ends with .f90, Emacs will enter Fortran 90 mode. This is really handy: it highlights the code syntax, gives you intelligent indentation, and allows autocompletion of loops and other program blocks.

For example, try entering a simple program like this:

program test
 
  implicit none
 
  integer :: a,b,c
 
  c = 4
 
  do a = 1, 10
     b = a + b + c
  end do
 
end program test

When you enter the program, all you need to do is press TAB at the start of each line to get indentation, and press TAB after each end to auto-complete that program block.

Entering Fortran 90 mode

If you edit a program file with a different extension (such as .F90), you can select the Fortran 90 mode manually by using the short cut:

M-x f90-mode

where M-x stands for the Meta-x keystroke, which you can get by typing escape and then x. Then hit enter. Bingo. You'll get all those nice language specific features.

Other Keystokes, if you would like them..

Emacs has a bunch if buttons and menus at the top which will allow you to do all your usual task with the mouse--loading and saving files, cut & paste etc. You can also move the cursor with the mouse, so you basically have all that you need. Happy editing!

Emacs also allows you to use a number (actaully a great many) keystrokes as shortcuts to all the bells and whistles. Here's a table of some good ones:


Useful Emacs shortcuts
keystrokes function
Ctrl-x Ctrl-f open a file (new or existing)
Ctrl-x Ctrl-s save a file
Ctrl-g cancel partially typed or accidental command
Ctrl-a edit: go to start of current line
Ctrl-e edit: go to end of current line
Ctrl-k edit: cut to the end of the current line
Ctrl-y edit: paste (yank) from the clipboard
Ctrl-s search for string (you will be prompted)