Search code examples
jsonxmlvim

Doubts with some Vim commands and macros (The missing semester of your CS Education, parse XML to JSON with Vim macros)


I'm trying this exercise from The missing semester of your CS Education, Lecture 3: Editors (Vim):

  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.

They give this solution:

  1. Gdd, ggdd delete first and last lines
  2. Macro to format a single element (register e)
    • Go to line with <name>
    • qe^r"f>s": "<ESC>f<C"<ESC>q
  3. Macro to format a person (register p)
    • Go to line with <person>
    • qpS{<ESC>j@eA,<ESC>j@ejS},<ESC>q
  4. Macro to format a person and go to the next person (register q)
    • Go to line with <person>
    • qq@pjq
  5. Execute macro until end of file
    • 999@q
  6. Manually remove last , and add [ and ] delimiters

I don't understand commands like:

  • <C
  • ^r"f>s":
  • S{<ESC>j@eA,<ESC>j@ejS}

Among others, could someone do a breakdown of the solution in order to understand the commands?


Solution

    • <C

      This one is meaningless because you split the command at the wrong place. The correct place is between f< and C":

      1. f< means "move the cursor to the next < on the line", see :help f.
      2. C" means "cut the rest of the line and insert a "", see :help C.
    • ^r"f>s":

      1. ^ means "move the cursor to the first printable character of the line", see :help ^.
      2. r" means "replace the current character with a "", see :help r.
      3. f> means "move the cursor to the next > on the fine", see :help f.
      4. s": is actually s": ", meaning "replace the current character with ": ", see :help s.
    • S{<ESC>j@eA,<ESC>j@ejS}

      1. S{ means "replace the whole line with a " at the correct indentation", see :help S.
      2. <ESC> is the Escape key, see :help key-notation.
      3. j means "go down one line", see :help j.
      4. @e means "play back macro stored in register e", see :help @.
      5. A, means "append a , at the end of the line", see :help A.
      6. <ESC>, j, @e, j, and S} have already been explained.

    All of that is pretty easy to piece out after a while (I rarely need to use Vim itself when answering Vim questions) but it sure is a lot to take in without the proper foundations.

    That cryptic example is really a case of giving the reader a quasi-magical fish instead of teaching them how to fish. I am not sure what the people behind those "get rich quick" Vim articles and courses think they are achieving but questions like this one should make them pause a little.

    The subject matter is much too complex to be covered in one lecture or in a short webpage so I would suggest you learn Vim properly instead of consuming context-free content like this.

    1. If you haven't already, do $ vimtutor as many times as needed to get the basics right.
    2. As instructed at the end of vimtutor, level up to the user manual :help user-manual. It's a hands-on tutorial that will guide you progressively through every feature, from basic to advanced. Go at your own pace and, most importantly, experiment along the way.
    3. Keep an eye on anti-patterns and inefficient actions, find improvements, practice. Rinse. Repeat.