Viewing
Highlight all lines matching a regex
Highlight all lines wider than 79 columns:
m-x highlight-lines-matching-regexp
.\{80\}
Replace
Remove empty lines
-
M-%: Query replace string (
query-replace
). -
C-q: Quote - enter original string now.
-
C-j, C-j: Two continuous
^J
characters. (Note an empty line is two continuous newline characters.) -
C-q: Quote —— enter replacement string now.
-
C-j:
^J
character. (Replace empty line with one newline character.)
Replace lines with string substituted
To replace lines like
echo "[INFO]example: 'development'"
with
read -p "examples: 'development':" -e INPUT
do
-
press key combination M-x and type in the command
query-replace-regexp
, or use the key binding for that: C-M-% -
type in regex pattern
^echo \"\[INFO\]example: \(.*\)\"
, and press Enter -
type in replacement pattern
read -p "examples: \1" -e INPUT
, and press Enter- Special characters in Emacs Lisp regex are
.
,*
,+
,?
,[
,^
,$
, and\
.]
is special if it matches an earlier[
and ends a character alternative.-
is special inside a character alternative.[:
and balancing:]
enclosing a character class are special. Any other character appearing in a regular expression is ordinary, unless a\
precedes it. - There are back slash constructs.
\"
: I'm actually not sure why"
needs escaping here.\[
and\]
: needs escaping since[
and]
are special characters but it is the literal characters that needs matching.\(
and\)
: needs escaping since(
and)
are non-special characters in Emacs Lisp regular expression, and needs escaping to use its special function as a back slash construct.\1
refers to the first matched group associated with\(.*\)
.
- Special characters in Emacs Lisp regex are
Here is a list of examples for this type of replacing:
(examples to be added)
Replace multiple continuous whitespaces inbetween words with one whitespace
Replace regexp `\b[:space:]+\b with ' ' (don't include the two ').
Comment out lines of form {"OriginalString", "Unit"}
-> ".*"
in Mathematica code
Query replace regexp \({"OriginalString", "Unit"} -> ".*"\)$ with: (* \1 *)
Resolving version control repository merge conflicts
When resolving merge conflicts for files in version control repositories, oftentimes, one need to keep the top or bottom halves for all the conflict blocks of form
<<<<<<< XXX top code block line 1 top code block line 2
top code block line 3
bottom code block line 1 bottom code block line 2 bottom code block line 3
YYY
Check and change file encoding
Check the coding system of the file opened in the current buffer:
describe-coding-system
or with keybinding
C-h C
Save the buffer in file using a different encoding:
C-x C-m f
or force it in situ in the buffer immediately
C-x C-m c
utf-8
Ent C-x
C-w Ent
Revert a buffer from file using a different coding system:
revert-buffer-with-coding-system
or with keybinding
C-x Ent r
-
References
http://emacswiki.org/emacs/ChangingEncodings
http://www.gnu.org/software/emacs/manual/html_node/emacs/Specify-Coding.html
Editing
Edit a remote file
Press keys C-x C-f to bring cursor into the Emacs mini-buffer, type
/ssh:username@hostname.com:/path/to/file.txt
and Ent.
Customization
M-x customize-group
Ent whitespace
Ent to enter into
a buffer to customize whitespace
mode options.
Modes that I use often
mediawiki-mode
- C-F5: open new page.
- A-g: refresh buffer with remote content, discarding local changes.
erc mode to chat in IRC
M-x erc-select
...
/join #haskell
Note
-
C- is Ctrl- in most cases.
-
M- is Alt- in most cases.
-
In Emacs
^J
is newline character.
See also
- Key bindings of Emacs
References
- 34.3.1.3 Backslash Constructs in Regular Expressions, GNU Emacs manual
- 19.9.4 Query Replace, GNU Emacs manual * [http://elpa.gnu.org/packages/ GNU Emacs Lisp Package Archive (ELPA) packages]. This *is standard Emacs' built-in repository of FSF sanctioned Emacs packages.