DEV Community


Vim practical example

delbetu profile image M Bellucci ・2 min read

The problem

In my job, I had to open in a browser a set of similar URLs.
A sample URL<element-name>.<lang>.<locale>

In this concrete case, I had to access 6 elements and each one in 6 different languages.
36 different URLs.


Approach 1
Open the browser and type every URL
This would be reaaaaaaally boring.

Approach 2
Use Vim

Open vim
Write the sample URL
Then replicate this URL 5 times.


Now we have one line per element.
alt 1

Write every element name

:1s/<element-name>/element1/ # scoped to line 1 replace <element-name> with element1
:<up_arrow> # show previous executed command

Repeat this 4 more times
alt 2

Replicate this block 5 times once per lang-locale

ggy6jG # go to top, yank 6 lines down, go to bottom
qq # start recording in register q
o<esc> # insert blank line
p # paste yanked block
G # go to bottom
q # stop recording

At this point, we have recorded insert a new line and paste yanked block.
Let's repeat this 5 times.

5@q # execute register q 5 times

alt 3

gg # Go to the top

For each block replace first lang-locale with
Create a macro for replacing one line and go to next line to replace.

qq # start recording
:.s/<lang>.<locale>/ # scoped to current line replace <lang>.<locale> with
7jq # go 7 lines down and stop recording
5@q # repeat this macro 5 more times once per each block.

At this point you should have something like this:
alt 4

Go to the second line and repeat the process with

2gg # go to second line

Repeate the process with pt.BR, es.MX, and

At this point, you should have something like
alt 5

Remove every empty line.

:g/^$/d # apply d command to lines matching /^$/

We have built every possible URL.
Now we need to open them into default browser.

:%! xargs open # pass all lines as the argument to open command


The default browser will open all those URLs in different tabs.
(open command is present on mac-os. For Linux you should have a similar)

I hope you've enjoyed.
All comments are welcome!


Editor guide
vlasales profile image
Vlastimil Pospichal
Bash$ xargs open{1,2,3,4,5,6}.{,,pt.BR,es.MX,,}
delbetu profile image
M Bellucci Author

Oh, this is awesome
That would solve the problem faster!

jerpint profile image

Why not use a simple python script for this?

Seems like the second someone decides to add/remove a country or number of elements, you have to start all over again. Not saying this isn't cool, but seems overkill.

For example:

langs = ['en','de',...]
elems = [1,2,...]

for elem in elems:
    for lang in langs:
        print(base_url + '/element' + str(elem) + '/' + lang)
indrora profile image
Morgan Gangwere 🔹

A cleaner way:

import itertools


urls = map( lambda k: "{0}#lang/{1}".format(*k),
   itertools.product(nums,langs) )
delbetu profile image
M Bellucci Author

The goal is practice new vim commands, the problem is not important in the context of this post.

chrboe profile image
Christoph Böhmwalder

For the record, on most Linux distributions the tool to use is xdg-open

moopet profile image
Ben Sinclair

In your example, you end up with the same five URLs repeated. E.g. line offset 40 is identical to line offset 33 in your last Vim screenshot. I'm not sure if this means you left off the final step?

Adding a blank line and then removing all the blank lines makes sense for understanding what you're doing (I do this sort of thing all the time) but for the purpose of this demo it's just extra work. I think you could improve the post by explaining that it's just done to make the screen of text easier to follow.

I know your post is demonstrating a bunch of different techniques, but you did ask for comments, so I'm going off on an efficiency jaunt.

The step of replicating the block could be reduced to simply adding a blank line at the end and then copy-pasting the whole thing, rather than using another macro recording:

Go<esc> # add a blank line at the end of the file
Ygg # copy from the end to the top
5P # paste the block (including its empty line) 5 times

You can also use 2G to go to the second line instead of 2gg if you want fewer keypresses but are ok with chords. Likewise Y5 instead of yy5.

ronalterde profile image
Steffen Ronalter

Nice use case!

I‘m just wondering whether we could utilize C-a for increasing numbers from within insert mode.

By the way, is there a typo in the numbering?

delbetu profile image
M Bellucci Author

Yes could be used for increasing numbers
And yes there is a typo on numbering, good catch!

hoffmann profile image
Peter Hoffmann

Where is this function in sublime?

delbetu profile image
M Bellucci Author

Lol I bet that sublime also has a fast way of doing this same job.

One thing that I noticed with vim is that you never stop learning so your editing skills are leveling up all the time, which makes you think different while programming, making it more enjoyable.