The Complete Magazine on Open Source

Master the Calendar, Centering, Macros and Drawing Tools in GNU Emacs

, / 150 0

GNU Calendar

Here’s another great article in the GNU Emacs series, which tutors readers on using the inbuilt calendar and then leads them through the text and drawing tools.

In this article in our ongoing series on GNU Emacs, you will learn how to use its calendar, center text, macros and drawing tools.

You can use and display a calendar inside GNU Emacs using the following command (see Figure 1):

M-x calendar

Figure 1: Calendar

You can move forward by a day using the ‘C-f’ shortcut, and move back a day using the ‘C-b’ keys. You can move to the current date using the ‘.’ key.
To start the week on a Monday, set the following command in your ~/.emacs.

(setq calendar-week-start-day 1)
Figure 2

Figure 2: Diagram using Picture mode

If you wish to move forward by a week, you can use the ‘C-n’ shortcut, and to move back by a week, use the ‘C-p’ shortcut. The ‘C-a’ shortcut can be used to move to the beginning of the week, while the ‘C-e’ shortcut can be used to move to the end of the week.

Figure 3

Figure 3: Artist mode operations

You can move to the beginning of a month using the ‘M-a’ shortcut. To move to the end of the month, use ‘M-e’.
You can move forward and backward a month using the ‘M-}’ and ‘M-{’ shortcuts, respectively.
If you wish to scroll forward three months, use the ‘C-v’ shortcut. To scroll backward three months, use the ‘M-v’ shortcut.

In order to move forward a year, you can use the ‘C-x]’ shortcut, and to move back a year, you can use the ‘C-x [’ shortcut.
You can go to a specified date using the ‘g d’ key combination. It will then prompt you with the messages: “Year (>0):”, “Month name:” and “Day (1-31):”, and will take you to the specified date.
You can move to the beginning of the year using the ‘M-<’ shortcut, and to the end of the year using the ‘M-v’ shortcut.
You are encouraged to read the ‘Calendar’ section in the GNU Emacs manual at, to learn more.

Figure 4

Figure 4: Ellipse

Figure 5

Figure 5: Polylines

Let’s assume you’ve written a poem where both the verses and the title appear left-margin aligned. So you want to centre the title and the text. You can do so by first placing the cursor on the title/text, and typing ‘M-x set-justification-center’.
Marking and highlighting the poem, and using ‘M-x center-region’ will center the poem.

Macros are recorded key strokes that can be stored and replayed. You can start defining a keyboard macro using the ‘C-x (’ command or the F3 key. You can then type a series of keys that constitute the macro. To finish defining the macro, you can use ‘C-x )’ or the F4 key. In order to execute the previously defined macro, you can use the ‘C-x e’ shortcut or F4.
Consider the following text in a buffer that contains a serial number, date and an examination subject list:


Suppose you wish to add a space after each comma, you can define the following macro (exclude the semi-colon followed by the text) for the first line using the following key strokes:

F3 ; Start macro definition
C-s ; Search for
, ; comma
Enter ;
Space ;
C-s ; Search again for
, ; comma
Enter ;
Space ;
C-n ; Move to next line
C-a ; Move to beginning of line
F4 ; End macro definition

Using ‘C-x e’ or F4 repeatedly will turn the above input CSV text into the following:
1, 2015-03-02, English
2, 2015-03-03, Physics
3, 2015-03-05, Mathematics
4, 2015-03-08, Biology
5, 2015-03-10, Chemistry
You can give a name (say,‘comma’) to the previously defined macro using ‘C-x C-k n’. You can then execute the macro using ‘M-x comma’. You can also insert the named macro into a file using the ‘M-x insert-kbd-macro’ command. You can bind a macro to a key using the ‘C-x C-k b’ shortcut.
If you wish to apply the macro to each line in a region, you can use ‘C-x C-k r’ keys. In order to cycle between the previous and next macros in the macro ring, you can use the ‘C-x C-k C-p’ and ‘C-x C-k C-n’ shortcuts respectively. You can also delete a macro using the ‘C-x C-k C-d’ key combination.

Figure 6

Figure 6: Circle fill

Picture mode
You can draw diagrams inside Emacs using Picture mode. To start Picture mode, use the ‘M-x picture-mode’ command, and to exit, use the ‘C-c C-c’ shortcut.
The cursor movement keys in a buffer are also applicable in Picture mode. To move the cursor right, you can use the ‘C-f’ keys, and to move left by one character, you can use the ‘C-b’ shortcut. To move the cursor up and down by one character, use the ‘C-p’ and ‘C-n’ shortcuts, respectively. The ‘C-d’ shortcut is used to delete a character.
Before you move the cursor to draw in the buffer, you need to set the drawing direction. Table 1 summarises the shortcut keys and their associated drawing direction.

Table 1
If you want to move the cursor forward in the drawing direction, you can use the ‘C-c C-f’ shortcut. To move the cursor backward, use the ‘C-c C-b’ key combination. If you want to delete a line, use the ‘C-k’ command. You can insert a new line using the ‘C-o’ shortcut. You can also draw a rectangle around a region using the ‘C-c C-r’ shortcut. A drawing done using Picture mode is shown in Figure 2.

Figure 7

Figure 7: Spray

Artist mode
Artist mode can also be used to draw diagrams in GNU Emacs. You can enter this mode using ‘M-x artist-mode’ and exit the same using ‘C-c C-c’.
You can draw pictures using the keyboard alone or also use the mouse in Artist mode. In order to start and stop drawing, use the Enter key. This is equivalent to putting the pen down when drawing, and lifting it up when you want to perform a different action.
The buffer navigation commands to move right and left are the same as the ‘C-f’ and ‘C-b’ shortcuts respectively. You can move up a column using the ‘C-p’ shortcut, and move down a column using the ‘C-n’ key.
You can draw geometric shapes using Artist mode. To select a shape or operation you can use the ‘C-c C-a C-o’ key combination. This will provide a list of shapes and actions you can perform. This list is shown in Figure 3.
The shortcuts listed in table 2 are available for drawing specific shapes.
Table 2

Figure 4 depicts an ellipse drawn using Artist mode.
Figure 5 is an example of polylines.
You can fill a shape using the ‘C-c C-a f’ key combination. Figure 6 shows a circular representation filled with dots.
You can also spray characters in the buffer using the ‘C-c C-a S’ shortcut keys. An example is shown in Figure 7.
The character to be used for drawing can be changed using the ‘C-c C-a C-l’ shortcut. The character to fill shapes can be set using the ‘C-c C-a C-f’ key combination.
If you want to cut an area, you can draw a rectangle around it using the ‘C-c C-a C-k’ key combination. You can also copy the image area using the ‘C-c C-a M-w’ keys, and paste the same using the ‘C-c C-a C-y or C-x r y’ shortcuts. To set the operation to erase text, you can use the ‘C-c C-a C-d’ key combination.
You can refer to the Emacs Wiki Artist mode for more documentation and help –