1. Home
  2. Tutorials
  3. Linux Text Editors
  4. emacs
Yolinux.com Tutorial

Using the Xemacs editor and programming C++

This tutorial is intended primarily for C/C++ software developers who use Xemacs.

Basic "xemacs" operation:

Command line options:

  • -rv: Reverse video

Partial list:

File commands:
Keystrokes Menu Selections Description
ctrl-x ctrl-c "File" + "Exit emacs" quit
ctrl-x ctrl-f "File" + "Open" read/load a file
ctrl-x ctrl-s "File" + "Save" save

Cursor movement:

Keystrokes Single Key Description
esc-v Page Up Scroll UP one page
ctrl-v Page Down Scroll DOWN one page
ctrl-p Up arrow Scroll up one line
ctrl-n Down arrow Scroll down one line
ctrl-f Right arrow Scroll forward one character
ctrl-b Left arrow Scroll back one character
ctrl-a Home beginning of line
ctrl-a End end of line
ctrl-a Delete delete char

You can multiply the effect of the cursor movement: ctrl-u 5 ctrl-n will move the cursor down 5 lines

Notes:

  • X-Emacs oftern uses hyperlinks. Use middle mouse button to select hyperlinks in a page, info page of help etc. Hyperlinks will be displayed with a green background when the mouse passes over the link.
  • X-Emacs shows newline as a space. If you delete this character it unwraps the line
  • To "reset" use the keystrokes ctrl-x ctrl-g. This will get you out of the current mode. (cancel)
  • Interupt/quit: ctrl-g
  • The literature on emacs/xemacs often refer to the Esc key as the "Meta" key.

Window control:
Keystrokes GUI selection Description
ctrl-x 2 Right mouse button, select "Split Window" Split window
ctrl-x 1 Right mouse button, select "Unsplit Window" Un-Split window which is to remain.

Delete:
Keystrokes Description
ctrl-d Delete char
esc-d Delete word after cursor
esc-DeleteKey Delete word before cursor
ctrl-k Delete from cursor to end of line

Operate on selected text:
Keystrokes Description
ctrl-w Kill or delete selected section.
ctrl-x ctrl-l Convert to lower case.
ctrl-x ctrl-u Convert to upper case.
esc-x Print region.
ctrl-x <tab> Indent selected region
esc-ctrl-\ Beautify selected region

Buffers:
Keystrokes GUI selections Description
ctrl-y - Paste buffer contents at cursor.
ctrl-x ctrl-b "Buffers" + "List all buffers" List buffers
ctrl-x ctrl-s - Save buffer
ctrl-s "Edit" + "Search" Search
ctrl-_ Undo icon on toolbar Undo

Mouse:
Mouse Button Function
left Set point or make a text selection..
middle Paste text.
right Pop up a menu of options.
SHIFT-left Extend a selection.
ctrl-left Make a selection and insert it at point.
ctrl-middle Set point and move selected text there.
ctrl-SHIFT-left Make a selection, delete it, and insert it at point.
esc-left Make a rectangular selection.

Mark a position:
Keystrokes Description
ctrl-<space-bar> Set a mark on line defined by current cursor position
ctrl-x ctrl-x Select with mouse to mark

Info:

More Notes:

  • Valid colors: /usr/lib/X11/rgb.txt
  • esc-x then type auto-fill-mode + enter. This feature is like a word processor and the carraige return is automatically inserted near the end of a line. The margin is set at 70. Change with ctl-x f.
  • Esc Esc Esc gets you out of recursive editing.
  • vi emulatuion mode (Viper): "Apps" + "Toggle VI emulation"
  • Spell check: Use mouse to highlight region to check then select spell icon

Tagging:

Keystrokes Description
esc-x
visit-tags-table
Visit a TAGS file.
esc-. Find a definition for a tag. The default tag is identifier under the cursor. Name completion type partial name and then TAB.
ctrl-x 4 . TAG Display tag in new window.
esc-, Find the next definition for the tag.
esc-* Pop tag stack (go back one level)

Note: Tags do not work in Viper mode.

The etags utility:

There are more than one version of etags out there. The original Unix version, the GNU version and the version that comes with vim. This discussion is about the GNU version. (default with Red Hat)

For use with C++:

   etags --c++ *.cpp *.h

Option Description
-d Creates entries for C preprocessor definitions.
-D No defines included.
-t Record typedefs in C code as tags.
-u Update tag entries for files specified leaving tag entries for other files in place.

Defaults file:

$HOME/.emacs

Example:
(setq c-set-style "ellemtel")
(setq c-default-style "ellemtel")

(custom-set-variables
 '(font-lock-auto-fontify t)
 '(font-lock-maximum-decoration t)
 '(delete-key-deletes-forward t)
 '(c-ignore-auto-fill (quote (string c c++ cpp code)))
 '(indent-tabs-mode nil)
 '(font-lock-mode t nil (font-lock)))
(custom-set-faces)

(setq indent-tabs-mode nil)
(setq c-tab-always-indent nil)

Beautify C/C++ code: (re-indent)

This file sets up my X-Emacs session for C/C++ editing style as defined by "Ellemtel". Other options include gnu, k&r, bsd, whitesmith, stroustrup, linux, java, ...
Set style: ctrl-c . <style name> + Enter
or use the defaults file above to define a default style.

To apply the coding style to a whole file:

  • On first line in file ctrl-spacebar to mark first line
  • Go to end of file: ctrl-EndKey
  • esc ctrl-\ Beautify the file. Indent region defined by selection mark and cursor.

One can also apply the style to a selection or section scoped by braces:
esc ctrl-q Auto Indent/beautify an entire balanced brace or parenthesis expression.

For more on C++ coding style and standards see the YoLinux C++ coding style tutorial

Set indent value:
  • esc-x set-variable
  • c-basic-offset
  • <value>

Set delete keys:

Delete to the right instead of default backspace behaviour:
(global-set-key [delete] 'delete-char)
(global-set-key [kp-delete] 'delete-char)
This assigns behavior to both the delete key and the key pad delete key.

Links:

Books:

"GNU Emacs and XEmacs"
by Larry Ayers
ISBN # 0761524460, Prima Publishing

Amazon.com
"Learning GNU Emacs"
by Debra Cameron, Bill Rosenblatt, Eric S. Raymond
ISBN # 1565921526, O'Reilly & Associates

Amazon.com
"Writing GNU Emacs Extensions"
by Bob Glickstein
ISBN # 1565922611, O'Reilly

Amazon.com

   
Bookmark and Share

Advertisements