How to replace multiple characters at once in emacs - emacs

What is the quickest way to replace multiple characters at once in emacs? For instance I would like to act on the region and replace 1 with a, 2 with b, ..., 9 with i.

You can stitch elisp into regex replacements (not sure if you can do the same with replace-string). For instance,
M-x query-replace-regexp \([1-9]\) \,(char-to-string (+ 96 \#1))
should replace single digits 1-9 with the letters a-i as appropriate.

Related

How to type unicode characters by name in spacemacs?

Is there some plugin that allows you to type unicode characters by name easily?
For example :
Writing \gamma and hitting TAB would replace the \gamma with γ.
Writing \Gamma and ... with Γ.
Writing \-> ... →.
Et cetera.
C-x 8 RET lets you do this anywhere in Emacs. I imagine it works for Spacemacs too, but someone will correct me if that is not quite correct.
It prompts you for the Unicode name with completion (ignoring case).
You can alternatively enter just the numeric code point for the character, instead of completing to its name.
[Caveat: Dunno whether Spacemacs plays nicely with Icicles - haven't tried it.]
If you use Icicles, and if option icicle-read-char-by-name-multi-completion-flag is non-nil, then C-x 8 RET is enhanced in a few ways:
It shows in *Completions*, for each candidate Unicode
character, its name and code point, as well as the character
itself.
When you cycle among the matching candidates, the name and code
point of the current candidate are shown in the mode line. The
code point is shown in hexadecimal, octal, and decimal
notations.
Completion candidates are in fact multi-completions, meaning
that you can match against the name or the code point, or both.
You can even match the character itself. Why might you want to
do that? To see the corresponding Unicode character name(s),
including any old names. For example, for the character `
(grave accent) you get these two completion candidates:
GRAVE ACCENT 60 `
SPACING GRAVE 60 `
The main purpose for this is to show you the characters and code
points together with their names (WYSIWYG). The characters are
shown in *Completions* using the same font as the frame from
which you invoked C-x 8 RET. That you can also match against
the characters themselves is just a nice-to-have.
The most important features of the Icicles version of C-x 8 RET
are (a) seeing the characters and code points (WYSIWYG) and (b)
being able to use progressive completion, so you can use multiple
simple patterns instead of a single complex pattern.
I don't use spacemacs, so I'm not sure if this works there.
I set default-input-method to "TeX". I can toggle to it with toggle-input-method (C-\). When it's enabled, there's a \ on the far left of the mode line.
This isn't the same as entering unicode chars by name, but probably closer to what you described.

How to read ranges in the syntax table of Emacs?

E.g. Below syntax table is from text-mode (invoked by C-h s).
The parent syntax table is:
C-# .. C-h . which means: punctuation
TAB .. C-j which means: whitespace
C-k . which means: punctuation
C-l .. RET which means: whitespace
C-n .. C-_ . which means: punctuation
By intuition I guess the .. means a list range. However I could not find confirmation in the GNU emacs manual. May I confirm about it here?
In addition, may I also confirm that the range is sorted by the ASCII code number? i.e. C-# .. C-h corresponds to ASCII code 0-10.
My apology if this looks too basic. I wish I could find related document from the reference manual but it is not
I can't definitively confirm this, but based on observation I agree 100% with your assessment.
The instances of A .. Z and a .. z and 0 .. 9 make for good supporting evidence.
Clearly the control characters are being rendered in a readable format, and sometimes the selected option isn't the ideal choice.
e.g. C-l .. RET would be clearer as the equivalent C-l .. C-m; and similarly if TAB .. C-j appeared as C-i .. C-j
The section on syntax table internals points out that they are implemented as char tables, which are "much like a vector [but] indexed by character codes". As such, syntax tables are based upon sequences of character codes, and the code which displays them undoubtedly iterates through the table, ascertaining the start and finish codes for every range of characters with an identical syntax, and then using the .. format to display each range.

how to replace extract word

all
I am new to emacs, I have a simple problem, when searching, emacs provides search by word, but when replace, how can I just replace the extract word, not a substring? I have searched on the internet, someone said to add \bfoo\b to match foo only, won't match foos but it doesn't work. Thanks.
For string-based search/replace, can run query-replace-word by typing: C-u M-%.
If you want regular expression search, then, indeed, you can enclose your regexp in either \b which matches word boundaries, or even \< and \> for beginning and end of word respectively. Make sure to use M-x replace-regexp in that case, not just M-x query-replace.

Combine two Emacs buffers?

Given two separate emacs buffers, how can I combine them by joining the first rows of each, then the second, etc? I'll probably need to add a space after each line in buffer #1 so I don't end up with "a b1 2", for example.
Buffer #1
a b
a c
c x
Buffer #2
1 2
5 4
3 2
Result
a b 1 2
a c 5 4
c x 3 2
You should be able to use rectangles to do this. Just copy the contents of buffer 2, and then rectangle-yank (C-x r y) them into buffer 1, with the point one space to the right of 'b' in the first line.
Use M-x 2C-associate and M-x 2C-merge. You might want to use C-h v 2C-windows-width or M-x 2C-shrink-window-horizontal.
I usually use the 'paste' tool for such applications (no Emacs though..). If you are using Linux it should be available by default.
It's as simple as:
$ paste file1 file2 > file3
which will merge the two files 'file1' and 'file2' into the output 'file3' the way you asked for. By default TABs are used as column separators, but this can be changed via the '-d' option.
If you don't mind doing the combining outside of emacs, you could save the two files, and combine them using awk.
Look at this example: http://www.linuxquestions.org/questions/linux-newbie-8/awk-question-331224/#post1682282

emacs/elisp: What is the hash (pound, number sign, octothorp) symbol used for?

What does this do?
(add-hook 'compilation-mode-hook #'my-setup-compile-mode)
...and is it different than
(add-hook 'compilation-mode-hook 'my-setup-compile-mode)
There is no difference:
(eq 'my-add #'my-add)
yields t
The # can be used in front of a lambda expression indicating to the byte-compiler that the following expression can be byte compiled, see the docs for Anonymous Functions. But there's nothing to compile in the case of a symbol.
In general, it is used in the printed representation along with the left angle bracket (<) to indicate that the object printed is a description (but cannot be read). For example:
#<buffer foo.txt>
It is also used in constructs by the reader to represent circular structures. See the docs for Read Syntax for Circular Objects.
And then you have its use for denoting the base for integers, e.g. #x2c -> 44.
Plus more I'm sure.
The should-be-comprehensive list can be found at the top of the Emacs lisp reference index.
Edit: Or even more conveniently, from within Emacs itself:
M-x info RET (open the info browser)
d m elisp RET (open the elisp manual)
I # RET (list the entries for # in the index)
I found this question while searching for what the hash meant in something I found while hacking mode-line-format:
#("-%-" 0 3
(help-echo "Display as tooltip when mouse hovers or with display-local-help."))
which is a format used for text properties in strings where:
"-%-", text to be propertized: one dash and a %-construct that results in "dashes sufficient to fill the remainder of the mode line", resulting in the famous Emacs ------.
0, the first character upon which the text properties apply.
3, the last character upon which the text properties apply, i.e. the entire "-%-".
(help-echo "..."), a property and a string as its argument.
This can be created with the propertize function:
(propertize "Hover over me!" 'help-echo '"congratulations!")
would be the same as #("Hover over me!" 0 14 (help-echo "Congratulations!")):
If you're using font lock mode, using the buffer-substring command might produce something like this:
(buffer-substring 1 28) ; First 27 characters in the current buffer
⇒ #(";; This buffer is for notes"
0 3
(fontified t face font-lock-comment-delimiter-face)
3 27
(fontified t face font-lock-comment-face))
So you could create something like: