expanding tabs in a visual selection

168 views
Skip to first unread message

Tim Johnson

unread,
Sep 16, 2012, 12:43:05 PM9/16/12
to vim ML
Hi :

Is there a way to apply the expand/retab process to a visual
selection?

:h ??

thanks
--
Tim
tim at tee jay forty nine dot com or akwebsoft dot com
http://www.akwebsoft.com

Bee

unread,
Sep 16, 2012, 1:02:16 PM9/16/12
to vim_use
:help :retab

:[range]ret[ab][!]
[new_tabstop]

Bill

Tim Johnson

unread,
Sep 16, 2012, 1:54:59 PM9/16/12
to vim_use
* Bee <fo...@calcentral.com> [120916 09:09]:
<blush> I shoulda asked for an example.
If I do the following
v " start visual mode
jjjj " select 4 more lines
:retab " nothing
:retab! " nothing
What else need I do?

Gary Johnson

unread,
Sep 16, 2012, 2:41:45 PM9/16/12
to vim_use
On 2012-09-16, Tim Johnson wrote:
> * Bee <fo...@calcentral.com> [120916 09:09]:
> >
> >
> > On Sep 16, 9:43�am, Tim Johnson <t...@akwebsoft.com> wrote:
> > > Hi :
> > >
> > > Is there a way to apply the expand/retab process to a visual
> > > selection?
> > >
> > > :h ??
> > >
> > > thanks
> > > --
> > > Tim
> > > tim at tee jay forty nine dot com or akwebsoft dot comhttp://www.akwebsoft.com
> >
> >
> > :help :retab
> >
> > :[range]ret[ab][!]
> > [new_tabstop]
> <blush> I shoulda asked for an example.
> If I do the following
> v " start visual mode
> jjjj " select 4 more lines
> :retab " nothing
> :retab! " nothing
> What else need I do?

I don't know. It works fine for me.

Since :retab takes a range of lines, you should probably start
visual mode with V rather than v. The latter will work, but the
range of text affected by the :retab command will be more apparent
with the former.

Example:

:set ts=8
:set list
:set noexpandtab

Enter the following text, with no leading space and using a real
tab where <Tab> is shown.

abc<Tab>cow
def<Tab>chicken
ghi<Tab>horse
jkl<Tab>sheep
mno<Tab>pig

:set expandtab

Move the cursor to "def" and type

Vjj:retab

You should see each of the tabs in those three lines replaced by
five spaces.

:set noexpandtab

Move the cursor to "def" and type

Vj:retab

You should see no change. Now type

gv:retab

You should see the spaces replaced by tabs on the two visually-
selected lines but not on the "jkl" line.

Do you see different behavior?

Note that :retab! will not replace a single space by a tab.

Regards,
Gary

Gary Johnson

unread,
Sep 16, 2012, 2:51:14 PM9/16/12
to vim_use
On 2012-09-16, Gary Johnson wrote:

...
> Move the cursor to "def" and type
>
> Vj:retab
>
> You should see no change. Now type
>
> gv:retab

That should have been

gv:retab!

> You should see the spaces replaced by tabs on the two visually-
> selected lines but not on the "jkl" line.

Regards,
Gary

Tony Mechelynck

unread,
Sep 16, 2012, 2:56:55 PM9/16/12
to vim_use
On 16/09/12 19:54, Tim Johnson wrote:
> * Bee <fo...@calcentral.com> [120916 09:09]:
>>
>>
>> On Sep 16, 9:43 am, Tim Johnson <t...@akwebsoft.com> wrote:
>>> Hi :
>>>
>>> Is there a way to apply the expand/retab process to a visual
>>> selection?
>>>
>>> :h ??
>>>
>>> thanks
>>> --
>>> Tim
>>> tim at tee jay forty nine dot com or akwebsoft dot comhttp://www.akwebsoft.com
>>
>>
>> :help :retab
>>
>> :[range]ret[ab][!]
>> [new_tabstop]
> <blush> I shoulda asked for an example.
> If I do the following
> v " start visual mode
> jjjj " select 4 more lines
> :retab " nothing
> :retab! " nothing
> What else need I do?
> thanks
>

When you type : in Visual mode, you should see

:'<,'>

where '< means "the first visually selected line" and '> means "the last
visually selected line". Don't erase these marks as you type :retab in
Visual mode. Or you could do without Visual mode, as follows:

:set expandtab
5:retab

(that second line will be expanded to :.,.+4retab and it should expand
tabs to spaces in the current line and the 4 following ones). Or

:set noexpandtab
5:retab!

to convert spaces to tabs instead.

See
:help v_:
:help N:


Best regards,
Tony.
--
Mandrell: "You know what I think?"
Doctor: "Ah, ah that's a catch question. With a brain your size you
don't think, right?"
-- Dr. Who

Tim Johnson

unread,
Sep 16, 2012, 3:44:30 PM9/16/12
to vim_use
* Gary Johnson <gary...@spocom.com> [120916 10:50]:
>
> Do you see different behavior?
>
> Note that :retab! will not replace a single space by a tab.
That did it.
thanks, Gary

Tim Johnson

unread,
Sep 16, 2012, 5:21:12 PM9/16/12
to vim_use
* Tony Mechelynck <antoine.m...@gmail.com> [120916 11:02]:
>
> When you type : in Visual mode, you should see
>
> :'<,'>
>
> where '< means "the first visually selected line" and '> means "the last
> visually selected line". Don't erase these marks as you type :retab in
> Visual mode. Or you could do without Visual mode, as follows:
>
> :set expandtab
> 5:retab
Elegant!

> (that second line will be expanded to :.,.+4retab and it should expand
> tabs to spaces in the current line and the 4 following ones). Or
>
> :set noexpandtab
> 5:retab!
Again ...
> to convert spaces to tabs instead.
>
> See
> :help v_:
> :help N:

Tony Mechelynck

unread,
Sep 16, 2012, 9:05:43 PM9/16/12
to vim_use
On 16/09/12 23:21, Tim Johnson wrote:
> * Tony Mechelynck <antoine.m...@gmail.com> [120916 11:02]:
>>
>> When you type : in Visual mode, you should see
>>
>> :'<,'>
>>
>> where '< means "the first visually selected line" and '> means "the last
>> visually selected line". Don't erase these marks as you type :retab in
>> Visual mode. Or you could do without Visual mode, as follows:
>>
>> :set expandtab
>> 5:retab
> Elegant!
>
>> (that second line will be expanded to :.,.+4retab and it should expand
>> tabs to spaces in the current line and the 4 following ones). Or
>>
>> :set noexpandtab
>> 5:retab!
> Again ...
>> to convert spaces to tabs instead.
>>
>> See
>> :help v_:
>> :help N:
> thanks
>

Beware though, that the former will convert hard tabs (but not
backslash-t etc.) even in string literals, and that the latter will
convert sequences of two or more spaces ending on a tab stop (i.e., in a
column whose number is a multiple of the 'tabstop' value) even in string
literals. IOW the :retab command doesn't know what quotes are for.

Best regards,
Tony.
--
We really don't have any enemies. It's just that some of our best
friends are trying to kill us.

Tim Johnson

unread,
Sep 17, 2012, 2:43:26 PM9/17/12
to vim...@googlegroups.com
* Tony Mechelynck <antoine.m...@gmail.com> [120916 18:45]:
> >
>
> Beware though, that the former will convert hard tabs (but not
> backslash-t etc.) even in string literals, and that the latter will
> convert sequences of two or more spaces ending on a tab stop (i.e., in a
> column whose number is a multiple of the 'tabstop' value) even in string
> literals. IOW the :retab command doesn't know what quotes are for.
Understood. Thanks, Tony.
Reply all
Reply to author
Forward
0 new messages