Editors (Vim)

Editors (Vim) #

Lecture source: https://missing.csail.mit.edu/2020/editors/

Movements, edits, counts, modifiers.

Exercises #

  1. (Advanced) Convert XML to JSON (example file) using Vim macros. Try to do this on your own, but you can look at the macros section above if you get stuck.

    • Gdd, ggdd delete first and last lines

    • Macro to format a single element (register e)

      • Go to line with <name>
      • qe^r"f>s": "<ESC>f<C"<ESC>q
        • qe: start recording a macro in register e.
        • ^: go to first non-blank character (that is <).
        • r": replace it as ".
        • f>: find forward > on the current line.
        • s": "<ESC>: substitute the >, replace it as ": ", then leave the insert mode.
        • f<: find forward < on the current line.
        • C"<ESC>: delete from cursor to the end of the line, add " at the end, then leave the insert mode.
        • q: stop recording macro.
    • Macro to format a person (register p)

      • Go to line with <person>
      • qpS{<ESC>j@eA,<ESC>j@ejS},<ESC>q
        • qp: start recording a macro in register p.
        • S{<ESC>: delete line and insert {, then leave the insert mode.
        • j: move one line down.
        • @e: replays the e macro (for name property).
        • A,<ESC>:
        • j: move one line down.
        • @e: replays the e macro (for email property).
        • j: move one line down.
        • S},<ESC>: delete line and insert },, then leave the insert mode.
        • q: stop recording macro.
    • Macro to format a person and go to the next person (register q)

      • Go to line with <person>
      • qq@pjq
        • qq: start recording a macro in register q.
        • @p: replays the p macro (to format a person).
        • j: move one line down.
        • q: stop recording macro.
    • Execute macro until end of file

      • 999@q
    • Manually remove last , and add [ and ] delimiters