Previous Post | Top | Next Post |
TOC
I could use Vim as a basic editor with TKL-keyboard while tapping some keys multiple times. This was functional but not so elegant!
I decided to re-learn Vim to use it properly. Here is my learning memo.
For simplicity, I may use ^F
to mean CTRL-F
.
This page has been updated in 2022 since some basic summary is valid and common to neovim. See Re-learning Vim (4) to get the latest situation. (Modern configuration with which-key.nvim under neovim can allow us not to remember complicated bindings..)
Concept
- Use default vim (minimum configuration, if possible).
- Use as much native functionalities.
- Don’t force Vim to behave like another program.
Guiding voice
Here are a few insightful recommendations on the best practices for Vim.
- Voice of Vim God (Vim upstream: Bram Moolenaar): Seven habits of effective text editing
- RTFM or type “
vim '+h|only'
”. - Voice of Vim Gurus (April, 2016): #vim Recommendations
- Some interesting hints found:
Here, I summarized my rationale behind my configuration decisions for me to remember them.
I follow the voice if Vim Gurus from April 2016 (based probably on Vim version 7.4) as much as possible. Back then, Vimscript was becoming more like a real language but we didn’t have native support to the asynchronously executing background process. Now the asynchronously executing background process offering spell checker, lint, git monitoring, etc. are widely used useful features. Neovim is building on this becoming mature and it use of lua as script language is great.
Please note that I stop customizing vim and migrated to neovim for fancy
customization. I only use vim as my backup editor when I suffer issues with my
neovim. (My old ~/.vim/
directory implementation is published at
https://github.com/osamuaoki/dot-vim . This uses Vim 8 native package
management system via ‘packadd!’, git
and git submodule
.)
For vi
, and nvim
, neovim is used on my system. (vi
is accessed via bash alias)
For vim
, vim is used on my system.
I also create bash aliases:
v
to startnvim -u NORC
v0
to startnvim -u ~/.nvim
(very simple setting)
These are handy when Neovim configuration files contain bugs.
(Neovim uses ~/.config/nvim
instead of ~/.vim/
for its configuration)
NORMAL MODE
Unlike other editors, the existence of NORMAL MODE is what makes Vim as the great editor which allows the minimum usage of the ALT-key and CTRL-key.
Cheat sheet
Here is my cheat sheet to remind me of all the important key strokes used around NORMAL MODE with emphasis on motion keys (mono space font to see this correctly):
NORMAL MODE <ESC>: Goto BOF → gg r +--- Next Char
screen n N # ?ααα e / +-- End of Word (non-alnum)
scroll ai+wW({[<sp"'`↓ ^B t / / +- Word Next (non-alnum)
↓ Change c{motion} cc c↑ ^U n / / / + End of Word (space)
↓ Delete d{motion} dd d↑ H e / / / / +Word Next (space)
+--+ Copy y{motion} yy y↑ { C / / / / / +Till α, x Delete char
|zt| Put After/Before p / P ( / / / / / / / Find α, r Replace char
|^e| "α Use α as register k/ / / / / / / / EOL, ~ Change case
|zz| 0 ^ Fα Tα gE B ge b h M l e w E W tα fα $ gd Goto definiton
|^y| ↓↓↓ Split Window ↓↓↓ j ←--- Line (J for Join line) ^] Tag jump
|zb| ^W + ncsvhjkl=+-_<>| ) ←--- Sentence ^T Tag jump-back
+--+ } ←--- Paragraph ^I/^O Jump next/prev
Forward Backward Search L ←--- Bottom of Page {visual}> Indent →
n N /ααα ?ααα ← Find ααα ^D ←-- Prev 1/2 Page {visual}< Indent ←
n N * # ←--- Indicator ^F ←-- Next Page {visual}= Indent ⇆
, ; tα Tα ←-- Till α /ααα ← Find ααα * n N {visual}gq Format Text
, ; fα Fα ←-- Find α G ←--- Goto EOF J Join line
Next Prev % ←-- Match [{ ]} [[ ]] [] ][ Look for {}
Record: qα, End-Record: q, Play-Record: @α, Mark as α: mα, Jump to α: `α/'α
Undo: u, Redo: ^R, Repeat: . , Fold: Open zo/zO/zr/zR, Close zc/zC/zm/zM
Insert Mode: i/a Before/After Char, I/A Before/After Line, O/o above/below
Visual Mode: v Char V Line ^V Block, Replace Mode: R, Ex Mode: : or q:
This is intentionally terse. ^
is used as prefix for CTRL-key.
I found some key sequences such as 3w
, bc$
, ciw
, 3yaw
, … quite useful,
now.
Hints for memorizing key strokes
Since most key assignments are chosen from the initial of the corresponding English word, they are quite intuitive. But, some key assignments look confusing. Here are my hints for memorizing such key strokes.
- Cursor move keys
j
shape points to downward.k
shape points to upward.
- Screen scroll keys:
^B
: Backward page^U
: Upward 1/2 page^Y
: expose up 1 line == Yester(day)-line, next to ^u (Keep cursor)^F
: Forward page^D
: Downward 1/2 page^E
: Expose down 1 line, next to ^d (Keep cursor)
Interesting commands
ga
: Get Unicode point (HEX etc.)g8
: Get Binary data HEX sequence^G
: Get filename and cursor position (see “:h 'ruler'
”)g^G
: Get cursor position in column, line, word, character and byte{visual}g?
: ROT13 (Super insecure encryption)
Map leader key
I map SPACE-key as leader-key in NORMAL MODE since it has no practical use and
easier access than default \
-key.
let mapleader = ' '
Some options
I tend to set as follows in the vim startup file (~/.vimrc
or its equivalent):
" make vim copy buffer bigger (default 50 lines: viminfo='100,<50,s10,h)
set viminfo='100,<5000,s100,h
" Allow to move cursor beyond for block
set virtualedit=block
INSERT MODE
INSERT MODE is started from NORMAL MODE by pressing i
, I
,a
,A
,o
, O
,
etc.
COMMAND MODE
COMMAND MODE is normally started from NORMAL MODE by pressing :
or /
or ?
.
You can use NORMAL-MODE-like editing for COMMAND MODE situation by starting it
with q:
or q/
or q?
.
TAB in COMMAND MODE
Normally, TAB
in COMMAND MODE starts wildmenu under :set wildmenu
. You
can use TAB
and S-TAB
to chose the one you want.
Basics in COMMAND MODE
:e ααα
: Edit ααα file:w
: Write file:wall
: Write all file:up[date]
: Write all modified:wq
: Write and Quit:q!
: Quit (force):bα
: Edit buffer α:bn
: Edit next buffer:bp
: Edit previous buffer:r ααα
: Read ααα file to cursor:r! ααα
: Read ααα command output to cursor:%s/ααα/βββ/gc
: Replace ααα with βββ for all lines with confirmation:bufdo ααα
: Execute ααα in each buffer in the buffer list.
Use |
as in-line command separator.
EMACS/shell-like key sequences
Here are some non-intuitive key sequences used by INSERT MODE and COMMAND MODE and looks like EMACS/Shell sequence.
- INSERT MODE and COMMAND MODE
^H
: Delete a character before the cursor. (same as<BS>
)^W
: Delete the word before the cursor.^U
: Delete all entered characters before the cursor.^P
: Find previous keyword.^N
: Find next keyword.
QUICKFIX MODE
If you have a task to search some text to make Vim to jump to particular places
in its buffer, QUICKFIX MODE is the one to automate your process. No more
grep
on another terminal window.
There are 4 basic commands to start preparing jump list data.
:mak[e]
: Runmake
for compile-jump-edit cycle. (:h :make
):gr[ep]
: Run system-grep
for search-jump-edit cycle. (:h :grep
):vim[grep]
: Run internal-grep
for search-jump-edit cycle. (:h :vimgrep
):helpg[rep]
: Run internal-grep
on all help text files for search-jump-read cycle. (:h :helpgrep
)
Then you access and use the jump list from COMMAND MODE.
:cw[indow]
: Open the quickfix window if error.:cope[n]
: Open a window to show the current list of errors.:ccl[ose]
: Close a window for the jump list.:cn[ext]
: Jump to next quickfix item location.:cp[rev]
: Jump to previous quickfix item location.
Please note there are l...
variants of above commands.
TERMINAL MODE
This is a new Vim/NVim feature. Now capturing shell session is as easy. No more envying EMACS terminal mode.
:ter[minal]
: Start TERMINAL MODE. (:h :terminal
)^WN
or^\^N
: Switch to TERMINAL-NORMAL MODE.- I will map
<Esc><Esc>
to^\^N
for more consistent UX
- I will map
^W^C
: Send a^C
(0x03, ETX) to the job in the terminal.^W.
: Send a^W
(0x17, DC1) to the job in the terminal.^W^\
: Send a^\
(0x1C, FS) to the job in the terminal.^W:
: Enter an Ex COMMAND MODE.^W^W
: Move focus to the next window
From TERMINAL-NORMAL MODE, typing i
gets you back to TERMINAL MODE.
TERMINAL-JOB MODE
Don’t run Vim inside TERMINAL-JOB MODE. I ended with ‘buftype’ error. It becomes ’terminal’ type.
Terminal settings
For terminal, set DEL
-code(0x7F) for Backspace and use escape sequence for
Delete. This frees the ASCII BS
(CTRL-H) code so window jump can be mapped to
<C-H>
.
Previous Post | Top | Next Post |