Using Vim/Emacs is like a refactoring process
This is an idea I got when I was still using vim, and I think this feeling got stronger after I switched to Emacs. It's a bit hard to find out if anyone had similar idea before, because googling "Vim/Emacs Refactoring" would only show some refactoring plugins or packages. So I decided to write it down and share it.
The definition of refactoring is:
Restructuring the existing code without changing its behavior.
So what are we restructuring here by using Vim/Emacs? I think it's our text editing skills.
It's kind of obvious since they are both text editors. But they are so powerful that:
- They can abstract text editing to a higher level.
- We can then think of the editing process as a piece of code.
- After that, we can refactor this code to make it more efficient.
Minimize key strokes in vim
Find more efficient keys
In vim, we do most of our text editing in the normal mode thanks to its powerful modal editing. Each key is bound to an action, e.g. h/j/k/l is left/down/up/right.
I believe most people just uses hjkl to move around the cursor when they just starts learning Vim. But it's not that efficient and it's still thinking in the normal text editor's way.
Turns out you can move your cursor by using other keys1:
geto move between words
;to move within the same line
)to jump between paragraphs, sentences
Nto search texts directly
Operate on text objects
Keys can be combined together to define the action target (text objects), e.g.
d is for deletion,
i is for inner,
p is for paragraph,
dip is deleting
all texts inner this paragraph.
These habits changes are like replacing a
for-loop with a more efficient
native function. After you get familiar with it, your text editing will
definitely be more efficient.
Recording keystrokes in macros
Macro is one of the most powerful things provided by Vim. You can define a macro and save you hundreds and thousands of keystrokes.
But the more powerful feature is that you can edit an macro like editing a piece of code2.
- Macros are also saved in (paste) registers.
- So you can paste a macro into a buffer then edit it and save it into the register.
- Furthermore, you can save this macro as a function in your rc file and use it in the future.
This makes using macro more like refactoring, because once you find a way to generalize your operations on texts, you can record it as a macro and even edit it as editing your code.
Minimize function calls in Emacs
Everything is a function
This was a mind-blown point to me: in Emacs, every operation is a function.
Even typing a key and inserting the character on it is also a function called
This turns your text editing process into a coding process: you are writing a code to write the code you want to write. (Meta-Programming!)
Macro can be turned into function
Native Emacs records a keyboard macro like Vim does: just saving the keystrokes. But it provides a better editing environment for keyboard macros and you can even see which function each key represents:
;; Keyboard Macro Editor. Press C-c C-c to finish; press C-x k RET to cancel. ;; Original keys: SPC w l SPC w h SPC w s SPC w k Command: last-kbd-macro Key: none Macro: SPC w l ;; evil-window-right SPC w h ;; evil-window-left SPC w s ;; evil-window-split SPC w k ;; evil-window-up
And there is a package elmacro3 that allows you to translate a keyboard macro directly into elisp function.
- Find a more efficient native function to do the work
- Automate things by keyboard macros
- Persist macros into functions in your config file