Cameron Lane

Erstwhile educator. Python programmer.

Using Perl One-Liners from Vim

I frequently use LaTeX to create my math and computer science assessments.  However, I usually spend an inordinate amount of time trying to format the .tex file so that it is easier to read.  It is not the most efficient process.  So I began to research the best ways to process and format text files programmatically.  One such search led me to perl1line.txt.  As the name implies, this is a fantastic collection of one line Perl scripts that includes a section on text formatting (remove leading whitespace, remove/add blank lines, etc.).  Sounded like a good solution.  So I set out to quickly run some scripts from my favorite text editor, MacVim. As detailed as the explanations in perl1liners were, the most basic information was absent.  Some of the scripts take commandline arguments in the form of a file name, but many do not.  What then?   And what if I am working inside vim, how do I execute one of these one liners on a file I am editing?  

I found the answer here, buried in a comment.  In order to replace all instances of 'foo' with 'bar' in the current file, type the following in command mode.

% ! perl -pe 's/foo/bar/g'

I chose a search and replace example here, and it should be noted that the same thing can be accomplished with vim regex search and replace, but for editing certain types of files (e.g., .tex files that contain lots of characters that need to be escaped), using perl is somewhat easier.  Additionally, perl opens up a whole other world of text processing.  For more information on perl1liners, check out these links.

comments powered by Disqus