Skip to content

More Vim Tips to Myself

  • by

Tip your server

Context

I’m doing more reports like the one I mentioned in the pandoc tips post. The final format is docx, but I prefer to write in vim where possible as i) I prefer it as a text editor and ii) doing so helps me stay sharp. So I work in a combination of LaTeX and (pandoc-flavoured) markdown.

Quick quotes: Prepend a string to multiple lines

I am inserting some quoted text, so wanted to prepend multiple lines with >. My first thought was to try visual line mode- highlight lines, hit ‘insert at start of line’ (shift-i) and enter the right angle bracket. For some reason that didn’t work.

This answer on SO by icktoofay shows I was within a gnat’s crotchet of the right approach. instead of using visual line (shift-v), I needed to use visual block instead:

  • ctrl-v (start visual block)
  • [movement keys] (select lines for prepending)
  • shift-i (insert at start of line)
  • > (insert literal right angle backet)
  • Esc (x2 to exit insert mode and apply across all lines in visual block selection)

I am not sure why this works only in visual block mode as opposed to visual line mode!

Quick emphasis: Wrap a variable number of words with a character (ie an asterisk or underscore)

In the quote blocks, some of the lines have emphasis (italics), and manually inserting the underscore or asterisk is tedious. This answer by Rich on the vi/vim stackexchange uses change and the small delete register, to wrap two words with brackets, but it’s easy to change 2w (2 words) to a different motion; in my case “up to a colon” – t:

The advantage to this method is that it can be repeated using . (period/dot). Quickly moving to the right part of the line — in my case, the second word, as the first is a > — to make it easy to repeat is as easy as ^w (move to start of line, then forward one word).

Repeat last substitution

Sometimes you want to substitute things on a single line (:s//), globally (:%s//), or on a visual selection. Other times, it’s easier to make a substitution, then go to where it’s needed and repeat it. However, substitutions don’t use the dot (.) repetition method.

Instead, they can be repeated a couple of different ways:

Make Vim Help Fullscreen

While reading up on these tips, I’ve been using :help <topic> to get help on various subjects- eg :help visual-block, :help change, etc. This opens help in a split- handy if you’re already in vim, needless if you have a new instance (eg another window in byobu/tmux/screen).

It’s easy to make it fullscreen: ctrl-w o (ie control+w, followed by o). Thanks craigp!

Bonus pandoc tip: markdown comments

I had to change a block of text, but wanted to leave the previous version in case that became useful — YAGNI be damned! — and there’s a useful answer from chl on SO:

I use standard HTML tags, like

<!---
your comment goes here
and here
-->

Note the triple dash. The advantage is that it works with pandoc when generating TeX or HTML output. More information is available on the pandoc-discuss group.

Neat!

Tell us what's on your mind