Chapter 4. Searching and Modifying Buffers
There will be lots of times when you want to search through a buffer for a string, perhaps replacing it with something else. In this chapter we'll show a lot of powerful ways to do this. We'll cover the functions that perform searches and also show you how to form regular expressions, which add great flexibility to the kinds of searches you can do.
Inserting the Current Time
It is sometimes useful to insert the current date or time into a file as you edit it. For instance, right now, as I'm writing this, it's 10:30pm on Friday, 18 August, 1996. A few days ago, I was editing a file of Emacs Lisp code and I changed a comment that read
;; Each element of ENTRIES has the form ;; (NAME (VALUE-HIGH . VALUE-LOW))
to
;; Each element of ENTRIES has the form ;; (NAME (VALUE-HIGH . VALUE-LOW)) ;; [14 Aug 96] I changed this so NAME can now be a symbol, ;; a string, or a list of the form (NAME . PREFIX) [bg]
I placed a timestamp in the comment because it could be useful when editing that code in the future to look back and see when this change was made.
A command that merely inserts the current time is simple, once you know that the function current-time-string yields today's date and time as a string.[18]
(defun insert-current-time () "Insert the current time" (interactive "*") (insert (current-time-string)))
The section More Asterisk Magic later in this chapter explains the meaning of (interactive ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access