Vim - set automatic indentation to fill a space with a space or tabstop

It seems that if we enable 'ai', vim will fill the tabstop leading space. I can fill it with a space "et". I don't like the C file mixed with space and tabstop.

My vimrc:

set ts=4 et
set ai
set hlsearch
syntax on
filetype plugin indent on
autocmd FileType make setlocal noexpandtab

However, in some cases I need to enter tabstop when I press "TAB" on the keyboard, for example, in a makefile and some others. The autocmd FileType command does not work: I cannot add each file type to vimrc.

I want just :

  • autoindent to fill the main area using space;
  • when you press "TAB" on the keyboard, tabstop is the input, not a space (therefore no 'et')

How to do it?

+3
source share
3
inoremap <expr> <tab> ((getline('.')[:col('.')-2]=~'\S')?("\<C-v>\t"):(repeat(' ', &ts-((virtcol('.')-1)%&ts))))

, @Lynch, .

<C-v><Tab>: <Tab> - expandtab, - <C-v> <C-v><Tab>.

inoremap <Tab> <C-v><Tab>

expandtab.

+1

. , , , - . vimrc:

set et

function! Inserttab()
    let insert = ""
    let line = getline('.')
    let pos = getpos('.')[2]
    let before = ""
    let after = line
    if pos != 1
        let before = line[ 0: pos - 1]  
        let after = line[pos : strlen(line) ]
    endif
    if pos != 1 && substitute(before, "[ \t]", "", "g") != "" 
         let insert = "\t"
    else
         let insert = "    "
    endif
    let line = before . insert . after 
    call setline('.', line)
    call cursor(line('.'), strlen(before . insert))
endfunction

inoremap <tab> <esc>:call Inserttab()<CR>a

Inserttab(). , ts -, 4, 4 , .

vim-, , , .

, "" set list. set nolist. ga, , .

Edit , . My script .

, , :

set et

function! Inserttab()
    let insert = ""
    let line = getline('.')
    let pos = getpos('.')[2]
    let before = ""
    let after = line
    if pos != 1
        let before = line[ 0: pos - 1]  
        let after = line[pos : strlen(line) ]
    endif
    let insert = "\t"
    let line = before . insert . after 
    call setline('.', line)
    call cursor(line('.'), strlen(before . insert))
endfunction

inoremap <tab> <esc>:call Inserttab()<CR>a

, .

0

-

  • : sw = 4 ( )
  • : set ts = 46 (or some large amount)

Then autoindent will not insert tabs unless you reach 46 spaces, in which case you can add a larger number.

Only drag this if someone uses tabs, then you need to reset ts to accept the file you are editing. On the other hand, this will make bookmarks immediately obvious, which may also be desirable.

0
source

All Articles