I just started diving into vi and so far learned only basic moving around/editing commands. While I am going through the book, is there a fast way to comment out a paragraph with -#
in the same column with the cursor position (indenting the lines accordingly)?
Let's say I have a piece of code:
%table
- unless paginate(@clients).nil?
%tr
%th
=t('index.name')
%th
=t('index.address')
%th
=t('index.phone')
=render :partial => 'client', :collection => @clients
and I want to comment out lines between - unless
and =render :partial
with -#
in one column and then be able to comment them in again. What command would that be?
In blockwise select mode, you can press I
to insert in front of the block and A
to insert after the block.
Setting 'relativenumber'
(:set rnu
) could help to count lines.
Start with CTRL-V to switch to blockwise select mode, then 8j
to go down eight lines, then I#
Esc to insert the #
.
To remove it: d
CTRL-V8j will delete blockwise.
Warning, if you happen to use vanilla gvim.exe on Windows, you probably have mswin.vim
activated which remaps CTRL-V, use then CTRL-Q instead (or disable this plugin)
If you're less interested about the how and just want it to work, there are a number of plugins that provide (un)commenting functionality for varieties of languages. Tim Pope's commentary.vim is the one I just started using recently, as a replacement for nerdcommenter.
I just installed it so I can't speak to any defects, but Tim Pope's stuff is (nearly?) always excellent. With the plugin you could comment a Haml paragraph by selecting a visual block and typing \\\
. It also takes motions, e.g. \\ap
.
The link:
https://github.com/tpope/vim-commentary
If you use it often, you can define a command in your .vimrc
command -range=% C :<line1>,<line2>s/^/-#/
Then in vi, you can apply :<range>C
in the usual manner. You can do this with :10,20C
or .,+10C
. You can use the following command for uncommenting.
command -range=% D :<line1>,<line2>s/^-#//
Since I am using vi for with languages with different types of commenting, I also us these commands:
command -range=% -nargs=1 Ca :<line1>,<line2>s/^/<args>/
command -range=% -nargs=1 Da :<line1>,<line2>s/^<args>//
Allowing you to just do :10,20Ca-#
, where you can replace -#
with the commenting method of choice.