rustom wrote:
1. In vi I can delete a line that contains a <pat> with
:g/<pat>/d
How is this done in emacs?
M-x query-replace-regexp
then enter "^.*<pat>.*$" as the regexp (no quotes), and leave the
replacement blank.
2. match-variables:
If I want to remove everything after the second : (in a file that has
3 fields separated with two :'s)
I do
:s/\(.*\):\(.*\):.*/\1 \2
How do I do that in emacs?
M-x query-replace-regexp
then "^[^:]*\(.*\):\(.*\):.*$" for the regexp, and "\1 \2" for the
replacement (again, no quotes).
The "^" and "$" both here and above are only there to prevent the regexp
matching across lines (unlike sed, regexp searches in an Emacs buffer
aren't line-oriented). Depending on your file, you might get away with
something simpler. E.g. for 2, if you know there will always be two
colons on every line, you could use "\(.*?\):\(.*?\):.*" instead ("?"
makes a wildcard non-greedy).
query-replace-regexp is interactive, like query-replace. I find this
useful just to check I've got the regexps right, and then hit "!" once
I'm happy with it to replace all the rest non-interactively. The
non-interactive version is replace-regexp.