Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configuring variable-width tab spacing in Vim

Tags:

vim

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?

like image 226
Magnus Avatar asked Sep 07 '09 16:09

Magnus


People also ask

What is Shiftwidth in Vim?

shiftwidth — Referred to for “levels of indentation”, where a level of indentation is shiftwidth columns of whitespace. That is, the shift-left-right commands, the formatting commands, and the behavior of vim with cindent or autoindent set is determined by this setting.

How do I show tabs in Vim?

A quick way to visualize whether there is a Tab character is by searching for it using Vim's search-commands : In NORMAL mode, type /\t and hit <Enter> . It will search for the Tab character ( \t ) and highlight the results.


1 Answers

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 
like image 120
Whaledawg Avatar answered Oct 05 '22 03:10

Whaledawg