Help me understand vim motions
I use vim on remote servers or on my machine to edit single files. I, however, use it in a very basic sense, I do not use any vim motions. I enter edit mode, I change what I need to change, exit edit mode and save and quit, that's it.
Recently, I've been looking for alternatives to Visual Studio Code as Microsoft is starting to push Copilot very heavily and while I could use a cleaned up fork, the other concern is with more and more essential extensions becoming closed source and subject to Microsoft's licensing. And vim is a text editor that pops up over and over when I ask for recommendations.
A few days ago I've listened to No Boilerplate's Writing at the Speed of Thought which brings up a point about vim and vim motions being designed around the human body and how "editing by letters is extremely unnatural ... [and] extremely ill-suited to our nature".
That just doesn't sit well with me and may be the reason why vim never fully clicked with me. For context, I've been using computers in some capacity since a very early age, so perhaps the 'unnatural' way I've learned is so ingrained that I just can't make the switch, maybe I just think about things in a way that is more computer-centric just due to that as well.
I am still on my quest to replace VSCode and I would love to make a switch to something that's less attached to a single corporation that can pull the rug from under me at any time. A part of that quest I guess turned out to be trying to understand vim and maybe finally making it click for me, so I turn to the wonderful community of Tildes for help :)
Thank you
The thing that helped me and sort of made the whole modal concept "click", was thinking about it is a grammar. There are vim nouns, vim verbs and using those on text objects. Here is a good tutorial to get started.
Wow I wish I read that a decade ago when I was first shown vim
I'm going to recommend doing the hard work and not wrapping any funky language around Vim. These are the fundamentals:
It's a modal text editor. "escape" for neutral, "i" to edit before the character, "I" at the start of the line, "a" to edit after the character, "A" at the end of the line. :w to write the file, :q to quit, ":" to do stuff, and ":q!:" if you don't know wtf you're doing. Hit "escape" if you feel lost, and when in doubt, force exit with :q! to not save. If you can't enter ":", press "escape" until you can, and hit ":q!" until vi/vim quits (you might have hit F1, which starts Help, which can be confusing).
I once got a call from a coworker who opened vim on a Linux server and didn't know how to close it. We all had a light-hearted chuckle and moment of commiseration. My early Linux days saw me opening vi and getting similarly lost.
I started with vi/vim on a practical foundation. vi is everywhere, vim is its advanced cousin, so if you know the UNIXy little brother you're set.
Have you done vimtutor? That's the official tutorial and generally the best way to get started.
If you have regular vim, just run the
vimtutor
command in your terminalIf you have Neovim, open it, then type the
:Tutor
command and press enter.The two versions are basically the same, the Neovim one is just a bit more interactive and improved a bit in some places, but they both teach the same basic motions and concepts.
If you actually intend to try and use it as your main editor, I strongly recommend Neovim over regular vim. There's basically no reason to use vim over nvim, as nvim has much better defaults in many places and Lua configuration is much nicer than Vimscript, especially when you start adding plugins.
Also, if you want to write a Neovim config, I HIGHLY recommend you check out kickstart.nvim.
It is a small "starting point" for nvim configuration. It sets up all the useful things like autocompletion for you. But it's very thoroughly documented and explains how it works in great detail, so you can easily go in and start changing things to perfectly fit your needs (which is the entire point of configurable editors).
From their own description:
Lua config is nice for real programmer. Vimscript config was much much nicer for me as a non-programmer. I barely change anything about my plug-ins on Neovim now. Thankfully, I'm way more of an Emacs user now, and Elisp is quite pleasant for someone who refuses to learn programming.
I wouldn't say Lua config requires you to learn programming, though it's definitely more verbose for simple things than Vimscript (eg.
vim.keymap.set("n", ",", "<C-w>")
instead ofnmap , <C-w>
- after all, the entire point of a DSL like Vimscript is to make common use cases as simple as possible. Vimscript does get much harder as soon as you try to do something more complicated than setting an option or mapping a key.Really? I used Doom Emacs for a while, and I had to look up much more stuff about elisp than I have to do for Lua with Neovim. For example, I had this line in my config, which I copied from somewhere on the internet:
What does this syntax mean? Why are there double parentheses around
undecorated . t
and a'
before it? Why is there a.
between two words? Someone who has no experience with Emacs Lisp probably couldn't write this line on their own.It worked fine as long as I just copied examples from somewhere else with minimal changed, but I couldn't really configure anything on my own without looking into elisp concepts.
In order to configure nnn.nvim, I must make sense of this wall of code:
lua code
That is not something I can or wish to learn how to do. I know this may sound ironic coming from an Emacs user, but that's a lot of curly braces to figure out. I tried and failed. And I don't want to just paste the entire default config into my init. That is just messy. To make matters worse, AI is kinda useless for this. Configurations in Elisp, on the other hand, are something AI understands very well. Not that I use it a lot, but when that is necessary, it often generates usable code.
I don't have anything like that in my config. That makes me think that this is either a setting I don't need or something that can be done in a much simpler way. Also, a lot of settings can be generated using the
customize
graphical interface. I will sometimes move automated settings fromcustom.el
intoinit.el
. Some users are a bit elitist aboutcustomize
, but it is perfectly fine for lots of stuff that would be a pain to do by hand.Overall, I find Elisp extremely friendly to manipulate without really coding anything. And due to the nature of Emacs, I can randomly change stuff and see what happens. A lot of things on Emacs and Elisp are extremely regular, so once you recognize a pattern, you can usually repeat it for similar stuff elsewhere.
Elisp feels very predictable to me, on a very basic visual level.
I fully acknowledge that Elisp is only nice because it is inside Emacs, with all the resources available to manipulate it. On its own, it would probably suck.
But I love it. I don't know how to explain this, I don't have the vocabulary to make precise statements about code. I just freaking love it.
I like to think of vim as spellcasting. Or a combo chaining video game for editing text. The first time I watched somebody use vim at an advanced level it was like watching a wizard raising a mountain out of the ground and forming into a castle. I use vim daily, and I still wouldn't consider myself advanced. I just learned enough to be 90% more productive.
You're chaining together all sorts of operators to perform an action. Sometimes they make sense, sometimes they are arcane. But it imbibes a sense of power.
:wq
is one such example. "Command, write (to disk), quit". Drop the w, add an ! at the end, now it is "Command, quit, forcefully." It won't save because you removed the w.Hence why vim has Command Mode and Edit mode. You are shifting modes, between casting spells and entering text. So all that's left is to learn the spells. Here is the most useful 101. Some words need translations from Old User to Modern User.
As @knocklessmonster mentioned, if you get lost and things don't work right, hammer a combo of Escape, q, and Enter until it does nothing then force quit. There are edge cases where escape itself won't be enough to get you into command mode.
The colon is used for special commands. Unless otherwise stated, these are just typed in.
d - delete (cut)
y - yank (copy)
p - put (paste)
Paste is a one off. "Put what you have where you are."
The other two begin and end a spell. To delete a line, you type
dd
, the beginning and end of the spell. There are other ways of ending the spell, the one I use most isG
, Go to end. By just entering that it jumps to the end. So to delete everything from where you are to the enddG
.Then you place modifiers between. Numbers are the best.
With a colon (
:50
), you will jump to line 50. With this combod5d
, you delete the 5 lines starting from your cursor. So a full spell:Jump to line 20, copy it and the next 9 lines, jump to the end of the file, paste them.
Maybe more later.
Huh, interesting, TIL that the number can go after the verb. I always did number first, e.g. 10yy, 10dd
The difference is that one of those is 10 undo-able with
u
(or redo-able with.
) actions, and the other is just one action affecting 10 lines. Figuring out which is which is left as an exercise to the reader :)Highly, highly recommend the built in vimtutor.
I work as a software engineer, and use vim daily (mostly in VSCode).
About 90% of my usage is:
ci{
orci”
for “change inside braces/quotes”y10y
“yank from here to ten lines down”d10d
“delete from here to ten lines down”cw
“change word (deletes from the cursor the current word to the right, and enters insert mode for typing”c$
“change line (deletes from the cursor to the end of the line, and enters insert mode for typing)”Save a keypress with
C
!I think the best way to understand Vim is to use Vim. In abstract, it doesn't make a lot of sense. It is a tool, not a theory. I suggest doing the Vimtutor a bunch of times.
When you learn Vim, it becomes pleasurable to use. More than anything, Vim is pleasant. It won't make you 100 times more efficient; that is just silly. I don't buy into the grandstanding about Vim. It is a text editor, not a way of life. In most situations, typing speed is not a significant bottleneck, and there are many tools that do the same things Vim does. But it is pleasant.
I dunno, I took the time to learn Vim like 10 years ago when I was in university and had nothing better to do and it's like riding a bike. It was shit for months, then one day it clicked and I can just go back to it and edit text reasonable quickly.
Problem is for coding I'd just not use it today because of stuff like intellisense and stuff like the solution explorer etc all in the IDE.
I tried the Vim extension for VS code for a bit and I nearly kept it, but the way it was implemented as kinda a second class service (some Vim shortcuts got eaten by VS code) killed it in the end for me.
Anyway what I'm trying to say is, if you really want to learn Vim for whatever reason, you can I'm sure if you just restrict yourself to using it and learn the shortcuts to make the work flows reasonably quick. But it'll take a while to learn.
LSP is supported natively by Neovim
I might try it again.
My remember is tingling and is telling me there's a reason why I shyed away from NeoVim but I can't for the life of me remember.
First, I'm using the open source branch of VSCode, VSCodium. Bypassing Microsoft marketplace restrictions is possible (I'm not sure about posting it here, but you can find it online - it's essentially a plist file edit.)
Learning Vim in a week convinced me to start Vim. The main idea I like the most is that you can learn 1-2 new bindings a week and see what sticks with you, and what you use the most daily sticks through.
I still use Vim only partially, because I sometimes don't remember the cursor jump bindings, but it's that accuracy that No Boilerplate was talking about - rather than having to manually put the cursor to where you want, either by mouse or arrow keys, you know precisely where the cursor would end up using (w), (b), (e), (g), (a)/A and so forth.
I think over time people just keep what they find useful. For me it's (o)/Shift-o for newlines, d[num]d for deleting any number of lines, and ci[bracket] to (c)hange content (i)n a bracket pair.
Also, remap your Caps Lock to Esc. escaping Insert mode with the left pinky is much more natural. (My custom keyboard layout is also mapping i to the right pinky so entering/exiting is symmetric for me, but that's beside the point)
Do you know of a way to remap Caps OS-wide on Linux? It's useful in vim, but my god it would be more useful everywhere.
Using KDE/Wayland on Arch (Endeavor) specifically. So far the answers I've found are all over the place.
Edit: Has somebody made a custom keyboard layout that just has 2 ESC keys? I'll have to search that later.
Check out kmonad. AFAIK it's the best keyboard remapping tool out there (unless you have a programmable keyboard)
Pretty off topic, but it baffles me that caps lock is even still a thing. It's such a prime location for a button that is practically never used on purpose. I've long wanted a keyboard that replaces all the lock key functionality with physical switches and gives me the button spaces back as non-modal buttons.
The reason I want switches is that I find the modal buttons that also lose their state for a variety of reasons rather silly. Not only is it silly that I have to press num lock every time I turn on my machine, but I have to do so twice because it gets cleared between two startup screens.
I bounced off nvim as an IDE, in part because so many of the nvim "distros" are a bit overwhelming out of the box.
I made the switch to Helix and found it a bit easier to get into, some of the changes to vim syntax make a bit more sense to me, primarily switching the verb and noun in a lot of the actions (eg:
wc
for "select the next word, then change the selection" in Helix rather thancw
in vim)I leveled up in vim using vim adventures. I didn’t even know there was a built in tutorial, probably because I got the basics down using vi a long time ago.
I leveled up again pair programming with someone who was incredibly good with vim. If you ever get this opportunity, I highly recommend it once you have the basics down. The advanced stuff falls under the you don’t know what you don’t know principal. It never even occurred to me to edit columns in vim, now I use that functionality often.
I used Vim as my primary editor since I was in university in the late 90s until I switched to Helix just recently. I'm a software engineer so I probably spend around a third of my total waking hours sitting in front of an editor. A lot of people suggest that thinking of Vim keybindings as a logical grammar is useful, and that may be the case for some, but for me it's almost entirely about building muscle memory around specific tasks over time. Usually it stems from having to do some repetitive, tedious task over and over again--I'll look up how I can save myself some time with Vim keybindings and then use it whenever I come across something similar to that task until it becomes second nature. Once the small, simple time savers get engrained as muscle memory you'll find yourself moving on to more and more complex ones until you eventually become a Vim ninja.
I wrote a silly story a long time ago trying to express that same sentiment called Vim Creep.