Indent block inline with other blocks is too far

69 views
Skip to first unread message

K otgc

unread,
Jun 27, 2025, 5:12:52 AMJun 27
to vim_use
Hello,
I'm indenting a code block by highlighting the block and then selecting the greater than key.
However this indents the block 8 spaces, which is too far past the other code blocks, which are only 2 spaces indented.
I am using version: VIM - Vi IMproved 8.1 (2018 May 18, compiled Apr 02 2025 16:39:51)
Is there a way to adust the indent please?

dva...@internode.on.net

unread,
Jun 27, 2025, 7:12:08 AMJun 27
to vim...@googlegroups.com
In .vimrc, set "tabstop". For a couple of decades, I've had:
set tabstop=3 expandtab ai
as a 3-space tab looks good on an "if" block.
Expanding tabs to an equivalent number of spaces avoids indenting changing when you share the code with someone from last century, still stuck on 8-space tabs.
Setting autoindent (aka ai) causes "Copy indent from current line when starting a new line (typing <CR> in Insert mode or when using the "o" or "O" command)." as ":help ai" explains.

If you also:
set listchars=tab:>-,trail:-
then tabs will be shown as '>', and trailing spaces as '-'. If a shared
file comes back with tabs, rather than being entirely space-expanded,
then that's visible, and you can then expandtab with your tabstop
setting, forcing consistency.

Erik


Paul

unread,
Jun 27, 2025, 7:17:57 AMJun 27
to vim...@googlegroups.com
Check out `:help 'shiftwidth'`.

K otgc

unread,
Jun 27, 2025, 9:25:24 AMJun 27
to vim_use
Thank you.
I ended up using the below, but unsure which of the 3 choices is easiest.
The Tab doesn't indent a highlighted block, so I need the Shift+> to indent the highlighted lines. Fortunately Shift+> seems to use the Tab spacing amount.

set expandtab

set tabstop=2

set shiftwidth=2

Gary Johnson

unread,
Jun 27, 2025, 12:30:52 PMJun 27
to vim_use, vim...@googlegroups.com
Yes. As someone else wrote, see

help >
help 'sw'

and just execute, or put in your vimrc,

:set sw=2

Regards,
Gary

K otgc

unread,
Jun 28, 2025, 3:20:23 AMJun 28
to vim_use
Just entering :set sw=2 in .vimrc leaves tabbing at 8 spaces.
The post 27 Jun 2025, 15:25:24 with 3 lines of code, is the only code that works to affect tabbing at 2 spaces.

dva...@internode.on.net

unread,
Jun 28, 2025, 4:41:47 AMJun 28
to vim...@googlegroups.com
On 27.06.25 06:25, K otgc wrote:
> I ended up using the below, but unsure which of the 3 choices is easiest.
> The Tab doesn't indent a highlighted block, so I need the Shift+> to indent
> the highlighted lines. Fortunately Shift+> seems to use the Tab spacing
> amount.
> set expandtab
> set tabstop=2
> set shiftwidth=2

They are not separate choices, but elements of one choice.
set  tabstop=2  shiftwidth=2
changes from default to a 2-space width, while
set expandtab
avoids it all coming undone when you share the file with someone with some other tabstop setting. It does that by inserting 2 spaces instead of a tab, thus protecting your code from foreign interference.

set autoindent
is productivity enhancing, as you can then hit <tab> on the first line inside a block to indent it, and subsequent lines in the block will automatically hold that indentation, without you having to find the mouse and faff with it.

A quick way to outdent after the block is to hit ^D on the first line after, e.g. the closing '}'in C. It isn't even necessary to remember to do it before typing the post-block line - the ^D also outdents any time while you're still in insert mode on the line.

In this way, you type your way through the indented block, then back out again, without moving your hands from the keyboard - no slowing for the curve, at all.

Erik

K otgc

unread,
Jun 28, 2025, 7:15:02 AMJun 28
to vim_use
Noice, good mana and potions your way :-)

Gary Johnson

unread,
Jun 28, 2025, 4:24:17 PMJun 28
to vim_use
On 2025-06-28, K otgc wrote:
> Just entering :set sw=2 in .vimrc leaves tabbing at 8 spaces.
> The post�27 Jun 2025, 15:25:24 with 3 lines of code,�is the only code that
> works to affect tabbing at 2 spaces.

In your original post, you didn't mention tabs--you asked about >,
which is a shift operator.

There are two schools of thought about tabs. One is that tabs
should always be 8 columns for consistency and portability. The
other is that tabs should be the same as the shift width. Neither
is right or wrong, just different preferences.

Regards,
Gary

Christian Brabandt

unread,
Jun 29, 2025, 1:47:53 AMJun 29
to vim...@googlegroups.com, vim_use
Hi,
There is extensive documentation and guidance around how to use tabs in the user manual at 
:h 30.5

Thanks 
Chris

Am 28.06.2025 um 09:20 schrieb K otgc <kontheg...@gmail.com>:

Just entering :set sw=2 in .vimrc leaves tabbing at 8 spaces.
--
--
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

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+u...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/vim_use/71db5174-cd0d-43fe-812f-918f719733f7n%40googlegroups.com.

K otgc

unread,
Jun 29, 2025, 4:22:20 AMJun 29
to vim_use
Yes, my query is originally regarding Shift+>, which seemed to be the only way to indent a block of code.
Tab and spacebar would work on individual lines, but not highlighted blocks.
Ultimately, I need the block to indent 2 spaces to match previous code's indentation so the code and file functions.
How to indent highlight code is the main question then, regardless if it's Spacebar, Tab or Shift+>.
According to :h 30.5, spacebar is best practice, to avoid trouble with programs that use a different tabstop value.
However having to manually spacebar multiple lines in my highlighted block is time consuming and manually messy.
For now, tabs have been set to 2 spaces, which has carried over to the Shift+> functionality.

Gary Johnson

unread,
Jun 29, 2025, 9:04:11 AMJun 29
to vim_use
On 2025-06-29, K otgc wrote:
> Yes, my query is originally regarding Shift+>, which seemed to be the only way
> to indent a block of code.
> Tab and spacebar would work on individual lines, but not highlighted blocks.
> Ultimately, I need the block to indent 2 spaces to match previous code's
> indentation so the code and file functions.
> How to indent highlight code is the main question then, regardless if it's
> Spacebar, Tab or Shift+>.
> According to :h 30.5, spacebar is best practice, to avoid trouble with programs
> that use a different tabstop value.
> However having to manually spacebar multiple lines in my highlighted block is
> time consuming and manually messy.

I think you are confusing the space bar with the space character.
Indenting with space characters is recommended, but that does not
mean you have to hit the space bar repeatedly at the start of every
line. Vim can do most indentation for you automatically. Reading
":help 30.3" may help you with that.

Regards,
Gary

Reply all
Reply to author
Forward
0 new messages