User:Ta bu shi da yu/Useful vim commands

I found that I wanted to add a w: to wikilinks to get articles ready for transwikification. This took too long. I also happen to like vim. Here's what I came up with to automate this task.

If none of this makes any sense, it's actually not that hard. I'm just using vim's search and replace facility coupled with inbuilt regular expressions. If you don't want to be bothered with the nitti-gritty, just copy and paste the global search and replace lines directly into vim using the mouse buffer. Simple!

Ta bu shi da yu 04:23, 31 Oct 2004 (UTC)

Using global search and replace
AlanBarrett pointed out that there is, in fact, a much simpler way of doing this:

Non-piped wikilinks
In vim: /\[\]\{-}\)\]\]/[[w:\1|\1 qa  /\[\]\{-}\)\]\]/[[w:\1|\1  q  100@a
 * to find all wikilinks that aren't piped, use:
 * to do a search and replace for all wikilinks, on a single line, that aren't piped, to prepare for convertion to Wikisource, etc, use
 * to record and use a macro that does this for all lines, use:
 * q is not a vi command (it's not in nvi). It's probably a vim command, and I assume that   means "place '...' into buffer a".  In vi, to place two lines of stuff into buffer a, you have to first put those lines into a file, and then cut them into buffer a with  .  &mdash;AlanBarrett 09:28, 31 Oct 2004 (UTC)
 * Yep, Alan is correct about the fact I'm not using vi. I'm using vim (never used vi before!). The q command starts recording a macro however. - Ta bu shi da yu 12:15, 31 Oct 2004 (UTC)
 * To do a search and replace on every line that matches a regular expression, use .  For example,  .  There's no need for macros when :g will do it all in a single command.  &mdash;AlanBarrett 09:37, 31 Oct 2004 (UTC)
 * Ah! so that's how to use the global command. - Ta bu shi da yu 12:15, 31 Oct 2004 (UTC)

Piped wikilinks
In vi: /\[\[.\{-}|.\{-}\]\] /\[\[[^w][^:].\{-}|.\{-}\]\]  :s/\[\[[^w][^:]\(.\{-}\)|\(.\{-}\]\]\)/[[w:\1|\2  qa  /\[\[[^w][^:].\{-}|.\{-}\]\]  :s/\[\[[^w][^:]\(.\{-}\)|\(.\{-}\]\]\)/[[w:\1|\2  q  100@a
 * to find all wikilinks that are piped, use:
 * to find all wikilinks that are piped, but haven't had a w: appended to the start, use:
 * to do a search and replace for all wikilinks, on a single line, that are piped (to prepare for conversion to Wikisource, etc), use:
 * to record and use a macro that does this for all lines, use:

Combine them together and you get an ugly hack method
Ugh. Global search and replace is much nicer! But this does demonstrate how to use macros in vim. qa /\[\]\{-}\)\]\]/[[w:\1|\1  q  100@a  qb  /\[\[[^w][^:].\{-}|.\{-}\]\]  :s/\[\[[^w][^:]\(.\{-}\)|\(.\{-}\]\]\)/[[w:\1|\2  q  100@b