The following code add a mapping <leader>s
that should do what you want on the specific file you provided, if you use spaces to indent.
nnoremap <leader>s :<c-u>call UnindentSpaces()<cr>
function! UnindentSpaces()
function! GlobalUnindent()
let line = getline('.')
let p = match(line, '\S') - s:n
s;^\s*;\=repeat(' ', p);
endfunction
let line = getline(1)
let s:n = match(line, '\S')
if s:n > 0
g/^/call GlobalUnindent()
endif
endfunction
Conditions in which I tested this code :
set expandtab
set tabstop=4 softtabstop=4 shiftwidth=4
If you use a tab character to indent, then the following code should be used instead (<leader>t
) :
nnoremap <leader>t :<c-u>call UnindentTabs()<cr>
function! UnindentTabs()
function! GlobalUnindent()
let line = getline('.')
let p = match(line, '\S') - s:n
s;^\s*;\=repeat("\t", p);
endfunction
let line = getline(1)
let s:n = match(line, '\S')
if s:n > 0
g/^/call GlobalUnindent()
endif
endfunction
Conditions in which I tested this code :
set noexpandtab
set tabstop=4 softtabstop=4 shiftwidth=4
Tell me if it works for you, otherwise I'll try to modify it if I can.
Edit : if you can rely on the first character of the first line to detect whether the file use spaces or tabs for indentation, then I think you can do it with only one mapping (<leader>u
) and one function which will cover both cases (tabs vs spaces) :
nnoremap <leader>u :<c-u>call Unindent()<cr>
function! Unindent()
function! GlobalUnindent()
let line = getline('.')
let p = match(line, '\S') - s:n
s;^\s*;\=repeat(s:indentchar, p);
endfunction
let line = getline(1)
let s:indentchar = matchstr(line, '\%1c.')
let s:n = match(line, '\S')
if s:n > 0
g/^/call GlobalUnindent()
endif
endfunction
v
, say, subtract fromv
the minimum ofv
so that its new minimum is now zero.