158 lines
7.3 KiB
HTML
158 lines
7.3 KiB
HTML
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
|
|
<html> <head>
|
|
<title>A Two-Cent Tip: The Date Command and Emacs</title>
|
|
</head>
|
|
|
|
<body bgcolor="#ffefd5" text="#181871">
|
|
|
|
<H4>
|
|
"Linux Gazette...<I>making Linux just a little more fun!</I>"
|
|
</H4>
|
|
|
|
<P> <HR> <P>
|
|
<!--===================================================================-->
|
|
<center><h1>Date and Its Switches</h1></center>
|
|
|
|
<center><h4><a href="mailto: layers@vax2.rainis.net">by Larry
|
|
Ayers</a></h4></center>
|
|
|
|
<P> <HR>
|
|
<!--===================================================================-->
|
|
<p>At first glance the humble GNU utility <em>date</em> seems to be a very
|
|
minor program, perhaps useful in shell-scripts but hardly something to get
|
|
excited about. Type "date" at the prompt, press <em>enter</em>, and
|
|
"Tue Feb 11 09:25:50 CST 1997" (or something similar) is displayed on your
|
|
screen. As with so many unix-ish utilities, the bare command is really
|
|
just a template, waiting to be laden with switches.
|
|
|
|
<p>I keep a journal, and I've been using a header line for each entry with
|
|
this format:<br><pre>
|
|
|
|
Tue 11 Feb 1997 *** Journal Entry #44 *** 9:30 PM
|
|
|
|
</pre>
|
|
|
|
<p>Weary of typing the header each day, some time ago I began attempts to
|
|
automate it. Creating an abbreviation or macro for the center field is not
|
|
hard with most editors, but I wanted the date and time as well. Reading the
|
|
man page for <em>date</em> I discovered that it has numerous formatting
|
|
switches. You can make the command print out the date and/or the time in
|
|
just about any fashion you can think of. The first field of the above
|
|
header can be created with these switches:<br>
|
|
|
|
<p><code>date '+%a %-d %b %Y'</code>
|
|
|
|
<p>while the time-of-day field uses these:<br>
|
|
|
|
<br> <code>date '+%-I:%M %p'</code>
|
|
|
|
<p>The single quotes are essential when combining several of the switches.
|
|
I tried for some time to get the command to do what I wanted without
|
|
success; while rereading the man- page I eventually noticed the quotes. Of
|
|
course no-one is going to memorize <b>date</b>'s numerous switches, which is
|
|
probably one reason the shell script was invented. I wrote two short
|
|
scripts; the first, called <b>mydate</b>, is just:<br><pre><code>
|
|
#!/bin/bash
|
|
date '+%a %-d %b %Y'
|
|
</code></pre>
|
|
|
|
<p>The second, called <b>mytime</b> is the similar but with the above time
|
|
switches for <b>date</b>.
|
|
|
|
<p>Typing the daily header in Emacs was now somewhat easier: first the command
|
|
<kbd>Control-u Esc-!</kbd>; when prompted in the mini-buffer I'd type
|
|
<b>mydate</b> and the formatted date would begin the line. Next a keyboard
|
|
macro for the center "Journal Entry" field, then a command like the first to
|
|
have the time inserted at the end of the line.
|
|
|
|
<p>After performing this little keyboard ritual for a few days, it occurred to
|
|
me that perhaps an Emacs macro could have a shell command embedded within it.
|
|
Reading a few <b>Info</b> files confirmed this supposition and suggested yet
|
|
another refinement. I learned that it's possible to cause a macro to pause
|
|
for input and then resume! This would be just ideal for the journal entry
|
|
number.
|
|
|
|
<p>The sequence which I came up with was: <kbd>Control-(</kbd> to start
|
|
recording the macro, then <kbd>Control-u Esc-!</kbd> followed (when
|
|
prompted) by <b>mydate</b>. At this point I typed in some spaces, then
|
|
<kbd>*** Journal Entry #</kbd>, followed by <kbd>Control-u Control-x q</kbd>
|
|
to start a recursive edit; this pauses the macro and allows the entry number
|
|
to be entered. Next is <kbd>Esc Control-c</kbd> which exits the recursive
|
|
edit and lets the macro proceed. The macro is completed with some more
|
|
spaces, then <kbd>control-u Esc-!</kbd>, the <b>mytime</b> shell-script
|
|
command, and ends with two <kbd>Enter</kbd> keystrokes and two spaces, to
|
|
indent the first sentence. <kbd>Control-)</kbd> stops the macro-recording.
|
|
Whew! That's a lot harder to describe than to type.
|
|
|
|
<p>This routine would be ridiculously esoteric if you had to remember
|
|
it. Luckily in Emacs you only have to do it one time. Once you've constructed
|
|
such a macro and tried it out to see if it does what you want, two more
|
|
steps will record it in your <kbd>~/.emacs</kbd> file so that it can be
|
|
executed with a simple keystroke.
|
|
|
|
<p>The first step is to give the macro a name, which can be
|
|
anything. <kbd>Esc-x name-last-kbd-macro</kbd>, followed by
|
|
<kbd>Return</kbd>, then the name and another <kbd>Return</kbd>, sets the
|
|
name. At this point load your <kbd>~/.emacs</kbd> file, move the cursor to
|
|
where you want the macro definition, then type <kbd>Esc-x
|
|
insert-kbd-macro</kbd>, followed by <kbd>Return</kbd>. There you go! As
|
|
long as you keep your <kbd>~/.emacs</kbd> file you'll have the macro
|
|
available. Now you can type <kbd>Esc-x [macroname]</kbd> and it'll
|
|
execute. If you've put a recursive edit in it, just remember to type
|
|
<kbd>Esc Control-c</kbd> after you've inserted the text you need and the macro
|
|
will conclude.
|
|
|
|
<p>This may seem like a convoluted procedure, and it is, the first time you do
|
|
it: haltingly typing in a macro, starting over from scratch after one
|
|
mis-typed character, all the while frequently referring to the docs. Then
|
|
repeating the process when it doesn't do what you wanted!
|
|
|
|
<p>The second time you will probably remember about half of the commands,
|
|
enough that it's no longer a tortuous task. Creating and saving macros
|
|
using these techniques isn't an everyday task; I've found that I have to
|
|
refresh my memory on at least part of the procedure every time I do it, but
|
|
for repetitive editing tasks the time spent is amply repaid.
|
|
|
|
<p>If you make very many of these you risk bloating your <i>~/.emacs</i>
|
|
file, causing the editor to load even more slowly and wasting memory.
|
|
Typically these macros have a specific use, so it makes sense to keep
|
|
them in categorised LISP files, one for each type of file you edit. Put
|
|
each file in the directory where it will be used, and load them on demand
|
|
with the command <kbd>Esc-x load-file [filename]</kbd>.
|
|
|
|
<p>So there is a reason the Emacs partisans like to call it an "extensible"
|
|
editor. These macros are just the tip of the iceberg; over the years many
|
|
LISP extensions to Emacs have been contributed to the free software
|
|
community by programmers world-wide. Luckily some of the best of them tend
|
|
to be incorporated into successive releases of Emacs and XEmacs; many others
|
|
are available from the
|
|
<a href="ftp://archive.cs.ohio-state.edu/pub/gnu/emacs/elisp-archive">
|
|
Emacs-Lisp Archive</a>.
|
|
Another good source for Emacs information is the
|
|
<a href="http://www.csc.uvic.ca/~jk/emacs.html">
|
|
Gnu Emacs and XEmacs Information and Links Site</a>.
|
|
|
|
<hr>
|
|
<address><a href="http://vax2.rainis.net/~layers/">Larry Ayers<layers@vax2.rainis.net></a></address>
|
|
<!-- hhmts start -->
|
|
Last modified: Thu Feb 27 18:39:47 CST 1997
|
|
<!-- hhmts end -->
|
|
|
|
<!--===================================================================-->
|
|
<P> <hr> <P>
|
|
<center><H5>Copyright © 1997, Larry Ayers <BR>
|
|
Published in Issue 15 of the Linux Gazette, March 1997</H5></center>
|
|
|
|
<!--===================================================================-->
|
|
<P> <hr> <P>
|
|
<A HREF="./index.html"><IMG ALIGN=BOTTOM SRC="../gx/indexnew.gif"
|
|
ALT="[ TABLE OF CONTENTS ]"></A>
|
|
<A HREF="../index.html"><IMG ALIGN=BOTTOM SRC="../gx/homenew.gif"
|
|
ALT="[ FRONT PAGE ]"></A>
|
|
<A HREF="./bbartlg.html"><IMG SRC="../gx/back2.gif"
|
|
ALT=" Back "></A>
|
|
<A HREF="./debian.html"><IMG SRC="../gx/fwd.gif" ALT=" Next "></A>
|
|
<P> <hr> <P>
|
|
</BODY>
|
|
</HTML>
|