Gavinok / emacs.d

nothing to see here 👀
87 stars 10 forks source link

From Vim to Emacs. #3

Open karbiv opened 2 months ago

karbiv commented 2 months ago

I use Emacs for many years, and tried Vim recently to explore whether Evil mode is worth it.

Emacs uses mnemonic mappings. C-f, C-b, C-p, C-n. Forward, backward, previous, next. What many users of Emacs and those coming from Vim don't know is that two keys on keyboard must be remapped for Emacs.

Control now is pressed by left thumb that never ever gets tired. It's under "x" letter on most keyboards and all notebooks. The small problem are those classic mechanical keyboard layouts where Left-ALT is located more to the left from "x".

This keyboard remapping is useful for Vim too. Instead of ESC for exit use C-c, left thumb + pointing finger down from "f". Even more, with this remapping utility https://github.com/rvaiya/keyd CAPS is ESC and ALT when pressed with other keys.

This ergonomic enhancement by remapping two keys is not more popular because besides other things there was a mistaken recommendation from R.Stallman not to use keyboards with flat keys. Those are actually the best, compact.

With this, Emacs' default mappings are actually ergonomic. More so than Vim's maybe. "C-a" and "C-e", "at start of line" and "end of line". "M-m" first non-blank char in line. With such mappings in Emacs CAPS(which is ALT now) can be pressed by a ring finger instead of pinky.

karbiv commented 2 months ago

Just tried puni, discovered in this init. Mostly to check what puni-expand-region does.

expand-region package works better. In Vim terms it selects in this sequence: word, WORD, inner in block/quotes. puni-expand-region starts selection with WORD and doesn't restore point position on C-g, keyboard-quit.

expand-region and exchange-point-and-mark ("C-x C-x") for region in Emacs generalize most of Vim's operators&moves.

karbiv commented 2 months ago

Neovim users with Lua have almost nothing in comparison.

Emacs is a Lisp machine that can be showcased. "C-M-x" is eval-defun. So what? Vim and Neovim can do ":source somescript" to reload any script or config. "C-u C-M-x" is eval-defun with prefix argument, it instruments current Lisp function definition(where cursor is). When it's invoked during work it's immediately shown in a window in debug stepping mode. Space key to step forward over code, "c" for continue to execute w/o stepping if there are no breakpoints. Traceback is available to see callstack - what invoked this func. "C-M-x" again on that function to eval without debug instrumentation.

REPL terminal in such a Lisp machine is not used directly, there's no need. Programmers usually imagine REPL just as some kind of a fancy terminal shell. Because it is so in most of implementations.

This is what dynamic typing is really for. Re-eval any function, not a whole module. Immediately test it. With static typing re-eval would require type analysis of a whole program in runtime which is not always feasible.


"Jump to definition" works by default for any loaded Lisp function in Emacs. Vim has only "K" - search in help files for a name under cursor. How that search in Vim works exactly, defined somewhere in C source code.

If Emacs' C source from Git is installed somewhere then Emacs help can show definition of any native function, jump to it: (setq find-function-C-source-directory "~/path/to/emacs/src")


In Lisp it's not quite two namespaces for functions and variables. It's one namespace for symbols where symbol is basically a C struct with slots for function and variable. And a slot for symbol properties. In Lua the only container type is hashtable which is more expensive than symbol.

Gavinok commented 2 months ago

Just to clarify something the way K looks up the help for the name under the cursor is using keywordprg. it's set to keywordprg=:help.

karbiv commented 2 months ago

the way K looks up

And there's no feature in Vim that lists all mappings in current buffer. I learned about "K" in reddit thread IIRC. I've found so many deficiencies in Vim that I no longer explore it. But Vim is good nonetheless.

I came to conclusion after evaluating Evil mode that it's too restrictive for Emacs. Emacs' mappings with CAPS->ALT and LALT->CTRL remap in OS were always more ergonomic and extensive.