8

I sometimes want Vim to read tab-formatted files where the most reasonable formatting implies a non-uniform tab width. In other words, I want a tab stop at positions:

5, 30, 50, 60, 70, 80

How can I do this in Vim?

1
  • There is a feature called variable tabstops which has been kicked around for a long time in vim. It was just reintegrated into the master branch in the last couple days. I've tested it a bit - had to patch it to avoid a few arithmetic errors, but it now appears pretty stable. You may be in luck pretty soon. Commented Oct 29, 2009 at 19:53

2 Answers 2

4

If you don't actually need to change the tabstops and can get away with just inserting the correct number of spaces, I would suggest you script it. Here's a quick and dirty version that might do what you want:

let s:tabstops = [0, 5, 30, 50, 60, 70, 80]
fun! Find_next(pos)
  if a:pos > min(s:tabstops) && a:pos < max(s:tabstops) 
    let my_count = 0
    while my_count < len(s:tabstops) - 1
      if a:pos > get(s:tabstops, my_count) && a:pos < get(s:tabstops, my_count+1)
        return get(s:tabstops, my_count+1)
      endif
      let my_count = my_count + 1
    endwhile
    return -1
  endif
  return -1
endfun
fun! Tabbing()
  let pos = col('.')
  let next_stop = Find_next(pos)
  let the_command = "normal i"
  let my_count = 0
  while my_count < next_stop - pos
    let the_command = the_command . " "
    let my_count = my_count + 1
  endwhile
  let the_command = the_command . ""
  execute the_command
endfun
imap <TAB> j<ESC>:call Tabbing()<CR>lxi 
Sign up to request clarification or add additional context in comments.

1 Comment

This is a great function, although this will not reformat an existing file, which is what I needed, but assist in formatting my own file on the fly.
0

Currently no. Not with any official builds.

However, if you're willing to invest a little effort on your side, I remember there was a patch for something like that. Check out vim's patches page.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.