Skip to content
Christopher P. Brown edited this page Jul 19, 2020 · 5 revisions

So you wanna vim!

one of us! one of us! we accept you! we accept you!

Prerequisites

Movement

  • hjkl actually isn't any better than arrow keys. If you find yourself holding down l, stop and think about what you're doing. Use f or t, :s/<fragment>, or text objects to jump around. (w, ), }, etc. :help objects for more.)

  • I prefer :set relativenumber number for line numbering. That way I can always see what line number I'm on, and can see how far away lines are so I can 11j, for example, to jump 11 lines down. And so I can tell at a glance that I want to 5dd without having to stop to count.

  • You can move lines with :m. If you want to move the current line 7 lines up, instead of dd7kp, you can :m-7. If you want Atom style moving lines up and down, remap something like Alt-k or the up arrow to :m-1, and Alt-j or the down arrow to :m+1.

"basics"

Concepts that I consider to make vim great

  1. registers

  2. macros

  3. marks

  4. dot/repeat

useful commands

Stuff I use a lot

  1. :help - Just read it.

  2. All the basic movement commands like f and F, t and T, and : and ; to repeat forward and backward those "find"s and "to"s

  3. ^[ sends escape. very nice if you've mapped caps lock to CTRL

  4. * Find word under cursor

  5. gx Open URL under cursor. (gf: open file path under cursor.)

  6. % Find matching bracket

  7. >> indent (<< un-indent)

  8. ^z suspend. drop into the shell. (if you're in the terminal.) Do your stuff, then fg to bring vim back to the "foreground".

  9. H, M, L jump to top, middle, bottom of screen. ("High", "Medium", "Low")

  10. zz move current line to middle of screen. (see also zt and zb)

Weird stuff

  1. g?<dir> - rot13. g?? a whole line

Plugins

For the longest time, I didn't use any plugins. Now I do. 🤷‍♀️

Check out https://vimawesome.com/

A couple of faves:

  • NERDTree - file explorer

  • fugitive and gitgutter - get some git in your life

  • vim-surround - easily change/add/delete/etc surrounding characters

  • emmet-vim - HTML expander

  • vim-airline - aesthics

suggestions

  1. keep your vimrc in a git repo. so you can pull it down where ever you are. snoop people's vimrcs on github. Here's mine: https://github.com/chrisman/dotfiles/blob/master/vimrc

Appendix A: Links and resources

Appendix B: FAQ

Q. What kind of vim to use?

A. Use vim or neovim. (I use neovim.) Avoid using a GUI like gvim or macvim. One of the glorious things about vim in the terminal is having quick access to the command line and to UNIX utils. And being able to quickly suspend (c-z) and drop into a shell or into a REPL.

Appendix C: Macros I use often enough to have written them down

fix "smarty pants" formatting

:%s/’/'/e | :%s/“/"/e | :%s/”/"/e | :%s/…/.../e

swap artist - title order

:s,\(.*\) - \(.*\),\2 - \1,

search and replace in all buffers

:bufdo %s/old/new/e | update

Other neat stuff

Sometimes I want to make a table of contents including all the 2nd level headers in a markdown file:

:g/^## /t2

This copies all lines starting with ## to line 2.

Here's all the headers (at the moment) in this file:

  1. Other neat stuff
  2. Appendix C: Macros I use often enough to have written them down
  3. Appendix B: FAQ
  4. Appendix A: Links and resources
  5. suggestions
  6. Plugins
  7. useful commands
  8. "basics"
  9. Movement
  10. Prerequisites

Oh but look, they're in reverse order 🤔

Okay well you can do the same thing by yanking to a register: qaq:g/^## /y A

(qaq is a little hack to quickly clear the a register since we're going to be appending to it.)

  1. Prerequisites
  2. Movement
  3. "basics"
  4. useful commands
  5. Plugins
  6. suggestions
  7. Appendix A: Links and resources
  8. Appendix B: FAQ
  9. Appendix C: Macros I use often enough to have written them down
  10. Other neat stuff

You can turn the ##s into a numbered list using column editing.

On the first pound, <c-v>9jr0 to turn them all into zeros. Then <c-v>9jg<c-a>a to auto increment them all. Then on the second pound, <c-v>9jr. to turn them all into periods.

that is: ^V9jr0l^V9jr.h^V9jg^A

Clone this wiki locally