Viewing

Highlighting all lines matching a regex

Highlight all lines wider than 79 columns:

m-x highlight-lines-matching-regexp

.\{80\}

Replacing

Removing 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.)

Replacing lines with strings 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 \(.*\).

Here is a list of examples for this type of replacing:

(examples to be added)

Replacing Markdown image element with an equivalent HTML image element

!\[\(.*\)\](\(.*\))

<img src="\2" data-source="\2" alt="\1" style="max-width:400px" />

style="max-width:400px" limits the image to a reasonable maximum width.

Replacing 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

Checking and changing 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

Editing 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.

Spellchecking

  • Install aspell as an external spellchecking program to be called by Emacs via ispell and flyspell.

  • In Emacs, install ispell-aspell

  • Add local personal dictionary files, e.g. ~/.aspell.en.pws. Optionally, create them as symbolic links to files synchronized via cloud storage system for portability and maintainability.

  • Move the cursor to the word that you'd like to spellcheck.

  • Press M-$ to spellcheck the word the cursor is at.

    • ?: Open documentation.
    • i: Insert the word into the dictionary.
    • u: Insert the uncapitalized version of the word into the dictionary.
  • References:

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

External links

blog comments powered by Disqus