Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

vim

rot13

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.

like image 558
Leeren Avatar asked Aug 10 '14 01:08

Leeren


People also ask

What are the Vim commands that start with G?

- Stack Overflow What are the vim commands that start with g? g is a prefix to several commands. e.g. goto to move the cursor, but also gqip to format a paragraph. Where is the reference for all commands that are prefixed with g? They are exactly that, "commands that start with g".

What does%s/pattern/replace/G MEAN in Vim?

:%s/pattern/replace/g is common, the following is nearly equivalent: :g/./s/pattern/replace/g (less common, but basically the with "substitute" command). "$ means "last line in the file"." is interesting to me because vim normally uses $ to represent the end of the line and G to represent the end of the file.

How to address a line in VI or Vim?

The ":" mode (e.g. ex-mode) commands in vi or vim have this form: Address can be a single line address (ex-mode operates on "lines"), or a line range. For instance, a very simple command in "p" which will print the addressed line (s).

What is the difference between G and EOL in Vim?

Whenever it is used in combination with a motion it acts as itself: "delete between these parentheses", "delete from here to EOL", etc. On the other hand, g is nothing: not an operator, not a command, not a motion. It was just an empty slot that Vim's creator has been using for decades as a dumpster for new commands.


1 Answers

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!!!

like image 91
romainl Avatar answered Oct 30 '22 20:10

romainl