Key binding for § symbol in Emacs - emacs

I'm working in Ubuntu, but since the standard way of inserting unicode characters (Ctrl+Shift+U and, after that, the unicode code) doesn't work inside emacs, I've, in my .emacs, some keystrokes for different unicode symbols which I use frequently, for example:
(global-set-key (kbd "C-c b") "☛")
and every symbol works fine, except the symbol §, which is replaced by a simple dash ("-") when I use the corresponding keystroke:
(global-set-key (kbd "C-c y") "§")
The question is, what does it make this symbol different for other symbols and how can I solve my problem?

global-set-key usually expects a function, so this should work:
(global-set-key (kbd "C-c y") (lambda () (interactive) (insert "§")))
But you're better off using the excellent insert-char function:
(global-set-key (kbd "<f2> u") 'insert-char)
It understands hex Unicode as well as text description (with completion and all).
Just press TAB to see the completions.

You can insert unicode chars in emacs by doing C-x8RET<unicode-hex>RET. So to insert § do C-x8RET00A7RET.
You can bind insert-char (the command run by C-x8RET) to a simpler key if you wish, or define a custom function and bind it to a key as follows
(global-set-key (kbd "C-c y") (lambda () (interactive) (insert "§")))

A general solution to the need to insert a number of Unicode chars quickly is to define a command for each, dedicated to inserting it, and bind that command to a simple key sequence.
That is, in effect, what #abo-abo and #Iqbal have offered you, in the form of:
(lambda () (interactive) (insert "§")))
If you have multiple such chars that you want to create such commands for, then
library ucs-cmds.el can help -- in two ways:
It provides a macro, ucsc-make-commands, that lets you, in one fell swoop, define commands for whole ranges of Unicode chars. For example:
(ucsc-make-commands "^cjk") defines an insert-char command for each Chinese, Japanese, and Korean Unicode character.
(ucsc-make-commands "^greek [a-z]+ letter") does the same for each Greek letter.
(ucsc-make-commands "arabic") does the same for each Arabic character.
It provides a replacement command (ucsc-insert) for C-x 8 RET, which acts exactly the same as the vanilla command (insert-char), except that if you give it a negative prefix argument then it not only inserts the char you choose but it also creates an insertion command for it (i.e., the kind of command that the macro creates in bulk).
The names of the commands created by macro ucsc-make-commands and command ucsc-insert are exactly the same as the Unicode names of the chars themselves, except that they are lowercase and have hyphens (-) in place of space chars.

Related

How to Zoom in /out in emacs -nw (command line only) whithout having a numeric keypad (or mouse wheel)? [duplicate]

This s-expression in my .emacs file does not produce the desired result:
(define-key global-map (kbd "C-=") 'djhaskin987-untab-to-tab-stop)
Why can't I bind a command to Ctrl+=?
EDIT for clarification:
I am using emacs23-nox on the standard build of urxvt-256colors for Debian, except that I have recompiled with --disable-iso405776 (or something to that effect) it so that Ctrl+Shift doesn't do the weird 'insert character' thing. I don't know if this affects anything. For example, C-M-i sends M-TAB, which I don't understand.
EDIT II:
I apologize for not making this clear. The function djhaskin987-untab-to-tab-stop has the line (interactive) in it. This part works.
The accepted answer in combination with the link in the first comment to it is enough to get started on a complete solution. The steps are:
make your terminal output escape codes for the key
make Emacs recognise the escape codes as a standard keypress
bind the keypress in a mode map
The first is very terminal and/or operating system dependent.
The link in the first comment shows some examples for X Window System. The key names are available in /usr/X11R6/include/X11/keysymdef.h (or try locate keysymdef.h), prefixed with XK_ (which should be removed for our purposes). I read that symbolic names are preferred over key literals.
I don't currently run X but I think it should look like this in your case:
XTerm.VT100.Translations: #override \
Ctrl ~Meta ~Shift <Key> equal: string(0x1b) string("[emacs-C-=")\n
The first string is the escape, the second is of your choosing.
In iTerm you can use Preferences->Keys and choose Send Escape Sequence as the Action. For example, I have:
Emacs Wiki lists some configuration methods for other terminals.
Now you can teach Emacs to recognize it as a C-=. First define-key into input-decode-map. I have a couple of helper functions:
(defun my/global-map-and-set-key (key command &optional prefix suffix)
"`my/map-key' KEY then `global-set-key' KEY with COMMAND.
PREFIX or SUFFIX can wrap the key when passing to `global-set-key'."
(my/map-key key)
(global-set-key (kbd (concat prefix key suffix)) command))
(defun my/map-key (key)
"Map KEY from escape sequence \"\e[emacs-KEY\."
(define-key function-key-map (concat "\e[emacs-" key) (kbd key)))
So then:
(my/global-map-and-set-key "C-=" 'some-function-to-bind-to)
Some keys (currently: ()\|;'`"#.,) will need escaping in the string, like C-\..
In a terminal, TAB is represented by the same byte sequence as C-i. And typically the terminal has no special byte-sequence for C-=, so it will just send a =. There is nothing that Emacs can do about it. But you might be able to teach your terminal emulator to send some special byte sequence of your choice (check the documentation of your terminal emulator for that), after which you can teach Emacs to recognize it as a C-= (with something like (define-key input-decode-map "...thebytes..." [?\C-=])).
The problem is that you use emacs in the terminal.
The terminal does not allow "C-=".
Try your function in the graphical emacs and it will work.
You will have to find another keybinding for the terminal.
You can map C-= using the default ascii codes: ^[[61;5u. Then you can bind it in Emacs either using:
(global-set-key (kbd "C-=") 'djhaskin987-untab-to-tab-stop))
or let use-package do it, e.g.:
(use-package expand-region
:ensure t
:bind (("C-=" . er/expand-region)))
I do want to thank Sam Brightman, for his wonderful solution. It's a very clean, albeit heavy-handed, approach that will work for any keys that cannot be sent via normal ascii codes. I've been wanting to get C-TAB working inside iterm2 for a long time. I was able to do it by deleting the builtin preferences keys for C-TAB/C-S-TAB and using his approach. With the following, I can be ssh'd into remote Linux boxes and quickly switch through lots of open buffers in projects, just like a desktop editor.
(use-package nswbuff
:defer 1
:after (projectile)
:commands (nswbuff-switch-to-previous-buffer
nswbuff-switch-to-next-buffer)
:config
(progn
(my/global-map-and-set-key "C-TAB" 'nswbuff-switch-to-previous-buffer)
(my/global-map-and-set-key "C-S-TAB" 'nswbuff-switch-to-next-buffer))
:init
(setq nswbuff-display-intermediate-buffers t
nswbuff-exclude-buffer-regexps '("^ "
"^\*.*\*"
"\*Treemacs.*\*"
"^magit.*:.+")
nswbuff-include-buffer-regexps '("^*Org Src")
nswbuff-start-with-current-centered t
nswbuff-buffer-list-function '(lambda ()
(interactive)
(if (projectile-project-p)
(nswbuff-projectile-buffer-list)
(buffer-list)))))
The function you're binding must be interactive. Try:
(define-key global-map (kbd "C-=")
(lambda () (interactive) (djhaskin987-untab-to-tab-stop)))

how can a person add abbreviated names for unicode character names in Emacs

Using emacs-24.
Some unicode names are quite long. Some characters have more than one name depending on the context. I would like to add some abbreviations/synonyms. How?
This approach is not so bad, but I have problems with shorter names that alias with longer ones, and it is non-standard, i.e. not consistent with the way other names are entered:
(global-set-key (kbd "C-x g all") "∀")
The approach of putting characters on keys has problems in Emacs, partly because the keymap is already overloaded:
(define-key key-translation-map (kbd "C-~") (kbd "¬"))
As a secondary question, I am curious as to why this confuses emacs (give it a try):
(global-set-key (kbd "C-x g neg") "¬")
What I would like is to hook the abbreviations into the current emacs method for entering unicode characters by name. (I've been using C-x 8 RET name RET - though wish there was a method to do this in fewer key strokes.)
You can easily define a command that inserts a given character (or that chooses from some small set of characters rather than from the entire universe of Unicode characters).
Library ucs-cmds.el can help with this. When you use C-x 8 RET with a negative prefix arg (e.g. C--), it not only inserts the char you choose but it creates a command to insert the char - the command name is the same as the char name. And you can quickly create such commands for whole ranges or other sets of characters (e.g. by matching a regexp). You can of course rename commands to whatever you like, including shorter versions.
But you already know how to bind a key to a keyboard macro that inserts a given character, as you have shown. If it helps to provide a named command for that then ucs-cmds.el can help.
You can also just do that yourself individually, using, for example:
(defun neg (&optional n)
"Insert \"¬\". With prefix arg N, insert N times."
(interactive "p")
(dotimes (ii n) (insert "¬")))
(global-set-key (kbd "C-x g neg") 'neg)
But you apparently are not very interested in dedicated commands that insert particular characters, and you want to be able to use C-x 8 RET but to type an abbreviation for a character name when it prompts you, instead of trying to match the real character name.
For that, Icicles can help. When you use C-x 8 RET you can match the character name or its code point (or the character itself - useful when the char is easy to type and you want to know its name or code point). You can match any combination of these at the same time.
Matching can be substring, regexp, pcompletion or any of several kinds of fuzzy matching, and you can change the matching behavior on the fly. So you can get the effect of the abbreviations you are asking for, provided you abbreviate in a way that corresponds to matching.
As for your question about (global-set-key (kbd "C-x g neg") "¬"): I think it is a bug. Consider reporting it: M-x report-emacs-bug. This is the error that it raises:
After 0 kbd macro iterations: user-error: No M-x tags-search or M-x tags-query-replace in progress
There are several modes around which provide simplified input for symbols needed by math and logic. For example agda2-mode. http://wiki.portal.chalmers.se/agda
OP:
What I would like is to hook the abbreviations into the current emacs method for entering unicode characters by name. (I've been using C-x 8 RET name RET - though wish there was a method to do this in fewer key strokes.)
What the OP is asking for is:
a) To use the emacs function 'insert-char' with its built-in shortcut 'C-x 8 RET', and
b) To use an alias for completion in the interactive minibuffer for 'insert-char' input.
The issue is that the minibuffer for 'insert-char' has its own TAB completion. If you want to insert the greek small letter epsilon (ε) using TAB completion, you have to input a minimum number of keystrokes like this: "greek" TAB "sm" TAB "l" TAB "ep". Even if you have an alias for epsilon in your 'init.el' configuration file like this: '("eps" "GREEK SMALL LETTER EPSILON")', the minibuffer will not automatically recognize it.
You can still use the alias for epsilon you have in your 'init.el' file using a second function 'expand-abbrev'. Using the method described in the OP, you can get an 'ε' by using "C-x 8 RET" (or "M-x insert-char"), entering your alias "eps", then call 'expand-abbrev' ("M-x expand abbrev") and return. This will expand your alias for the 'insert-char' function. (There is also a 'C-x' shortcut for 'M-x expand-abbrev'.)
Like the OP, I prefer this method over (or in addition to) automatic alias replacement. If you have something in your config file like this:
;; a quick way to insert unicode characters by code point or name
(global-set-key [f8] 'insert-char)
;; call 'expand-abbrev', especially in the 'insert-char' input minibuffer
(global-set-key [f9] 'expand-abbrev)
;; abbreviate unicode names
(define-abbrev-table 'global-abbrev-table '(
("ueps" "GREEK SMALL LETTER EPSILON")
("Ueps" "ε")
("ugsl" "GREEK SMALL LETTER ")
("uforall" "FOR ALL")
("Uforall" "∀")
))
;; see .emacs.d/abbrev_defs
;; M-x edit-abbrevs
;; turn on abbrev mode by default
(setq-default abbrev-mode t)
, you have two ways to type an epsilon. You can let emacs replace the alias "Ueps" automatically, or you can use seven keystrokes "[f8]ueps[f9]RET". (Actually there are four ways here.)
As the OP suggests, it is somewhat impractical to have aliases (or key-bindings) for every single special character. That is why it makes sense to use 'insert-char' with 'expand-abbrev'. If you want to insert a less commonly used greek letter like omicron 'ο', for instance, you do not need a special alias; you can expand an alias like 'ugsl' to "GREEK SMALL LETTER ", and enter "omicron" (or "omi" + TAB).

How to select text between quotes, brackets... in Emacs?

In vim, you can do this by vi", vi[, vi( ...
For example, if you have a line like this:
x = "difference between vim and emacs"
and the cursor is anywhere between those quotes and you hit vi", then the string will be visually
selected.
The package expand-region is convenient for this. Calling er/expand-region with the point inside the quotes will mark the nearest word, and then calling it again will mark all the words inside the quotes. (Calling it a third time will expand the region to include the quotes.)
I have it bound to C-;.
(global-set-key (kbd "C-;") 'er/expand-region)
With this binding, pressng C-; C-; will highlight the text between the quotes.
From Emacs Documentation
Nearly all modes support “(,)” as parentheses, and most also support
square brackets “[,]” and curly brackets “{,}”. However, you can make
any pair of characters a parenthesis-pair, by using the following
command:
(modify-syntax-entry ?^ "($")
(modify-syntax-entry ?$ ")^")
Also, take a look at this post How to mark the text between the parentheses in Emacs?. key combination given per this post
Try the key sequence C-M-u C-M-SPC (i.e., while holding the Control and Meta keys, press u and Space in sequence), which executes the commands backward-up-sexp and mark-sexp
Late to the party, but you can also use Evil mode, which does a bang-up job of Vim emulation, including the motion commands you mentioned.
(defun select-text-in-delimiters ()
"Select text between the nearest left and right delimiters."
(interactive)
(let (start end)
(skip-chars-backward "^<>([{\"'")
(setq start (point))
(skip-chars-forward "^<>)]}\"'")
(setq end (point))
(set-mark start)))
On top of the toolkit
https://launchpad.net/s-x-emacs-werkstatt/+download
the following keys/commands are delivered:
(global-set-key [(super \))] 'ar-parentized-atpt)
(global-set-key [(super \])] 'ar-bracketed-atpt)
(global-set-key [(super \})] 'ar-braced-atpt)
(global-set-key [(super \")] 'ar-doublequoted-atpt)
(global-set-key [(super \')] 'ar-singlequoted-atpt)
That way with a couple of more chars known as delimiters will constitute commands.
ar-delimited-atpt will return the string around point found by nearest delimiter.
A group of more powerful commands allows re-using keys like that
(global-set-key [(control c)(\")] 'ar-doublequote-or-copy-atpt)
(global-set-key [(control c)(\')] 'ar-singlequote-or-copy-atpt)
(global-set-key [(control c)(<)] 'ar-lesser-angle-or-copy-atpt)
(global-set-key [(control c)(>)] 'ar-greater-angle-or-copy-atpt)
Here a doctring given as example:
ar-doublequote-or-copy-atpt is an interactive Lisp function in
`thing-at-point-utils.el'.
It is bound to C-c ".
(ar-doublequote-or-copy-atpt &optional NO-DELIMITERS)
If region is highlighted, provide THING at point with doublequote(s),
otherwise copy doublequote(ed) at point.
With C-u, copy doublequote(ed) without delimiters.
With negative argument kill doublequote(ed) at point.
Use libraries thingatpt+.el and thing-cmds.el.
You will find there commands such as thing-region, which selects a thing at point as the region. Since string-contents and list-contents are defined as things (in thingatpt+.el), these select the string/list contents as the region:
(thing-region "string-contents") ; Select the contents of the string at point.
(thing-region "list-contents") ; Select the contents of the list at point.
Or interactively:
M-x thing-region RET string-contents RET
Other, related commands include:
C-M-U (aka C-M-S-u) -- mark-enclosing-list: Select enclosing list. Repeat to expand list levels. Works with any balanced-parenthesis expressions (e.g., vectors): whatever the current syntax table defines as having balanced-delimiter syntax.
mark-thing -- Select successive things of a given kind (repeating).
next-visible-thing -- Move to the next visible thing of a given kind (repeating).

How do I save the character under the point in emacs

I have a list of special unicode characters that I use frequently in one of my files.
To avoid typing (and learning) unicode numbers all the time I would like to just have a line with those characters at the top of my file (it's only 25 symbols) and save/yank them when I need them.
I cannot find the proper shortcut to save the character under the point though...
It's no different to copying anything else. Move point to the character you wish to copy, set the mark with C-SPC, move forward one character so that the region covers the character of interest, and save to the kill ring with M-w.
Or you could do something like this:
(defun my-copy-character-as-kill (pos)
"Copy the character at point (or POS) to the kill ring."
(interactive "d")
(if (eobp)
(error "End of buffer.")
(copy-region-as-kill pos (1+ pos))
(when (called-interactively-p 'interactive)
(let ((print-escape-newlines t))
(message "%S" (char-to-string (char-after pos)))))))
(global-set-key (kbd "C-c c") 'my-copy-character-as-kill)
Here is another way to go, especially if don't use a lot of such characters and don't want to fiddle with an input method.
Download library ucs-cmds.el and put it in your load-path (byte-compile it). Then put this in your init file (~/.emacs):
(require 'ucs-cmds)
(define-key global-map [remap insert-char] 'ucsc-insert)
Then use M-- C-x 8 RET and use completion to enter the Unicode name or code point of the character you want. That does two things: C-x 8 RET inserts the character you chose before the cursor. The M-- makes it also create a command with the same name as the character. You can then bind that command to a handy key sequence. For example:
M-- C-x 8 RET greek small letter lambda RET
That defines command greek-small-letter-lambda, which you can bind to some key sequence.
If you want to define such commands for several Unicode characters at once, you can instead just use macro ucsc-make-commands to do so. See the Commentary in file ucs-cmds.el. You provide a regexp to the macro. It is matched against all Unicode character names. An insertion command is created for each of the characters whose name matches.
Sample command creations:
(ucsc-make-commands "^math") ; Math symbols
(ucsc-make-commands "latin") ; Latin alphabet characters
(ucsc-make-commands "arabic")
(ucsc-make-commands "^cjk") ; Chinese, Japanese, Korean characters
(ucsc-make-commands "^box drawings ")
(ucsc-make-commands "^greek [a-z]+ letter") ; Greek characters
(ucsc-make-commands "\\(^hangul\\|^circled hangul\\|^parenthesized hangul\\)")

Arbitrary characters for forward-word/backward-word in Emacs

How to add an arbitrary character (hyphen, for example) to forward/backward-word functionality, so Emacs would handle words like "k-vector" as a single word. The same question about double-click on the word.
Syntax tables define which character is considered a word constituent. You can read about it here.
Emacs's notion of "word" is rather fixed and doesn't quite match what you want. You can mess with the syntax-table, but it might break functionality of the major-mode.
AFAICT what you want is to handle what Emacs calls "symbols" rather than "words". E.g. use forward-symbol and backward-symbol commands.
The easiest way to use those other commands might be to enable superword-mode.
Add following to your .emacs file to make hyphen included in words for every major mode:
(add-hook 'after-change-major-mode-hook
(lambda ()
(modify-syntax-entry ?- "w")))
You can use this code it use regular expression it use space or parents as word separator
(defun backward-word-with-dash ()
(interactive)
(search-backward-regexp "[ ()][A-Za-z\-]+ *")
(forward-char))
(defun forward-word-with-dash ()
(interactive)
(search-forward-regexp " *[A-Za-z\-]+[ ()]")
(backward-char))
(global-set-key "\M-b" 'backward-word-with-dash)
(global-set-key "\M-f" 'forward-word-with-dash)
if you want other characters just add it to the regex