1

How can we do this with vim?

set hlsearch                        " Highlight search matches
set incsearch                       " Highlight search matches as you type
set visualbell                      " Use visual bell instead of beeping
set shortmess+=I                    " Disable vim intro message
set lazyredraw      █...............x<-- Put the cursor here

In particular:

  • Should work without changing normal Tab functionality
  • Solution doesn't necessarily have to use the Tab key
2
  • Tabs are tabs. You can't get them to behave in non-standard ways. That means you can set 'tabstop' to a large enough number to get you there with one Tab press (or some equivalent variation of that) and that's about it. If you relax your requirements so other potential keystrokes are allowed besides just pressing Tab your options may open up a bit more.
    – B Layer
    Aug 2 at 10:43
  • Just to clarify, you could change the behavior of Tab if you override it (i.e. with a key mapping)...but I assumed you didn't have that in mind.
    – B Layer
    Aug 2 at 10:52
2

Well, you can do it with vimscript, here is the proof of concept:

func! MyTab() abort
    let spaces = matchstr(getline(line('.')-1), '\%.c\s*')
    if len(spaces)
        return spaces
    else
        return "\<Tab>"
    endif
endfunc
inoremap <expr> <tab> MyTab()

enter image description here

NOTE though, the example uses quite recent vim that has added \%.c for regexes -- match at the current column position. With older vim you have to substitute it with real column position taken from either col('.') or getpos() functions.

Upd

If you don't want to change "normal" tab functionality do not remap tab, do

inoremap <expr> <C-j> MyTab()

instead.

Still if you look for some very simple solution, like existing built-in setting to change -- there is none atm afaik.

1
  • I just tested this and I like how it works. With your permission, may I take a shot at further improving this answer?
    – webninja
    Aug 3 at 4:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.