Search code examples
vim

Any ideas on how to format these lines in vim?


I have the following lines that I've already formatted quite a bit, but I can't seem to figure out how to format these jagged lines so they are all in the correct position. I've been trying using macros and also using /s but it just seems the tabs or spaces after the word is a bit different for each. What might be a good way to format this here?

enter image description here


Solution

  • You can select the entire part you want to format and then use the normal command

    '<,'>s/\([^: ]\+\)[ ]*:[ ]*\([^: ]*\)/\=printf('%-8s%8s%s%8s%8s',submatch(1),' ',':',' ',submatch(2))/g
    

    which will pad the result evenly around the :.

    You can select in vim using v then enter normal mode with esc followed by entering command mode with :. Paste the above line in command mode once the selection is made and hit enter.