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


I've got a pretty well working 'supertab' key based on Maxim Kim's posted solution.

A modified tabbing behavior that jumps to align with the next word on the previous line can be achieved by including this in your vimrc:

" Tab that will advance to align with next word on the previous line
fun! SuperTab() abort
  let spaces = matchstr(getline(line('.')-1)[col('.')-1:], '^\s*')

  return len(spaces) ? spaces : "\<tab>"

inoremap <expr> <C-t> SuperTab()

For a more succinct solution if backwards compatibility is not a concern (since support for a regular expression anchor \%.c that represents the current cursor position has been introduced into vim in version 8.2.3110):

" Tab that will advance to align with next word on the previous line (needs vim >= 8.2.3110)
fun! SuperTab() abort
  let spaces = matchstr(getline(line('.')-1), '\%.c\s*')

  return len(spaces) ? spaces : "\<tab>"

inoremap <expr> <C-t> SuperTab()

Although binding this to something like CtrlTab could feel intuitive, that happens to be one of the key combinations that doesn't have universal support across terminal emulators. For this reason, I've currently got it bound to CtrlT in normal mode.

  • 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 '21 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 '21 at 10:52

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
        return "\<Tab>"
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.


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

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


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

  • 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 '21 at 4:17

You already have a working solution, not sure what your question is about now, since you updated it :/

Anyhow, here is another simple solution. On the line above, where you want the cursor to be positioned, put the cursor at the exact stop and remember the value echoed by :echo col('.')-1. Now enter :setlocal varsofttabstop=<number> where <number> is the result of what col('.')-1 returned.

This makes use of the vartab feature, that allows you to specify how far to stretch the tab. By default a <Tab> usually spans multiples of 8, but with the vartabs feature, you can specify a different amount for several different stop. See the help at :help 'varsofttabstop' for more information.

Note: this can be made a bit more automatic, by creating a function, that analyzes the buffer content and sets the vartabs setting accordingly (My CSV plugin does this for tab delimited files).

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.