Working with text regions in Emacs

Emacs is clearly one of the greatest free text editors around; it can facilitate text writing, simplify everyday processes, and enhance our productivity. Much of the editor’s functionality is rightly available, yet sometimes we have to add our own code. In the following, I provide some functions that allow text insertion at the beginning (prefix), at the end (suffix) and around (circumfix) a text region.

The following text comes from Friedrich von Schiller’s “The Poetry of Life”:

Who would himself with shadows entertain,
Or gild his life with lights that shine in vain,
Or nurse false hopes that do but cheat the true?–
Though with my dream my heaven should be resigned–
Though the free-pinioned soul that once could dwell
In the large empire of the possible,
This workday life with iron chains may bind,
Yet thus the mastery o’er ourselves we find,
And solemn duty to our acts decreed,
Meets us thus tutored in the hour of need,
With a more sober and submissive mind!
How front necessity–yet bid thy youth
Shun the mild rule of life’s calm sovereign, truth.

(You can read the poem here).

Prefix
To add text at the beginning of this text region, you can select the region, and press C-x M-p. The command line will prompt you to type the prefix. For example,
(1)
“C-x M-p Schiller wrote”:

Alternatively, you can M-x string-insert-rectangle <RET> string <RET>

Schiller wrote: Who would himself with shadows entertain,
Schiller wrote: Or gild his life with lights that shine in vain,
Schiller wrote: Or nurse false hopes that do but cheat the true?–
Schiller wrote: Though with my dream my heaven should be resigned–
Schiller wrote: Though the free-pinioned soul that once could dwell
Schiller wrote: In the large empire of the possible,
Schiller wrote: This workday life with iron chains may bind,
Schiller wrote: Yet thus the mastery o’er ourselves we find,
Schiller wrote: And solemn duty to our acts decreed,
Schiller wrote: Meets us thus tutored in the hour of need,
Schiller wrote: With a more sober and submissive mind!
Schiller wrote: How front necessity–yet bid thy youth
Schiller wrote: Shun the mild rule of life’s calm sovereign, truth.

Suffix
To add text at the end of the text region you have to select the region and press C-x M-s. The command line will prompt you to type the suffix. For example,
(2)
“C-x M-p wrote Schiller””.
Who would himself with shadows entertain,: is a verse written by Schiller.
Or gild his life with lights that shine in vain,: is a verse written by Schiller.
Or nurse false hopes that do but cheat the true?–: is a verse written by Schiller.
Though with my dream my heaven should be resigned–: is a verse written by Schiller.
Though the free-pinioned soul that once could dwell: is a verse written by Schiller.
In the large empire of the possible,: is a verse written by Schiller.
This workday life with iron chains may bind,: is a verse written by Schiller.
Yet thus the mastery o’er ourselves we find,: is a verse written by Schiller.
And solemn duty to our acts decreed,: is a verse written by Schiller.
Meets us thus tutored in the hour of need,: is a verse written by Schiller.
With a more sober and submissive mind!: is a verse written by Schiller.
How front necessity–yet bid thy youth: is a verse written by Schiller.
Shun the mild rule of life’s calm sovereign, truth.: is a verse written by Schiller.

Circumfix
To add text before and after a text region, you have to select the region and press C-x M-c. The command line will prompt you to type the circufix. For example,
(3)
“C-x M-c    %%%   ”
%%% Who would himself with shadows entertain, %%%
%%% Or gild his life with lights that shine in vain, %%%
%%% Or nurse false hopes that do but cheat the true?– %%%
%%% Though with my dream my heaven should be resigned– %%%
%%% Though the free-pinioned soul that once could dwell %%%
%%% In the large empire of the possible, %%%
%%% This workday life with iron chains may bind, %%%
%%% Yet thus the mastery o’er ourselves we find, %%%
%%% And solemn duty to our acts decreed, %%%
%%% Meets us thus tutored in the hour of need, %%%
%%% With a more sober and submissive mind! %%%
%%% How front necessity–yet bid thy youth %%%
%%% Shun the mild rule of life’s calm sovereign, truth. %%%
To achieve this functionality you can add the following code in your .emacs file.

(4)

(defun add-suffix (str b e)
 “prompt user to provide a suffix:”
 (interactive "sSuffix: \nr") 
 (goto-char e)
 (forward-line -1)
 (while (> (point) b)
 (end-of-line)
 (insert str)
 (forward-line -1))
(end-of-line)
 (insert str)
)
(global-set-key (kbd "C-x M-s") 'add-suffix)
;; Add Prefix
(defun add-prefix (str b e)
 “prompt user to provide a prefix“
 (interactive "sPrefix: \nr")
 (goto-char e)
 (forward-line -1)
 (while (> (point) b)
 (beginning-of-line)
 (insert str)
 (forward-line -1))
(beginning-of-line)
 (insert str)
 (forward-line -1))
(global-set-key (kbd "C-x M-p") 'add-prefix)
;; Add Circumfix
(defun add-circumfix (str b e)
 “prompt user to provide a circumflex“
 (interactive "sEnter Circumfix: \nr")
 (goto-char e)
 (forward-line -1)
 (while (> (point) b)
 (end-of-line)
 (insert str)
 (beginning-of-line)
 (insert str)
 (forward-line -1))
 (end-of-line)
 (insert str)
(beginning-of-line)
 (insert str)
 (forward-line -1))
(global-set-key (kbd "C-x M-c") 'add-circumfix)
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s