diff options
author | Tom Ryder <tom@sanctum.geek.nz> | 2017-10-30 17:54:47 +1300 |
---|---|---|
committer | Tom Ryder <tom@sanctum.geek.nz> | 2017-10-30 18:08:37 +1300 |
commit | 8c28246d507120174bcdcd455fdf56e289f5b81c (patch) | |
tree | 58fc8e6bda2a3b3af8ee464dba6532a77efc5da7 | |
parent | Use `normal!` not `normal` in Vim config macro (diff) | |
download | dotfiles-8c28246d507120174bcdcd455fdf56e289f5b81c.tar.gz dotfiles-8c28246d507120174bcdcd455fdf56e289f5b81c.zip |
Switch to VimL functions for whitespace stripper
vim-vint says:
>Do not use a command that has unintended side effects (see Google
>VimScript Style Guide (Dangerous))
>Avoid commands that rely on user settings (see Google VimScript Style
>Guide (Fragile))
In both cases, it's referring to the use of :substitute in this file.
The Google style guide on which vim-vint is based says <https://google.github.io/styleguide/vimscriptguide.xml?showone=Fragile_commands#Fragile_commands>:
>Avoid :s[ubstitute], as its behavior depends upon a number of local
>settings.
It also says <https://google.github.io/styleguide/vimscriptguide.xml?showone=Dangerous_commands#Dangerous_commands>:
> Avoid using :s[ubstitute] as it moves the cursor and prints error
> messages. Prefer functions (such as search()) better suited to
> scripts.
>
> For many vim commands, functions exist that do the same thing with
> fewer side effects. See :help functions() for a list of built-in
> functions.
I reimplemented the function based on an answer I found by `romainl` to
a similar question: <https://vi.stackexchange.com/a/5962>
There are plenty of other trailing-whitespace-stripping solutions out
there, but this one can be mine. It now passes vim-vint. I'll make a
plugin out of it at some point.
The \m\C shibboleth at the front of the regular expression is to enforce
the 'magic' setting for the regular expression, and to enforce
case-sensitivity. This is recommended by the style guide:
<https://google.github.io/styleguide/vimscriptguide.xml?showone=Dangerous_commands#Regular_Expressions
> Prefix all regexes with \m\C.
>
> In addition to the case sensitivity settings, regex behavior depends
> upon the user's nomagic setting. To make regexes act like nomagic and
> noignorecase are set, prepend all regexes with \m\C.
>
> You are welcome to use other magic levels (\v) and case sensitivities
> (\c) so long as they are intentional and explicit.
Before I committed this, I checked with vint -s to include stylistic
recommendations as well, and it insisted on l: prefixes to the `li` and
`line` variable to make them explicitly local to the function, so I did
that, too.
-rw-r--r-- | vim/config/whitespace.vim | 9 |
1 files changed, 5 insertions, 4 deletions
diff --git a/vim/config/whitespace.vim b/vim/config/whitespace.vim index 2202f47b..119d2c48 100644 --- a/vim/config/whitespace.vim +++ b/vim/config/whitespace.vim @@ -5,10 +5,11 @@ set nojoinspaces " Strip trailing whitespace with \x if has('eval') function! StripTrailingWhitespace() - let l:search = @/ - %substitute/\s\+$//e - let @/ = l:search - nohlsearch + let l:li = 1 + for l:line in getline(1,'$') + call setline(l:li, substitute(l:line, '\m\C\s\+$', '', 'g')) + let l:li = l:li + 1 + endfor endfunction nnoremap <silent> <leader>x :<C-U>call StripTrailingWhitespace()<CR> endif |