Just Another Tech Blog Filippos Karailanidis

The Power of VIM - a simple Use Case

Utilizing simple tools provided by the vim editor to format a list of emails and saving myself hour(s) of manual work
8 August, 2019

If you have absolutely no experience with vim, I’m going to write another post in the future exploring why learning vim is the best investment a developer can make, and how to go about it

This post we are going to look into a specific use case (that occured at work) where I had to format a big list of emails.

The task

We were provided with a Word file that looked like this:

The Big List of Emails


Department 1
mcnihil@outlook.com
rddesign@outlook.com
danneng@optonline.net
facet@hotmail.com
jramio@optonline.net
...

Department 2
valdez@me.com
johnh@msn.com
yamla@att.net
zavadsky@outlook.com
jaarnial@outlook.com
kodeman@icloud.com
sburke@msn.com
...

(obviously fake addresses)

We need to transform those email addresses into all capital letters, replacing special characters with underscores. The resulting strings would be used as contstants inside the software (don’t ask).

Why vim?

The file was about 70 lines long.

“Pffft, so what? Why bother using some archaic tool, I can type this in less than 30 minutes”

Sure, but what if it was 700 lines? Or 7000?

And would be absolutely sure you made no mistakes?

Well with vim you can, and you’ll be done in less than a minute

The Solution

Removing Extra Lines

As you noticed, there are some lines declaring Departments, some empty lines etc.

We want to get rid of all of those and leave the emails addresses, so:

:g!/@/d

This looks like gibberish, but let’s break it down

: Writing colon lets us enter vim’s command mode
:g Applies an operation to all lines that match a regex
:g! Applies an operation to all lines that DON’T match a regex
:g!/@/ The regular expression matches all lines that contain at least one ‘@’
:g!/@/d The operation to be applied to all those lines is ‘delete’

So with one command we got rid of those pesky lines leaving us with the good stuff.

mcnihil@outlook.com
rddesign@outlook.com
danneng@optonline.net
facet@hotmail.com
jramio@optonline.net
valdez@me.com
johnh@msn.com
yamla@att.net
zavadsky@outlook.com
jaarnial@outlook.com
kodeman@icloud.com
sburke@msn.com
...

Further reading: Delete all lines containing a pattern

Capitalization

We need all our addresses to be capital letters. This is even easier to accomplish with vim:

gggUG

No my keyboard did not get stuck, these are three separate vim commands.

This time we start in vim’s normal mode
gg Go to the start of the document
gU Capitalize until…
G Go to the end of the document

Here, gU is a command that requires a ‘motion’ to be defined. This is why we begin at the start of the document, and we capitalize until the end of it.

MCNIHIL@OUTLOOK.COM
RDDESIGN@OUTLOOK.COM
DANNENG@OPTONLINE.NET
FACET@HOTMAIL.COM
JRAMIO@OPTONLINE.NET
VALDEZ@ME.COM
JOHNH@MSN.COM
YAMLA@ATT.NET
ZAVADSKY@OUTLOOK.COM
JAARNIAL@OUTLOOK.COM
KODEMAN@ICLOUD.COM
SBURKE@MSN.COM
...

Further reading: Switching case of characters

Special Characters

For my last trick, we are going to make all special characters turn into undescores.

And to achieve that, we will use the magic of regex

:%s/[@\.]/_/g

: Entering Command mode
:s Apply substitute command on current line
:%s Apply substitute command on whole document
:%s/[@\.]/ Replace any @ or . (dot needs escaping because it’s a special character)
:%s/[@\.]/_/ Replace with _ the first match on each line
:%s/[@\.]/_/g Replace all matches on each line

The keen eyed amongst you have already noticed that this does not cover all cases.

What if there are other symbols like + in the address?

A more complete approach would be to use a regex that catches everything, except allowed characters

:%s/[^A-Z0-9]/_/g

This way, anything other than numbers and capital letters with be replaced with the underscore.

MCNIHIL_OUTLOOK_COM
RDDESIGN_OUTLOOK_COM
DANNENG_OPTONLINE_NET
FACET_HOTMAIL_COM
JRAMIO_OPTONLINE_NET
VALDEZ_ME_COM
JOHNH_MSN_COM
YAMLA_ATT_NET
ZAVADSKY_OUTLOOK_COM
JAARNIAL_OUTLOOK_COM
KODEMAN_ICLOUD_COM
SBURKE_MSN_COM
...

Further reading: Search and replace

Conclusion

I hope this was helpful to showcase in a simple manner how vim can be useful in your day-to-day tasks.

Obviously, I am not a wizard who lives alone in a cave and writes vim all day (except for Mondays). What I mean is that you do not need to remember a random sequence of obscure characters like gggUG

The next time you will need to do a boring task, just try googling how you could do that with vim, or maybe sed or awk

You might be surprised!