reddit / emacs

Semicolon to underscore trick

gopar · 2022-12-13 17:23
Hey, all. Here's something easy i've been doing for the past few years to avoid typing the underscore. (defun gopar/easy-underscore (arg) "Convert all inputs of semicolon to an underscore. If given ARG, then it will insert an actual semicolon." (interactive "P") (if arg (insert ";") (insert "_"))) (global-set-key (kbd ";") 'gopar/easy-underscore) If you do `C-u ;` then you get a normal semicolon. === I was doing the same thing with JS where if I type the semicolon twice, it will insert a semicolon but if I type a letter it will automatically uppercase it for camelCase. (defun easy-camelcase (arg) (interactive "c") ;; arg is between a-z (cond ((and (>= arg 97) (<= arg 122)) (insert (capitalize (char-to-string arg)))) ;; If it's a new line ((= arg 13) (newline-and-indent)) ((= arg 59) (insert ";")) ;; We probably meant a key command, so lets execute that (t (call-interactively (lookup-key (current-global-map) (char-to-string arg)))))) &#x200B; Ya'll have anything like that worth sharing? :)
eshelyaron · 2022-12-13 20:08
Nice! C-u ; seems a little redundant since you can just as well do C-q ; right?
gopar · 2022-12-13 21:06
TIL about `C-q`, surprised I never noticed it.
terminal_cope · 2022-12-13 23:30
For minimizing awkward finger movements, I feel like a decent approach would be to borrow techniques from the tiny keyboard crowd make a multi-layered keyboard layout to minimize movement. e.g. some people have [34 key keyboards](https://github.com/pierrechevalier83/ferris) with multiple layers triggered in various ways to get the full range of input. (Some people go even further, [e.g. to 16 keys](https://www.youtube.com/watch?v=5RN_4PQ0j1A&list=PLCZYyvXAdQpsEWfa6OEBOhHn48SWgneoD&index=12)). A tool like [kmonad](https://github.com/kmonad/kmonad) can be used to do this sort of key map on existing keyboards at the OS level rather than in a keyboard microcontroller.
yurikhan · 2022-12-14 06:43
Many years ago, my employer gave me an ISO layout keyboard. It has this extra key between left `Shift` and `Z`, and it by default does the same that the `\|` key does. I mapped it to enter `_` by editing the layout in `/usr/share/X11/xkb`. Since then, I got into DIY keyboards, and I kept the underscore key to the left of `Z`, and also added a double quote key to the left of `A`. ---- As for Emacs tricks, I arrange for `;;` to expand into `::` in c++-mode, `-.` to `->` in C++ and Python, and `!!` to `!=` and then `!=!` to `!= nil` in Go. (Because Go has a lot of `if err != nil { return something, err }`.) I do that by rigging abbrev to trigger not only on space but also final characters of each punctuation-based expansions.
_viz_ · 2022-12-14 17:24
>I do that by rigging abbrev to trigger not only on space but also final characters of each punctuation-based expansions. Does this mean you patched self-insert-command to remove/change character before point is a word constiuent check? My attempts at achieving abbrevs like !! failed due to this check.
yurikhan · 2022-12-14 18:18
The simplified description of the mechanism by which my hack works is: * Have abbrevs arranged into tables by major mode, defined with an `:enable-function` that checks the major mode, and `:regexp` that matches all of that mode’s abbrevs’ names. * `(add-hook 'post-self-insert-hook` a function that checks `(char-before)` against those abbrevs’ names’ last characters, and invokes `(expand-abbrev)` if it finds a match.
_viz_ · 2022-12-15 03:40
That makes sense and thanks for post-self-insert-hook! I didnt know of it. I had hoped to avoid any hooks but this will do.
yurikhan · 2022-12-15 06:37
Better than advising ;) Do avoid doing heavy things in a post-self-insert-hook though.