Tuesday, February 7, 2012

Re: commenting out/in a paragraph (haml)

On Feb 7, 5:51 am, Алексей Данченков <adanchen...@gmail.com> wrote:
> Hi!
>
> I just started diving into vi (and just subscribed to the mailing list) and
> so far learned a basic moving around/editing commands. While I am going
> through the basic 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 9 lines (or a paragraph?) between - unless
> and =render
> :partial with -# in one column like that...
>
> %table
>
>   -# - unless paginate(@clients).nil?
>   -#   %tr
>   -#     %th
>   -#       =t('index.name')
>   -#     %th
>   -#       =t('index.address')
>   -#     %th
>   -#       =t('index.phone')
>
>   -#   =render :partial => 'client', :collection => @clients
>
> ...and then be able to comment them in again. What command would that be?
>

I believe there's some commenting plugins out there, but I've never
used them, so I cannot speak much on them.

The easiest way to insert the same text in the same position in a
range of lines, is to use blockwise-visual mode (often called "column
edit" or "column select" in other editors). To enter blockwise-visual
mode, press CTRL-V in normal mode. If CTRL-V pastes for you (or does
some other action instead of blockwise visual mode), you can instead
use CTRL-Q.

Once in blockwise visual mode, move the cursor to the last line you
want to insert text on. Depending on how your code is formatted, you
might be able to use } to hit the next blank line, use ap to select a
"paragraph" text object, use 'j' and 'k' repeatedly, use a search, or
any other way of moving the cursor. Vim has plenty of cursor motion
commands, and I've never coded in haml, so I'm not sure what ways to
move efficiently about haml code are best.

After you have all the lines selected, press I to insert text just
before the selection on all lines, or A to enter text just after. Type
the desired text, press <Esc>, and Vim will populate all lines with
your text.

That's a lot of explanation, but it should really only be a few
keystrokes. For example, if all goes well, you can type:

CTRL-V
}
I
-#
<Esc>

or

CTRL-V
9j
I
-#
<Esc>

The takeaway message is "use blockwise visual mode for inserting text
on lots of lines at once".

To uncomment, go into blockwise visual mode with CTRL-V, highlight all
the inserted comments, and press d to delete them.

Notes:
* I mentioned the "ap" text object previously, but this changes the
visual mode on you, so you'll need to force visual-block mode after
using. Something like:
v
ap
CTRL-V
I
-#
<Esc>
* It's much easier to use counts on j/k if you turn on the
'relativenumber' option, so you can use my "9j" suggestion without
actually counting lines

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

No comments:

Post a Comment