From 4c5e30242182c2b970b69381758f17f9e3703b11 Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Fri, 3 Nov 2017 00:36:04 +1300 Subject: Move trailing space strip config into plugin --- vim/config/whitespace.vim | 57 -------------------------------- vim/plugin/strip_trailing_whitespace.vim | 57 ++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 57 deletions(-) delete mode 100644 vim/config/whitespace.vim create mode 100644 vim/plugin/strip_trailing_whitespace.vim diff --git a/vim/config/whitespace.vim b/vim/config/whitespace.vim deleted file mode 100644 index cc2554bd..00000000 --- a/vim/config/whitespace.vim +++ /dev/null @@ -1,57 +0,0 @@ -" Strip trailing whitespace with \x in the whole document -if has('eval') - - " Define function for stripping whitespace - function! s:StripTrailingWhitespace() - - " Iterating line number - let l:li = 1 - - " Line number of last line that had non-whitespace characters on it - let l:lw = 0 - - " Line number of the file's last line - let l:ll = line('$') - - " Iterate over the lines - while l:li <= l:ll - - " Get the line text - let l:line = getline(l:li) - - " Replace the line with a subsitution of its text stripping extraneous - " whitespace - call setline(l:li, substitute(l:line, '\m\C\s\+$', '', 'g')) - - " If this line has any non-whitespace characters on it, update l:lw with - " its index - if l:line =~# '\m\C\S' - let l:lw = l:li - endif - - " Increment the line counter for the next iteration - let l:li = l:li + 1 - endwhile - - " If the last non-whitespace line was before the last line proper, we can - " delete all lines after it - if l:lw < l:ll - - " Get the current line and column so we can return to it - " (Yes I know about winsaveview() and winrestview(); I want this to work - " even on very old versions of Vim if possible) - let l:lc = line('.') - let l:cc = col('.') - - " Delete the lines, which will move the cursor - execute l:lw + 1.',$ delete' - - " Return the cursor to the saved position - call cursor(l:lc, l:cc) - endif - endfunction - - " Map \x to the function just defined - nnoremap x :call StripTrailingWhitespace() - -endif diff --git a/vim/plugin/strip_trailing_whitespace.vim b/vim/plugin/strip_trailing_whitespace.vim new file mode 100644 index 00000000..cc2554bd --- /dev/null +++ b/vim/plugin/strip_trailing_whitespace.vim @@ -0,0 +1,57 @@ +" Strip trailing whitespace with \x in the whole document +if has('eval') + + " Define function for stripping whitespace + function! s:StripTrailingWhitespace() + + " Iterating line number + let l:li = 1 + + " Line number of last line that had non-whitespace characters on it + let l:lw = 0 + + " Line number of the file's last line + let l:ll = line('$') + + " Iterate over the lines + while l:li <= l:ll + + " Get the line text + let l:line = getline(l:li) + + " Replace the line with a subsitution of its text stripping extraneous + " whitespace + call setline(l:li, substitute(l:line, '\m\C\s\+$', '', 'g')) + + " If this line has any non-whitespace characters on it, update l:lw with + " its index + if l:line =~# '\m\C\S' + let l:lw = l:li + endif + + " Increment the line counter for the next iteration + let l:li = l:li + 1 + endwhile + + " If the last non-whitespace line was before the last line proper, we can + " delete all lines after it + if l:lw < l:ll + + " Get the current line and column so we can return to it + " (Yes I know about winsaveview() and winrestview(); I want this to work + " even on very old versions of Vim if possible) + let l:lc = line('.') + let l:cc = col('.') + + " Delete the lines, which will move the cursor + execute l:lw + 1.',$ delete' + + " Return the cursor to the saved position + call cursor(l:lc, l:cc) + endif + endfunction + + " Map \x to the function just defined + nnoremap x :call StripTrailingWhitespace() + +endif -- cgit v1.2.3