Reformatting X12 in Vim

I’ve been working with a lot of ANSI X12 files lately. Often, they come to me formatted either as a single line, or as multiple lines with line breaks at 80 columns. They’re much easier to work with when they’re formatted as multiple lines, but with line breaks at delimiters. So I added the following to my .vimrc:

When I get one of these files, I open it in Vim (well, Neovim), and I hit <leader>x12, and then everything is reformatted how I like it.

Explanation:

  • :%s/\n//g<cr> — Remove all newlines (puts 80-col formatted documents on one line)
  • :%s/\~/\~\r/g<cr> — Find all the line delimiters (~) and append a carriage return (now we have the X12 formatted how we want)
  • gg — Go to the top of the document
  • :nohlsearch<cr> — Remove the highlighting from the search results

I’m sure you can foil it, and it does nothing to make sure the document is indeed X12 before reformatting it. I find it useful, though.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.