Exploring Vim & BASH advanced features

In this blog post I illustrate how I have been pushing my knowledge in Vim and BASH by using some of their most powerful features.

tl;dr:

  • Groups can be captured in Vim using the syntax \(<example group>\) which are useful when modifying, adding, and changing text around a pattern that is meant to stay in tact (group).
  • Xargs is an essential and probably one of the most important BASH commands. It allows for powerful piping, and combining commands, which when used together become sophisticated and powerful.

Vim Capture Groups

One of the most useful features in Vim is capture groups. I will be honest, I have known about capture groups for a while but I never pushed myself to learn them, that is why for this weeks inquiry I wanted to get a grasp of this feature. To illustrate what capture groups are, I will explain different use cases for them, and give a visual example of them at work.

Common use cases

Adding text around a group to change how it functions within a program.
Changing the structure of multiple lines.
Swapping, or re-ordering multiple groups.

This video illustrates some of the examples that I have already encountered in my date-to-day work, illustrated through examples. (ignore the last 1 minute of the video, there was a glitch when combining the two videos)

BASH – ls, and xargs commands

The Issue

This term I have been using a lot of BASH commands when accessing and organizing information for my various classes. To illustrate this, consider an example: It is common for me, and I assume other students to be searching for information that is contained in a specific pdf file but unsure which pdf that information is contained in. There are probably many different approaches to this problem, however the way that I usually solve this is combining all of the pdf files into one then searching for keywords.

A Rudimentary Solution

The way that I would traditionally accomplish this would be with a command like pdfunite *.pdf all.pdf however this leads to an issue where the files are out of order, this solution somewhat solves the issue but it could still be better and more robust.

A Good Solution

It would be nice to be able to sort the pdf’s by an identifier, or metadata like the time/date the files were downloaded, or some other parameter. This is not immediately obvious how to accomplish, however incidentally I came across the xargs command (“build and execute command lines from standard input”). Which allows this kind of sorting to be done before passing the file names to a program. In the improved command we can first sort all of the pdf’s in that folder from oldest to newest which would be the order that the pdf’s were released from the prof, then pass that sorted list of files to pdfunite and replace \n to \0 in case the file contains spaces. -t means sort by time -r means reverse the ordering.

ls -tr *.pdf | tr '\n' '\0' | xargs -0 pdfunite all.pdf

Another use I have found is for music being in order from an unsorted album, each song is its own file. We can apply a similar command to ensure that we play the album in the correct order. The -c flag means sort by time the file was modified. Therefore this will sort the files by time they were downloaded.

ls -trc *.mp4 | tr '\n' '\0' | xargs -0 mpv

If you have any bash commands that you find useful I would love to know, thanks for stopping by!

“Write programs that do one thing and do it well.”
“Write programs to work together.”
“Write programs to handle text streams, because that is a universal interface”

https://en.wikipedia.org/wiki/Unix_philosophy