Vim is awesome. And config files are half the fun!
For about two years, I've known about Vim and it's notoriety in both the developer community and the Linux community for being quicker, more expressive, and even a more native way of manipulating text. It was one of those things that I admired, knowing that once I took the dive, that I would never look back.
The possibilities are endless when it comes to Vim's and Neovim's config files, especially when accounting for the huge world of plugins available out there.
For a brief history, Wikipedia's entries on Vi and Vim show a good glance on the background of these programs: Vim. Vim was officially established in November 1991, so it's only a year younger than I! It's parent program it was derived from, Vi, was established near 1978! Besides it's historical significance, Vim has been automatically included in all Unix machines and will continue to be in the future, making it one of the most prevalent programs ever.
I wanted to share my Vim config and opinions on a setup to maybe persuade the common pillars of usage with Vim. I host in on a Github gist at the moment of this writing, and can be found here: my init.vim on GitHub
The characters: 0 and -
In Vim, 0
(the key for zero next to 9) is a very commonly used key for moving to the beginning of the line of your cursor. Inversely, the character $
is the key to move to the end of the line of your cursor. These are super handy to use, but why is the key to move rightward six keys to the left of the key to move the left most?
So there must be a use for the character `-`, the key just adjacent to the right of `0` right? Well, not really. I can't seem to find anything of use on this key, but there must be reason for this. Perhaps it is one of the many small nuances in Vim that seem funky to modern keyboards because of the way keyboards have changed since 1978. One example of this nuance is why the directional movement keys are h, j, k, and l
instead of j, k, l, and ;
.
I can't seem to find anything except for tradition for why the end-of-line
command shouldn't be the -
key. Could be becuase of a change in keyboard layout historically as well. But to update your Vim to modern keyboards,tTry it, and never look back.
Map
$
to-
for obvious left-orientation and ease of finger placement
The config line would look something as simple as bellow. We use map
instead of noremap
because we want the -
key to persistent into other commands that would involve $
map $ -
jj and/or jk
Now this is an extremely common configuration amongst Vim users. For anyone unfamiliar with jj
or jk
and know at least some Vim; mapping jj
and jk
to Escape
allows the typer to exit insert mode into normal mode without having to leave the homekeys. This is essential for me, especially because sometimes I use my 2017 Macbook Pro that has a touchbar. Having zero physical feedback for the escape
key is worse than scratching a chalkboard. Scratching chalkboards got nothin' on me.
What makes jj
and jk
usable in insert mode is because they aren't an occuring combination of letters in the english language. Well, in my normal flow when focusing on keyboard only, I often do a double h
or a double k
for when my fingers aren't aligned to the home row. So I was started pondering why wouldn't someone add in these binds as well? Unless you're typing in the word 'bukkake'... you should be fine.
inoremap <silent> hh <Esc>
inoremap <silent> kk <Esc>
d and x keys
So every Vim user knows d
and x
. d
is for deleting, or more specifically cutting, which goes into a register. Well, I thought it was a little peculiar that d
not only deletes, but cuts, while x
only deletes one character. x
has a use, but it's quite niche compared to modern text editing. So my suggestion is to change x
almost entirely.
Since Ctrl-x
or Cmd-x
is typically used for cutting system-wide in default operating systems, it makes the most sense to change x
to the previous functionality as d
whereas it cuts.
So here is a snippet of my init.vim
for how I rearranged these:
nnoremap d "0d
nnoremap x "*d
vnoremap d "0d
vnoremap x "*d
d
deletes, and when you want to cut, use x
.
There is a caveat with changing this. We're changing x
to act just like d
would, meaning it requires an operator (like in a verb-object way like kakoune explains). So something like xiw
works to cut the word under the cursor. Because of this, to delete a single character (the old default x
keybind), you can type dl
. To cut a single character, it would be xl
.
l
is the default object for a letter in Vim. Taking this into account, I like to bind c
in my operator mode to alias the l
key, since c
makes more sense for character more than letter, plus c
on the keyboard is adjacent to both d
and x
.
onoremap c l
That makes it really simple to dc
or (delete character).
June 16: update
Turns out, there is a very easy Vim plugin to do exactly what I outline with d
and x
called Vim-Cutlass
In addition, I converted my init.vim
file to it's own git repository. I had seen others I follow on Github use gists as an easy
publication of their vim config, but I find that using a git repository for this helps tremendously becasuse of the occasional update,
and also for easier redundancy across different computers that I use.
Vimium
Also if you're a Vim user, you're most likely using the Vimium extension in your browser for simple navigation. Vimium is fantastic, and just works intuitively (check it out if you haven't). The default keybindings for Vimium has the handy keybinds for d
for the Vim equivalent of <C-d>
(Or Ctrl-d
)for moving the page down, and also u
for the same thing appropriately for moving a page up. My proposed keybinding is setting e
to the same behavior as u
in Vimium so that you're using the same hand and same finger for scrolling up and down the page. Setting e
doesn't interfere with any other binding and can be done easily in Vimium's settings. In Chrome-based browsers, that is in Settings > Extensions > Vimium > Extensions settings, and then you can paste in:
map e scrollPageUp
Vimium's syntax is different from Vim's scripting, so no need for n
before map
or nore
.
Top comments (1)
Great article!
I found this
map $ -
mapping so useful