Leeren
Leeren

Reputation: 2081

What functionality does the "g?" command provide in vim

As a beginner programmer I've been practicing vim on vimgolf recently and saw that the command "g?" was used effectively to switch many lines of 'Ivm' to become 'Vim'. I understand that this shifts each alphabetical letter 13 times to the right but do not understand how this would prove useful except in unique circumstances like these.

Upvotes: 15

Views: 7353

Answers (3)

elig
elig

Reputation: 3058

It can prove useful on the case where you want to quickly hide some part of text that you typed in a visible vim buffer from onlookers.

For example some piece of password or token which you put in your code (but only do this temporarily, when you must).

Perhaps you want to invite a team-mate to look at some of your code, or you work in a place where people use to walk behind your back all the time so you can just rot13 the string and it is useless to them (at least in a glance). It probably works best against non technical passerby's or for short exposure period.

Keep in mind it does not rotate numbers and for security purposes it was even better if it could take a rotation size.

It can also become useful when you solve a CTF that has a rot13 challenge...

Upvotes: 0

romainl
romainl

Reputation: 196456

I have been using Vim since 4 years and learned about that command very early on but, even if I knew perfectly well what ROT13 was, I never found a use for g?.

Until a couple of weeks ago when I needed to add a bunch of <li> with unique IDs to a <ul> in an HTML prototype…

The starting point:

<ul>
    <li id="lorem">foo</li>
    <li id="ipsum">foo</li>
</ul>

After duplicating the two <li>:

<ul>
    <li id="lorem">foo</li>
    <li id="ipsum">foo</li>
    <li id="lorem">foo</li>
    <li id="ipsum">foo</li>
</ul>

After g?i" on the two new <li>'s ids:

<ul>
    <li id="lorem">foo</li>
    <li id="ipsum">foo</li>
    <li id="yberz">foo</li>
    <li id="vcfhz">foo</li>
</ul>

There! I found a practical use for g? in actual "programming"! Celebration!!!

Upvotes: 14

Keith Thompson
Keith Thompson

Reputation: 263177

The g? command (type :help g? for brief documentation) implements the rot13 algorithm, which rotates each letter 13 places forward or backward in the alphabet.

I'm not sure how commonly it's used today, but on Usenet it used to be a common way to encode spoilers. For example, if I'm writing a post that gives away the ending of something that not everyone has seen, I might use rot13 to weakly encrypt part of the article. It's enough to make it impossible to read accidentally (unless you've had a lot of practice), but easy to read if you're using a newsreader that has a built-in rot13 function -- as most of them do.

For example:

Pretend this is a spoiler. Filter with rot13 to read it.

would become

Cergraq guvf vf n fcbvyre. Svygre jvgu ebg13 gb ernq vg.

If I don't want to read the spoiler, I can ignore it. If I do want to read it, I can decrypt it easily enough.

Upvotes: 22

Related Questions