Overview to Text-Editing Programing In Emacs

Xah Lee, 200510-2006

In emacs, a user can program it using the embedded language (called Emacs Lisp, or elisp) so that he can have custome functions to insert texts, templates, process files, and many other features of emacs. This page gives a overview of how this environment works. If you don't know elisp, first take a look at Emacs Lisp Basics.

Emacs provides you with a text-edit programing framework. More specifically, a set of functions for text editing and manipulation.

For example, there is a lisp function that returns the cursor position in a buffer. A function that returns the beginning (or ending) position of the current selected text (aka active region). Functions that move the cursor to a given position, or delete a block of text of given beginning and ending positions. Functions that insert a string at a given position. Functions that open or save files. Functions that list opened files. Functions that list buffer names. Function that returns the current mode of a given buffer. Functions that activate modes. Functions that make a section of text into a particular color or font, ... and so on.

So, programing emacs to manipulate text, is a matter of calling these functions provided in Emacs.

Example of Simple Elisp Functions

Here are some examples of simple elisp functions.

Cursor Position

; current cursor position is called “point”.
; The first char in buffer is 1
; This returns the current cursor position
(point)

; returns the position of the beginning/end of region
(region-beginning)
(region-end)

; returns the position for the end of buffer
; (taking account of narrow-to-region)
(point-max) ; (there's also point-min)

Moving Cursor and Searching

; move cursor to position 392
(goto-char 392)

; move cursor by n chars
(forward-char n)
(backward-char n)

; move cursor to the first char that's not a newline or tab
; Returns the distance traveled
(skip-chars-forward "\n\t")
(skip-chars-backward "\n\t")

; move cursor to the location of myStr
; returns the new position
(search-forward myStr)
(search-backward myStr)

; move cursor to the location matched by a regex
; returns the new position
(re-search-forward myRegex)
(re-search-backward myRegex)

Text Editing

; delete 9 chars starting at current cursor pos
(delete-char 9)

; deleting text
(delete-region myStartPos myEndPos)

; insert string at current cursor position
(insert "hi i ♥ u.")

; get the string from buffer
(setq myStr (buffer-substring myStartPos myEndPos))

; change case
(capitalize-region myStartPos myEndPos)

Strings

; length
(length "abc") ; returns 3

; gets a substring
(substring myStr startIndex endIndex)

; change a given string using regex
(replace-regexp-in-string myRegex myReplacement myStr)

Buffers

; return the name of current buffer
(buffer-name)

; return the full path of current file
(buffer-file-name)

; switch to the buffer named myBufferName
(set-buffer myBufferName)

; save current buffer
(save-buffer)

; close a buffer
(kill-buffer myBuffName)

Files

; open a file (in a buffer)
(find-file myPath)

; same as “Save As”.
; close current buffer and open the new saved
(write-file myPath)

; insert file into current position
(insert-file-contents myPath)

; append a text block to file
(append-to-file myStartPos myEndPos myPath)

; renaming file
(rename-file fileName newName)

; copying file
(copy-file oldName newName)

; deleting file
(delete-file fileName)

; get dir path
(file-name-directory myFullPath)

; get filename part
(file-name-nondirectory myFullPath)

; get filename's suffix
(file-name-extension myFileName)

; get filename sans suffix
(file-name-sans-extension myFileName)

A Simple Example

This code illustrates how to insert a string, then position cursor somewhere inside.

(defun insert-p ()
  "Insert <p></p> at cursor point."
  (interactive)
  (insert "<p></p>")
  (backward-char 4))

Type the above in any file, then select the whole code, type “Alt+x eval-region”. To execute the command, type “Alt+x insert-p”.

For many simple elisp examples that illustrate how text manipulation programing is done, see Elisp Examples.

For the official elisp documentation, See GNU Emacs Lisp Reference Manual. Chapters 1 to 19 are related to elisp the language. You may not need to read them until much later. The interesting chapters, are chapters 30 to 32, which directly tells you what elisp functions are available for manipulating text. Here's some links to the most interesting, immediately useful chapters:

Note that each of these chatpers are quite long. You do not need to read them all. A most practical apporach in elisp programing is first to get a simple overview of the language (See Emacs Lisp Basics), so that, you know how to code conditionals, printing, list, iteration, or how to define your own functions. Then, go to Elisp Examples to see actual, simple, useful examples. Perhaps modify some of them for your own use. Then, scan the above 3 chatpers, and or read other chapters when you need to.


Page created: 2005-10.
© 2005 by Xah Lee.
Xah Signet