
To Master Vim, Use It Like Language - danielmiessler
https://danielmiessler.com/study/vim/?fb_ref=118ef0e03ab54c0d8197214328648a68-Hackernews
======
stevebmark
This article is well written, but it has nothing to do with mastering Vim. It
goes no further than running "vimtutor" at the command line. It has all the
same tips and shortcuts as any other vanilla Vim tutorial. This is a common
problem among Vim articles and tutorials. Authors title them as if it will
give you Vim mastery, but instead it only offers the basics.

Mastering Vim means going beyond vanilla movements and learning all of Vim's
nasty warts to replace good GUI features. It means learning the wonky, dead
Vimscript language. It means knowing how to use the arcane arglist. It means
knowing how to go into insert-normal mode, and when to use the expression
register. It means knowing everything about very magic search, and why default
search is poorly designed. These are all randomly chosen examples, because
mastering Vim involves mentally juggling a very large playbook of oddball
features to complete editing tasks.

The main resources to help you master Vim are the book "Practical Vim" by Drew
Neil, and if you're ever in the unfortunate position of needing to write or
edit a Vim plugin, "Learn Vimscript the Hard Way" is invaluable
[http://learnvimscriptthehardway.stevelosh.com/](http://learnvimscriptthehardway.stevelosh.com/)

~~~
danielmiessler
Article author here.

The irony in your comment is that I recommend Drew's book in the primer, and
Drew has recommended this primer as well.

Maybe they're both good ways to learn Vim.

~~~
stevebmark
Then title it "Vim Primer," which is accurate.

~~~
coryfklein
This is hardly a primer. If somebody is using the motions and actions
described in the article they may not be a complete wizard in obscure vim
features, but they are certainly far beyond being merely "primed" to use vim.
I've been an avid vim proponent for years and I still picked up new things
from this article.

------
DarkTree
My issue with getting better at vim is that I've gotten to the point where I'm
comfortable moving around text, copying/pasting, and everything I would
normally do with a mouse. I know vim is so much more than that, but it's one
of those things where "you don't know what you don't know" and because of
that, I don't have the incentive to continue learning. Hopefully this post
will inspire me to incrementally advance my vim usage.

~~~
thesteamboat
Aiming to combat "you don't know what you don't know", here's a list of things
to consider. How comfortable are you with:

* find/replace/regexes

* registers and macros

* vim settings (spell, list, highlight, number, wrap)

* the plugin ecosystem

* editing multiple files simultaneously (splits, buffers, moving between them)

* folds

Anyone else want to chime in with stuff I've either forgotten or don't know I
don't know?

~~~
base698
I asked on /r/vim about "Who uses folds?" Didn't get any takers. I always
forget they exist and haven't used them in my workflow yet.

Tag files would be another good one.

~~~
mdup
I use them all the time (I've switched them on by default in .vimrc) while
editing C++ code. Extremely, extremely helpful to get the structure of a file
at a glance. For example, if a 400-line file has about 10 functions in it, you
can fold all "zM" and instantly you see only the function signatures, while
the bodies are folded.

How to enable it: use "set foldmethod=syntax" (with C++) or "set
foldmethod=indent" (with JS, where "syntax" doesn't work for me). Also, I use
"set foldminlines=0" so that 1-line paragraphs get closed as well (it looks
more consistent).

How to use it: close all folds "zM" (I remember it because the M is shaped
like it's all folded on itself). Open all folds "zR". Open one fold "zo",
close one fold "zc". Open one fold recursively "zO", close one fold
recursively "zC".

By the way, I almost never use manual folds, so "zf" is almost useless to me.

------
blymphony
I consider myself at least an intermediate user of Vim, and I'm glad that you
mentioned the "f" motion (along with its cousins "t" and ";"). I don't think
these keys get enough exposure among beginner Vim users (they aren't mentioned
in vimtutor), but they allow you to quickly and efficiently move across files
and do things like "ct(" (change text up to first left parentheses), which is
super useful for changing function names.

Shameless plug: I wrote a plugin ([https://github.com/unblevable/quick-
scope](https://github.com/unblevable/quick-scope)) that facilitates the use of
"f" and family that even beginners can take advantage of.

~~~
0942v8653
The commands you mentioned along with "ci'", "ci[", and "ciW" to replace all
text between delimiters have been some of my favorite to use. It really annoys
me when my terminal's vi-mode doesn't support "ci" (I think it's vim-only).

~~~
nitrogen
The "ci" and "ca" commands are part of a Vim-only feature called Text Objects
(type _:help objects_ in Vim's command mode, or search the web).

I like using _yap_ and _dap_ to copy/move paragraphs around when I'm editing
markdown text.

------
PhasmaFelis
The thing that always frustrated me about Vim (and command-line apps in
general) is that it punishes you for being a GUI power user. The more mastery
you have of conventional GUI keyboard shortcuts to highlight, jump words,
copy/paste, etc., the more often you will reflexively hit them and do the
wrong thing while trying to use Vim. You have to unlearn a lot of perfectly
good skills before you can really master Vim, and that's frustrating.

Of course it's not Vim's fault that the standards established by vi were not
taken up by the early GUI systems, but I wish there was a better way to deal
with this problem than a figurative grizzled old console cowboy going "suck it
up, n00b, it builds character." I'm aware that people have made command sets
that make Vim and the *nix console in general more GUI-standards-friendly, but
these are never presented to actual CLI newbies, so by the time you're aware
of them you've done half the work already...

~~~
dghf
I have the opposite problem. When I'm writing stuff in MS Word at work, I'll
often see a ":w" appear in the text when I automatically try to save the file
Vim-style.

Cream[0] is a configuration for Vim that makes it work in a way more familiar
to people used to Word and other traditional Windows GUI programs.

[0] [http://cream.sourceforge.net/](http://cream.sourceforge.net/)

------
barbs
> inoremap jk <ESC>

Using two keystrokes instead of ESC to leave insert mode is definitely a
personal preference and not necessarily more efficient or comfortable.

~~~
0942v8653
Way more useful (for me anyway) is mapping Capslock to ESC. It works with any
vi anywhere as long as you are using your main machine to connect. Also with
inoremap don't you have to wait for the letter j to show up until you type
your next character? That would drive me crazy.

Edit now that I have actually looked at the article: Yes, you can map it to
Control but for me it's an awkward place to put a modifier. What if you want
to do Ctrl-A? Ctrl-C? At least the way I type, I'm better off using the one in
the corner.

~~~
bonquesha99
I have a similar remapping of Caps:

    
    
      * When I tap Caps, it's `<esc>`
      * When I hold Caps OR hit it in combination with any other key, it's `<ctrl>`
    

It really feels like the best of both worlds. I primarily develop on OSX, so I
use [https://pqrs.org/osx/karabiner/](https://pqrs.org/osx/karabiner/) to
handle the remapping.

~~~
tremendo
This works great on my Linux machine. On the Mac though I hate that all Mac
apps use Command for well, all command-key combinations instead of Ctrl, so I
map the Caps-lock to Command instead, but while Karabiner can do the dual "Esc
on tap plus other on hold" for Ctrl, Shift and Option keys, it cannot
frustratingly do it for the Command key. ITerm can switch caps-lock back to
Ctrl, but then I lose the Esc double function. It's maddening.

------
logn
From the article: "Arguably the most brilliant thing about vim is that as you
use it you begin to think in it."

Bill Joy, regarding vi: "It's like one of those pinatas--things that have
candy inside but has layer after layer of paper mache on top. It doesn't
really have a unified concept. I think if I were going to go back--I wouldn't
go back, but start over again."

Hunter S. Thompson, on an unrelated topic: "That was the fatal flaw [...] the
desperate assumption that somebody--or at least some force--is tending the
Light at the end of the tunnel."

I think vim is a useful tool. It's available on most systems and is powerful.
But trying to grok it and appreciate its UI isn't really something I want to
do.

------
voidpointer
While a tweaked configuration can be a great productivity boost, it has a
major drawback. My main reason for using vim is its ubiquitousness. Whenever I
need to ssh into a box to check something out, I can trust that it's there.
However, I cannot trust that its configuration has been tweaked like I expect.
Hence, I stay away from getting used to non-standard key mappings.

~~~
marklgr
Checking something out on some server does not require remapped keys or
commands: you generally just browse some file and fix a couple of things.
Tweaked configuration for more involved tasks is fine, to me.

------
uxcn
This is a really good introduction to vim. It is better to think of it as a
grammar to manipulate text.

A few additional things that have saved me keystrokes...

replace mode ( _R_ )

insert normal mode ( _< c-o>_)

paste from a register in insert mode ( _< c-r>reg_)

expression register ( _=_ )

clipboard and active selection registers ( _+_ , __* respectively)

command ranges (e.g. _10,+5s /foo/bar/g_)

miscellaneous commands ( _:global_ , _:argdo_ , _:normal_ , etc...)

------
andhess
I have my interns setup Vim on their first day, and it's usually their editor
of choice by the end of the summer. Apparently it gives them mad credit among
their peers at school too.

------
PeterWhittaker
Great tutorial. About 20 years ago, I was a Level 4 user (by the article's
definition), but that knowledge faded with disuse. When I switched back to a
Linux/UNIX machine about a decade ago, I started using vim again (well, I
started using _vi_ again, but it was vim), and remembered a lot of Level 1 and
2 stuff, and could recall being able to do more, but never had the motivation
to crawl through the man page to force myself to recall how I did more.

This article is so logically laid out I think anyone could get to at least
Level 3 with the article and a few hours diligent practice. Level 4 would then
beckon....

When I became a serious sed - and then ed - user, I became a much better
(faster, more productive) vi/vim user when I didn't need to be in visual mode
all the time.

------
zappo2938
Vim Adventures is so much fun. [http://vim-adventures.com/](http://vim-
adventures.com/)

------
melling
Which plugins do people find valuable? EasyMotion was recently recommended to
me:

[http://code.tutsplus.com/tutorials/vim-essential-plugin-
easy...](http://code.tutsplus.com/tutorials/vim-essential-plugin-easymotion--
net-19223)

~~~
thesteamboat
I like vim-surround[0] and vim-unimpaired[1].

[0]: [https://github.com/tpope/vim-surround](https://github.com/tpope/vim-
surround)

[1]: [https://github.com/tpope/vim-unimpaired](https://github.com/tpope/vim-
unimpaired)

------
bootload
from config: _" I remap it to Ctrl at an operating system level. This way my
left pinky can simply slide to the left by one key to execute Ctrl-whatever."_

Been a while since I used emacs, ctrl- _everything_ is an emacs idiom [0] and
a pinky killer. I guess the best bit is it's configurable. Really like this
tute Daniel, found a nice add for my .vimrc already.

[0]
[http://mally.stanford.edu/~sr/computing/emacs.html](http://mally.stanford.edu/~sr/computing/emacs.html)

------
enigmatic7
I agree that knowing basics of Vim is of great help and adds value to a
programmer's skill set.

I don't agree that it should be the only thing one uses for all
development/writing tasks.

Yes, vim is ubiquitous. However, no one uses vim without high levels of
customization. So i think there is a bigger dependency issue here.

Also, I've seen millennials struggling to cope with vim - it's like handing a
cassette to a person who's grown up with an iPod.

As long as the work gets done, an editor does not matter.

------
ridiculous_fish
Learning vim is easy, I've done it a thousand times! (As Mark Twain might say)

This time around I've been keeping a list of the stuff I miss from my usual
workflow. Here's two of the most maddening:

1\. Searching for literal text. Is there a way to avoid having to manually
escape special characters?

2\. Maintaining cursor position when scrolling. Maybe I want to go look at
another part of the file and come back. How can I scroll without it moving my
cursor?

As far as I can tell, I'm SOL on both counts.

~~~
fphilipe
1\. /\Vliteral search

2\. C-e and C-y as long as the cursor position doesn't move out of the visible
area

~~~
mdup
1\. For the mnemonic, "\v" turns on "very magic" where all possibly special
characters will have their special meaning without prefixing them with "\".
Now, "\V" is just the opposite of "very magic", which is "very not magic",
i.e. disable all special characters.

------
rajathagasthya
Thanks for this tutorial. I'm at Level 1 having switched from an IDE (PyCharm)
and I find this helpful. Any reason why you use Pathogen vs Vundle for
managing plugins? Does it even matter?

~~~
mikerichards
You're doing yourself a disservice. Intellij (PyCharm) has a vi plugin.
Whatever extra stuff you give up from regular Vim, you gain an order of
magnitude of productivity using a development environment that is "smart".
Think about what PyCharm does for you and what Vim doesn't.

Vi(m) is a model, not an implementation. I wish people would stop handicapping
themselves with very bad implementations. Intellij, Visual Studio (Resharper)
understands the languages your working in. Let the machine do what it does
best and aid you in the language while you concentrate on domain.

~~~
oneJob
In that spirit, I find it hilarious that the entire industry proudly releases
products weekly that do things like re-order laundry detergent at the press of
a button or games that require the player to do nothing more than swipe their
finger this way or that. On the other hand then there is this long raging
religious war over the tools used to build these products where some claim the
end all be all is this text only, esoteric, super high learning curve tool.
Sure it's superpowerful once mastered, but as in so many other areas, the
argument that you just made seems to carry the day: let the machine do the
work.

Also, Minority Report.

~~~
danso
Text-interfaces are the most direct and flexible way to get the machine to do
work. When you're stuck to a consumer-friendly GUI, you can only command the
computer the way the product has been designed to command the computer. You
learn how to use a product rather than how to teach the computer.

That said, of course I use a web browser for 99% of my Internet actions. But
being able to Alt-Tab into the terminal and run wget to quickly pull down a
list of files may feel like 1970s-era, but it's still a powerful technique
today, and for as long as users need to do things beyond what designers
intended them to.

~~~
oneJob
Respectfully, I propose you are limiting yourself when using left-to-right
text-interfaces. I think consumer-friendly may be a bit unfair as well. I'm
currently learning sign language and in parallel programming a Leap Motion
interface so as to bypass one-dimension (a line of symbols) and two-dimensions
(a gui on a plane) and more easily encapsulate concepts (words) by states of
motion in three dimensions.

Text-interfaces work well because they restrict input to precise parameters,
however as interfaces become "smarter" and are able to capture the meaning of
imprecise interfaces such as hand gestures and interpret them precisely
without requiring the user to deliver them with robotic precision, interfaces
will become more expressive, we will be able to work more naturally in the
domain in which we think, which is, these physical three dimensions.

If you've ever watched a mathematics professor write on the chalk board you'll
know what I mean. So expressive. So much conveyed with so few markings. And
yet, very sloppy. But still. Reading it off the board, those symbols just feel
so much better in your head than any words would.

------
melling
Is there a way to navigate words taking camel case into consideration?
helloWorld should be treated as two words when I want to delete or move, at
least sometimes when I want to edit.

~~~
nicholasferber
I am sure there is a 'normal' way to do it, but look into easy motion. I use
space in normal mode as a shortcut to go to any word in the line and that
includes camel case movement.

------
allendoerfer
Does work less well if you do not think in English.

~~~
bdcravens
Wouldn't the same apply to every programming language?

~~~
setpatchaddress
Kinda works in reverse for Ruby. I wish the Ruby maintainers had seen the need
for proper English language documentation.

------
bhagyas
This has been one of my favourite articles on Vim for beginners. I'd recommend
this to any beginner or a user of any other text editor.

Great post.

------
hackatroll
What is a better editor VIM or emacs? Why?

------
Animats
Vim is a regular expression system hooked to a 1970s user interface. If you
need to apply regular expressions to text files, vim is useful. I used vi to
write code for years, but that was decades ago. There's been some progress
since the 1970s.

~~~
scrollaway
That's just not what vim is... I don't even really know where to begin, you're
just flat out wrong. I'd say you're confusing vim with something else, but I
don't even know what else fits that description.

~~~
vezzy-fnord
He does have a point. Using vi derivatives does seem a tad spartan today, and
I've personally never been a fan of vi, nor of emacs, but much of the
community has this pseudo-elitist dichotomy set up around those two editor
families.

I'm not really a fan of what passes as IDEs, either, but I generally am not
attached to any particular editor and hop around them.

(The right answer to vi vs. emacs is, of course, Acme.)

~~~
mkozlows
There's nothing spartan about it. Vim has tons of plugins. Using it as a
Javascript editor, I've got syntax highlighting, git integration, linting,
code completion, and more.

~~~
wyclif
What is the JS plugin scene for vim like right now?

