|
| 1 | +--- |
| 2 | +title: "Unleashing the Power of vim-commentary Plug-in" |
| 3 | +date: 2023-12-03T16:32:26+08:00 |
| 4 | +draft: false |
| 5 | +author: "King Tam" |
| 6 | +summary: "The incredible vim-commentary plug-in developed by [tpope]" |
| 7 | +showToc: true |
| 8 | +categories: |
| 9 | +- Linux |
| 10 | +tags: |
| 11 | +- vim-commentary |
| 12 | +- Plug-in |
| 13 | +- vim |
| 14 | +ShowLastMod: true |
| 15 | +cover: |
| 16 | + image: "img/vim-commentary/Cover.jpeg" |
| 17 | +--- |
| 18 | + |
| 19 | +### Introduction: |
| 20 | + |
| 21 | +The incredible vim-commentary plug-in developed by [tpope](https://github.com/tpope). |
| 22 | + |
| 23 | +As someone who doesn't use vim frequently, I often forget to take advantage of its features. This post serves as a handy memo to remind myself (and others) about the usefulness of this plug-in. |
| 24 | + |
| 25 | +--- |
| 26 | + |
| 27 | +### The Basic Concept of Modes in Vim: |
| 28 | + |
| 29 | +Before we delve into the practical usage of the vim-commentary plug-in, it's essential to grasp the concept of modes in Vim. Vim operates in three primary modes that determine how we interact with the text: |
| 30 | + |
| 31 | +| Mode | Description | |
| 32 | +| ------------ | ------------------------------------------------------------ | |
| 33 | +| Normal | Default; for navigation and delete or duplicate, etc. | |
| 34 | +| Insert | For edit text | |
| 35 | +| Command Line | For operations like saving, exiting, or work with plug-in, etc. | |
| 36 | + |
| 37 | +--- |
| 38 | + |
| 39 | +### Use the vim-commentary Plug-in: |
| 40 | + |
| 41 | +> The different ways to utilize the vim-commentary plug-in in each mode. |
| 42 | +
|
| 43 | +### Normal Mode: |
| 44 | + |
| 45 | +1. #### `gcc`: |
| 46 | + |
| 47 | + In normal mode, entering "`gcc`" comments or uncomments a single line. Repeating the command will toggle the comment status. |
| 48 | + |
| 49 | +  |
| 50 | + |
| 51 | + comments |
| 52 | + |
| 53 | +  |
| 54 | + |
| 55 | + uncomments |
| 56 | + |
| 57 | + |
| 58 | + |
| 59 | + The vim-commentary plug-in also allows us to comment multiple lines at once. |
| 60 | + |
| 61 | + By using the command "`gc`" followed by a `digit number` and the up or down arrow keys (`j`/`k`), we can effortlessly comment or uncomment as many lines as we want. |
| 62 | + |
| 63 | + e.g. `gc`+`3`+`j` |
| 64 | + |
| 65 | +  |
| 66 | + |
| 67 | + comments |
| 68 | + |
| 69 | +  |
| 70 | + |
| 71 | + uncomments |
| 72 | + |
| 73 | +2. #### `gc` + `j`/`k`: |
| 74 | + |
| 75 | + Using "`gc`" followed by the up or down arrow keys (`j` or `k`) allows us to comment or uncomment two lines simultaneously, with the indicator adjusting accordingly. |
| 76 | + |
| 77 | +  |
| 78 | + |
| 79 | + comments |
| 80 | + |
| 81 | +  |
| 82 | + |
| 83 | + uncomments |
| 84 | + |
| 85 | +3. #### `gcap`: |
| 86 | + |
| 87 | + Typing "`gcap`" in normal mode comments or uncomments an entire paragraph or block of text efficiently. |
| 88 | + |
| 89 | +  |
| 90 | + |
| 91 | + comments |
| 92 | + |
| 93 | +  |
| 94 | + |
| 95 | + uncomments |
| 96 | + |
| 97 | +### Command Line Mode: |
| 98 | + |
| 99 | +1. #### `:107,113Commentary`: |
| 100 | + |
| 101 | + This command lets us comment or uncomment lines 107 to 113. Executing the command again will undo the action. |
| 102 | + |
| 103 | +  |
| 104 | + |
| 105 | + comments |
| 106 | + |
| 107 | +  |
| 108 | + |
| 109 | + uncomments |
| 110 | + |
| 111 | +2. #### `:g/alias /Commentary`: |
| 112 | + |
| 113 | + By using this command, we can comment or uncomment lines that match a specific pattern, such as "**alias** " Repeating the command will revert the action. |
| 114 | + |
| 115 | +  |
| 116 | + |
| 117 | + comments |
| 118 | + |
| 119 | +  |
| 120 | + |
| 121 | + uncomments |
| 122 | + |
| 123 | +### Visual Mode: |
| 124 | + |
| 125 | +The other modes, Vim also offers a Visual Mode. In this mode, we can select lines and use the "`gc`" command to comment or uncomment them effortlessly. |
| 126 | + |
| 127 | + |
| 128 | + |
| 129 | +comments |
| 130 | + |
| 131 | + |
| 132 | + |
| 133 | +uncomments |
| 134 | + |
| 135 | +--- |
| 136 | + |
| 137 | +### Conclusion: |
| 138 | + |
| 139 | +The vim-commentary plug-in significantly enhances text editing efficiency. |
| 140 | + |
| 141 | +By familiarizing ourselves with its capabilities, we gain the ability to seamlessly comment on or uncomment lines, paragraphs, and even specific patterns of text. |
| 142 | + |
| 143 | +--- |
| 144 | + |
| 145 | +### Reference: |
| 146 | + |
| 147 | +- https://github.com/tpope/vim-commentary |
| 148 | + |
| 149 | +--- |
| 150 | + |
| 151 | +### Related: |
| 152 | + |
| 153 | +- [Linux 的命令基本用法 - vim 編輯器](https://kingtam.win/archives/vim.html) |
| 154 | + |
| 155 | +- [Install VIM-PLUG on Synology DSM](https://kingtam.eu.org/posts/dsm-custom/#install-vim-plug-on-synology-dsm) |
| 156 | +- [NeoVim and Vim plugins Install in Alpine Linux](https://kingtam.eu.org/posts/alpine-customization/#neovim-and-vim-plugins-install-in-alpine-linux) |
0 commit comments