Use this page to discuss script 2308 Clever Tabs: using tabs for indent only on start of line

  • Add constructive comments, bug reports, or discuss improvements (see the guideline).
  • Do not document the script here (the author should do that on
  • This page may be out of date: check the script's page above, and its release notes.

Should use &shiftwidthEdit

Rather than passing shiftwidth as a parameter to the function, I suggest reading the value of the Vim option directly. That way, if the user has different shiftwidths for different file types, this can continue to work transparently.

" clever tabs (tabs only on the line beginning)
function! CleverTabs()
    let line = getline('.')[:col('.')-2]
    if col('.') == 1 || line =~ '^\t*$' || line =~ '^$'
        let z = "\t"
        let space = ""
        let shiftwidth = shiftwidth()
        let shiftwidth = shiftwidth - ((virtcol('.')-1) % shiftwidth)

        while shiftwidth > 0
            let shiftwidth = shiftwidth - 1
            let space = space . ' '

        let z = space

    return z
endfunction "CleverTabs

" map tab key to function
imap <silent> <Tab> <C-r>=CleverTabs()<cr>

--October 9, 2014

I changed your function to use the shiftwidth() function instead of the option, since it changed recently to use 0 as a flag to use tabstop instead. --Fritzophrenic (talk) 16:10, October 10, 2014 (UTC)


Ad blocker interference detected!

Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.