Last active
December 17, 2015 10:09
-
-
Save olivoil/5592131 to your computer and use it in GitHub Desktop.
Toggle between tabs vs. spaces mode
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| " Return indent (all whitespace at start of a line), converted from | |
| " tabs to spaces if what = 1, or from spaces to tabs otherwise. | |
| " When converting to tabs, result has no redundant spaces. | |
| function! Indenting(indent, what, cols) | |
| let spccol = repeat(' ', a:cols) | |
| let result = substitute(a:indent, spccol, '\t', 'g') | |
| let result = substitute(result, ' \+\ze\t', '', 'g') | |
| if a:what == 1 | |
| let result = substitute(result, '\t', spccol, 'g') | |
| endif | |
| return result | |
| endfunction | |
| " Convert whitespace used for indenting (before first non-whitespace). | |
| " what = 0 (convert spaces to tabs), or 1 (convert tabs to spaces). | |
| " cols = string with number of columns per tab, or empty to use 'tabstop'. | |
| " The cursor position is restored, but the cursor will be in a different | |
| " column when the number of characters in the indent of the line is changed. | |
| function! IndentConvert(line1, line2, what, cols) | |
| let savepos = getpos('.') | |
| let cols = empty(a:cols) ? &tabstop : a:cols | |
| execute a:line1 . ',' . a:line2 . 's/^\s\+/\=Indenting(submatch(0), a:what, cols)/e' | |
| call histdel('search', -1) | |
| call setpos('.', savepos) | |
| endfunction | |
| command! -nargs=? -range=% Space2Tab call IndentConvert(<line1>,<line2>,0,<q-args>) | |
| command! -nargs=? -range=% Tab2Space call IndentConvert(<line1>,<line2>,1,<q-args>) | |
| command! -nargs=? -range=% RetabIndent call IndentConvert(<line1>,<line2>,&et,<q-args>) | |
| " allow toggling between tab and spaces mode | |
| function! TabToggle() | |
| if &expandtab | |
| set shiftwidth=2 | |
| set softtabstop=0 | |
| set noexpandtab | |
| retab! | |
| set list! | |
| Space2Tab | |
| else | |
| set shiftwidth=2 | |
| set softtabstop=2 | |
| set expandtab | |
| set list | |
| retab | |
| Tab2Space | |
| endif | |
| endfunction | |
| nmap <Leader>ta :call TabToggle()<cr> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment