Prefacing lines in emacs...
So, I wanted a way to insert the string PROMPT in front of every line in a region of a text file. (This is for an SQL script for Ray's DB Mgmt. class.).
Here's how to do it in emacs:
-
Highlight the region that you want to preface each line of (hit
Ctrl-spacebarand then arrow down or up to highlight). -
Hit
M-x replace-regexp(on my mac, that's actuallyCtrl-[ x replace-regexp). -
Enter the following regular expression (regexp, for short):
^(.*)$
(the
^is the marker for the beginning of the line, the\(and\)are escaped parentheses, the.*is regexp-speak for "any character, many or no times" and the$is the marker for the end of the line.) -
Hit return.
-
Now, what to replace it with? I chose
PROMPT \1
(
PROMPTis what I wanted to preface each line with and\1says to take the stuff found between the parentheses in the regexp and put it there.) -
Hit return.
For more on emacs replace-regexp type M-x describe-function replace-regexp.