Vim Tricks

A collection of Vim tricks I found and are likely to forget because I do not use them enough.

  • Reload file that's edited somewhere else
    • :e!
  • Magically create a numbered list
    • Type "1" a X times
    • Visually select everything expect the first line
    • g to create a numbering
    • g for decreasing numbering
    • :help v_g_ctrl-a
    • Source: https://www.reddit.com/r/vim/comments/eh72g0/creating_a_list_of_numbers/
      • N.B. in normal mode, c-a and c-x increment and decrement a single number under the cursor (or directly right to it)
  • Automatically timestamp
    • iabbrev date- =strftime("%Y/%m/%d %H:%M:%S")
    • Werkt niet echt lekker.
    • Source: https://www.reddit.com/r/vim/comments/29ade4/journaling_in_vim/
    • Mijn eigen alternatief: :put! =strftime(\"%a %Y/%m/%d\")
  • Open file from netrw with "x"
    • Handig om snel iets in browser te openen, hoef ik het path niet meer te zoeken of met verkenner te werken!
  • Check for illegal bytes
    • Case: illegal quote sign copied from pdf prevented rendering to pdf with pandoc
    • Error was that \xbf was illegal
    • Solution: search for /[^\x00-\xFF]
  • Jump to end of previous word: ge for word and gE for WORD.
    • Before I did bbe
  • Use the changelist! gi and g; and g,
    • I actively use the jumplist for navigation (Ctrl-O, Ctrl-I). But when I'm programming I often look through several files and then want to continue where I was last actually editing text (as opposed to navigating text). Turns out that Vim sets a marker in your last edit location that you can jump to with gi.
    • g; to older positions in the list, g, to newer positions.
  • Repeat forward command f and F with ; (forward) and , (backward)
    • Navigation syntax is consistent with the semicolon and comma in g; and g,
    • I often tend to do a quick search with / and jump with n, but f is cleaner if you just want to jump to a word in the same sentence (highlighting with / is overkill in this case; and n probably jumps to another location in the document at some point which required you to jump back)
    • Example: in Python function names we_use_underscores_a_lot. e will skip to the end of the word because the underscore is not defined to be a word separator. You can of course adjust this behavior (but that is more complex and may introduce side effects elsewhere ), but here f
  • Reminder: more actively use fuzzy file finding for other things than files as well. E.g. also rely on CtrlPBufTag to find e.g. function definitions in opened files. (I mapped to sb for "search buffer"; st is mapped to CtrlPTag).
  • More control over pasting from register.
    • In insert and command mode, paste with Ctrl-R + register name. I always forget this.
    • In normal and visual mode, paste with "xp with x being the register identifier.
    • Very good explanation with cool use cases: https://stackoverflow.com/a/3997110
    • 0 is the yank register (when yanking in normal mode got to 0 and default register ")
    • 1-9: shifting deleting register, with 1 storing the most recent deletion (from c and d commands)
    • " default (unnamed) register
    • + and *: system clipboard
    • a-z for own use
    • :: stored last VimL command
    • /: stores search pattern register
      • I've often been unable to quickly get back a search pattern to refine it. Now I know why: it's in its own register!

TODO: Vim for writers.

  • ]s jump to next spelling error.
  • Adding words to spelling list.
  • Having spelling for multiple languages at once
  • Fixing spelling error on the fly
    • "inoremap u[s1z=`]au"
    • Source: https://castel.dev/post/lecture-notes-1/
  • Maybe a use for vimsessions for larger projects?
  • File navigation

Also see: