Unleashing the Power of vim-commentary Plug-in

Cover.jpeg
Cover.jpeg

Introduction:

The in­cred­i­ble vim-com­men­tary plug-in de­vel­oped by tpope.

As some­one who does­n't use vim fre­quently, I of­ten for­get to take ad­van­tage of its fea­tures. This post serves as a handy memo to re­mind my­self (and oth­ers) about the use­ful­ness of this plug-in.


The Basic Concept of Modes in Vim:

Be­fore we delve into the prac­ti­cal us­age of the vim-com­men­tary plug-in, it's es­sen­tial to grasp the con­cept of modes in Vim. Vim op­er­ates in three pri­mary modes that de­ter­mine how we in­ter­act with the text:

ModeDescription
NormalDefault; for navigation and delete or duplicate, etc.
InsertFor edit text
Command LineFor operations like saving, exiting, or work with plug-in, etc.

Use the vim-commentary Plug-in:

The dif­fer­ent ways to uti­lize the vim-com­men­tary plug-in in each mode.

Normal Mode:

  1. gcc:

    In nor­mal mode, en­ter­ing "gcc" com­ments or un­com­ments a sin­gle line. Re­peat­ing the com­mand will tog­gle the com­ment sta­tus.

2023-12-03_121111.png
2023-12-03_121111.png

com­ments

2023-12-03_121121.png
2023-12-03_121121.png

un­com­ments

The vim-com­men­tary plug-in also al­lows us to com­ment mul­ti­ple lines at once.

By us­ing the com­mand "gc" fol­lowed by a digit number and the up or down ar­row keys (j/k), we can ef­fort­lessly com­ment or un­com­ment as many lines as we want.

e.g. `gc`+`3`+`j`

2023-12-03_160908.png
2023-12-03_160908.png

com­ments

2023-12-03_160916.png
2023-12-03_160916.png

un­com­ments

  1. gc + j/k:

    Us­ing "gc" fol­lowed by the up or down ar­row keys (j or k) al­lows us to com­ment or un­com­ment two lines si­mul­ta­ne­ously, with the in­di­ca­tor ad­just­ing ac­cord­ingly.

2023-12-03_121304.png
2023-12-03_121304.png

com­ments

2023-12-03_121317.png
2023-12-03_121317.png

un­com­ments

  1. gcap:

    Typ­ing "gcap" in nor­mal mode com­ments or un­com­ments an en­tire para­graph or block of text ef­fi­ciently.

2023-12-03_121703.png
2023-12-03_121703.png

com­ments

2023-12-03_121710.png
2023-12-03_121710.png

un­com­ments

Command Line Mode:

  1. :107,113Commentary:

    This com­mand lets us com­ment or un­com­ment lines 107 to 113. Ex­e­cut­ing the com­mand again will undo the ac­tion.

2023-12-03_121851.png
2023-12-03_121851.png

com­ments

2023-12-03_121942.png
2023-12-03_121942.png

un­com­ments

  1. :g/alias /Commentary:

    By us­ing this com­mand, we can com­ment or un­com­ment lines that match a spe­cific pat­tern, such as "alias " Re­peat­ing the com­mand will re­vert the ac­tion.

2023-12-03_122054.png
2023-12-03_122054.png

com­ments

2023-12-03_122102.png
2023-12-03_122102.png

un­com­ments

Visual Mode:

The other modes, Vim also of­fers a Vi­sual Mode. In this mode, we can se­lect lines and use the "gc" com­mand to com­ment or un­com­ment them ef­fort­lessly.

2023-12-03_122226.png
2023-12-03_122226.png

com­ments

2023-12-03_122242.png
2023-12-03_122242.png

un­com­ments


Conclusion:

The vim-com­men­tary plug-in sig­nif­i­cantly en­hances text edit­ing ef­fi­ciency.

By fa­mil­iar­iz­ing our­selves with its ca­pa­bil­i­ties, we gain the abil­ity to seam­lessly com­ment on or un­com­ment lines, para­graphs, and even spe­cific pat­terns of text.


Reference:


Related: