Wednesday, February 26, 2025

Re: Unexpected indentation with :left

On Wed, Feb 26, 2025 at 7:44 PM Tim Chase <vim@tim.thechases.com> wrote:

The gist of my question/hope was wanting to get the N in

  :left N

to prepend N *indents* (using N tabs if 'noet' and N*shiftwidth if 'et'
is set)

As it currently stands, with 'et' set, I get N spaces, not N
'shiftwidth', and if I have 'noet' set, I get int(N/tabstop) tabs
followed by mod(N, tabstop) spaces.  E.g.

  :set ts=8 sw=4

with

  :set et
  :'<,'>left 10

puts 10 spaces at the beginning of each trimmed line.

And without 'et' set:

  :set noet
  :'<,'>left 10

gives me one tab (8-worth) followed by 2 spaces.

Neither gives me 10 tabstops or 10 shiftwidths which is what I'd
expected.

-tim

This seemed like it might be useful, so I wrote it:

# Different from regular :left in that the indent is taken to be a multiple of the &shiftwidth setting and not just a number of spaces.
export def g:LeftIndentLines( line1: number, line2: number, indent: string )
  var indentSize: number = indent == '' ? 0 : str2nr( indent )

  if ( indentSize > 0 )
    # Replace existing indent with the number of spaces required
    execute $':{line1},{line2}s/^\s*/{repeat(" ", indentSize * &shiftwidth)}'

    # Use retab to insert tabs if tab-based indentation is enabled3
    execute $':{line1},{line2}retab!'
  else
    execute $':{line1},{line2}left'
  endif
enddef
com! -nargs=? -range Left g:LeftIndentLines( <line1>, <line2>, <q-args> )

Call just like the regular :left, except with :Left. Takes a range and an indent (default is 0, just like regular :left). The only difference between this and the built in version is that the number passed to this is seen as a multiple of the shiftwidth instead of an absolute number of spaces. It will also call `retab!` to convert indents to tabs as needed.

--
--
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+unsubscribe@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/vim_use/CANuxnEdBcSh%2B_qe4ZQ7mhZvZtV9DcnC%2BZsumDvZcZGDnV-XuwQ%40mail.gmail.com.

No comments: