Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to tab without losing block

Tags:

vim

I have been using vim for about 3 weeks now and have been having problem with tabs.

What I usually do is do gg to go to the top, do a ctrl+v for visual block, do a G to select everything to below(one column), do a $ to select to the right, then press < or > to tab.

However having to do all this, I lose the selection and I have to do all those commands to do another tab.

How do I not lose selection? Or is there a better way?

NOTE: I sometimes don't need to select everything sometimes just a portion of the file.

like image 442
Teej Avatar asked Mar 09 '11 16:03

Teej


2 Answers

To reselect a visual selection use the gv command followed by your command. Although this is not the best way.

Instead select the whole buffer with ggVG then indent with >. This will indent the selection. To repeat the command again just press .. The . command will repeat the last normal command, in this case the > command. If you have indent a few times to many just use the undo command, u, as many times as necessary. This vimcast is a great screencast describing this technique and this one describe more indention techniques.

Others prefer the following mappings:

xnoremap <Tab> >gv
xnoremap <S-Tab> <gv
like image 100
Peter Rincker Avatar answered Nov 06 '22 05:11

Peter Rincker


I am not sure what is important here: being able to do multiple indents in a single go or keeping the visual block. Assuming you want to do multiple indents, just prefix the > with a number, e.g. 3> will indent the selected block three times.

Also, you don't need to select the entire line to indent it (i.e. the $ is not needed).

like image 43
Brian Rasmussen Avatar answered Nov 06 '22 05:11

Brian Rasmussen