vim + space = ️
Konstantin Epishev
Posted on February 27, 2021
About one year ago I started using vim
as primary text editor in my job. Sometimes I think about emacs
and its distributions with mnemonic keybindings focused on space-key usage. It's very elegant and easy to remember <space>-f-g
to global find, or <space>-g-s
to reveal git
status. As leader
key it allows to use some plugins with pleasure.
For example vim-surround. Instead default \S
you can easily press <space>S
.
In that way space
is better for your health, because the thumb is the strongest finger on our hand. When you're using pinky finger to press capslock
or \
as leader
key it not so comfortable and your hand getting tired much faster.
About one month ago I've discovered, that it's possible to similarly use space
in vim
.
I know about spacevim, but it's so complicated and I like to build my editor by myself, keeping only features what I really need.
Let's try it!
Add vim-which-key
vim-which-key
allows to see all your keybindings at any time. It looks absolutely like the same feature in emacs
(and in spacevim
too).
Initial configuration
Add a few lines to init.vim
:
let g:which_key_map = {}
" needs to call `whichkey` on `whitespace` press
call which_key#register('<Space>', "g:which_key_map")
" reassign default space behaviour in normal mode
nmap <SPACE> <Nop>
" map fuzzy search on double space
noremap <Leader><SPACE> :ProjectFiles<CR>
let g:which_key_map['SPACE'] = 'project-files'
" open `whichkey` after single space press and small wait
nnoremap <silent> <leader> :<c-u>WhichKey '<Space>'<CR>
Example bindings for git
Add some useful bindings for vim-fugitive:
let g:which_key_map.g = { 'name': '+git' }
noremap <Leader>gs :G<CR>
let g:which_key_map.g.s = 'git-status'
noremap <Leader>gc :Gcommit<CR>
let g:which_key_map.g.c = 'git-commit'
noremap <Leader>gd :Gdiffsplit!<CR>
These bindings can be easily remembered as git - status
or git - commit
.
That's all! If you know other useful hacks for vim
let's discuss it! ✨
Posted on February 27, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.