From dc5d23f3b64d82ac692151d6141023baf4cd3397 Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Mon, 30 Oct 2017 17:21:41 +1300 Subject: Use ==# consistently in Vim config I got a set of warnings from vim-vint about using just "==" for these comparisons: >Use robust operators `==#` or `==?` instead of `==` (see Google >VimScript Style Guide (Matching)) It does seem a lot more sensible to be explicit about case sensitivity, and not to lean on the configured 'ignorecase' value, especially if the user changes it. --- vim/config/format.vim | 2 +- vim/config/linebreak.vim | 6 +++--- vim/config/syntax.vim | 2 +- vim/vimrc | 4 ++-- 4 files changed, 7 insertions(+), 7 deletions(-) (limited to 'vim') diff --git a/vim/config/format.vim b/vim/config/format.vim index cbc995af..fadbd8f7 100644 --- a/vim/config/format.vim +++ b/vim/config/format.vim @@ -1,6 +1,6 @@ " If we can, add j to the format options to get rid of comment leaders when " joining lines -if v:version > 703 || v:version == 703 && has('patch541') +if v:version > 703 || v:version ==# 703 && has('patch541') set formatoptions+=j endif diff --git a/vim/config/linebreak.vim b/vim/config/linebreak.vim index 887d5f39..5abdabd1 100644 --- a/vim/config/linebreak.vim +++ b/vim/config/linebreak.vim @@ -4,7 +4,7 @@ if has('linebreak') set linebreak set showbreak=... - if v:version > 704 || v:version == 704 && has('patch338') + if v:version > 704 || v:version ==# 704 && has('patch338') set breakindent endif @@ -15,13 +15,13 @@ if has('linebreak') if &linebreak set nolinebreak set showbreak= - if v:version > 704 || v:version == 704 && has('patch338') + if v:version > 704 || v:version ==# 704 && has('patch338') set nobreakindent endif else set linebreak set showbreak=... - if v:version > 704 || v:version == 704 && has('patch338') + if v:version > 704 || v:version ==# 704 && has('patch338') set breakindent endif endif diff --git a/vim/config/syntax.vim b/vim/config/syntax.vim index df75b3f0..08b908c8 100644 --- a/vim/config/syntax.vim +++ b/vim/config/syntax.vim @@ -19,7 +19,7 @@ if has('syntax') let l:bg = len(l:colorfgbg) ? l:colorfgbg[-1] : '' " Choose the background setting based on this value - if l:bg == 'default' || l:bg == '7' || l:bg == '15' + if l:bg ==# 'default' || l:bg ==# '7' || l:bg ==# '15' set background=light else set background=dark diff --git a/vim/vimrc b/vim/vimrc index 043dea8b..c72857ce 100644 --- a/vim/vimrc +++ b/vim/vimrc @@ -26,8 +26,8 @@ if v:version >= 701 " The 'sahara' colorscheme only works for dark backgrounds with 256 colors if has('syntax') - \ && &background == 'dark' - \ && (has('gui_running') || &t_Co == 256) + \ && &background ==# 'dark' + \ && (has('gui_running') || &t_Co ==# 256) silent! colorscheme sahara endif endif -- cgit v1.2.3 From ba6e8ed9016343b4ae7acc2cac6e3a637b42b619 Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Mon, 30 Oct 2017 17:22:55 +1300 Subject: Remove 'nocompatible' setting vim-vint says: >Do not use nocompatible which has unexpected effects (see :help >nocompatible) I can't actually find anything in the help item it references that says that setting 'nocompatible' is bad, but the situation in which it's needed is very niche anyway; per the removed comment: >Don't make any effort to be compatible with vi, use more sensible >settings. This is only here in case this file gets loaded explicitly >with -u; the mere existence of a ~/.vimrc file already turns this off. We'll just leave it out, and see if anything bad happens..."if in doubt, rip it out". --- vim/vimrc | 5 ----- 1 file changed, 5 deletions(-) (limited to 'vim') diff --git a/vim/vimrc b/vim/vimrc index c72857ce..e0020b5e 100644 --- a/vim/vimrc +++ b/vim/vimrc @@ -1,8 +1,3 @@ -" Don't make any effort to be compatible with vi, use more sensible settings. -" This is only here in case this file gets loaded explicitly with -u; the mere -" existence of a ~/.vimrc file already turns this off. -set nocompatible - " Use UTF-8 by default wherever possible, including in this file if has('multi_byte') set encoding=utf-8 -- cgit v1.2.3 From 654e907668eb9f392c2fc15201d0f1bd7808be00 Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Mon, 30 Oct 2017 17:28:00 +1300 Subject: Use single-quoted string in gvimrc vim-vint says: >Prefer single quoted strings (see Google VimScript Style Guide >(Strings)) Perl::Critic warns about a similar thing; don't use doublequotes if you don't need to expand e.g. \n, \r or interpolate variables. Makes sense to me. --- vim/gvimrc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'vim') diff --git a/vim/gvimrc b/vim/gvimrc index 2db471da..286b84ba 100644 --- a/vim/gvimrc +++ b/vim/gvimrc @@ -1,7 +1,7 @@ " My choice of font changes depending on which operating system I'm using; " these are both workable monospace fonts, but Ubuntu Mono doesn't render very " nicely on Windows a lot of the time -if has("unix") +if has('unix') silent! set guifont=Ubuntu\ Mono\ 12 else silent! set guifont=Consolas:h11 -- cgit v1.2.3 From 13ada038d3e8cd6a066a123312db4c711549c713 Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Mon, 30 Oct 2017 17:41:33 +1300 Subject: Use `normal!` not `normal` in Vim config macro vim-vint says: >Avoid commands that rely on user settings (see Google VimScript Style >Guide (Fragile)) The style guide explains: >Always use normal! instead of normal. The latter depends upon the >user's key mappings and could do anything. Can't argue with that... --- vim/indent/html.vim | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'vim') diff --git a/vim/indent/html.vim b/vim/indent/html.vim index 7b16fb3a..a6f84726 100644 --- a/vim/indent/html.vim +++ b/vim/indent/html.vim @@ -6,9 +6,9 @@ nnoremap v :exe "!tidy -eq -utf8 " . shellescape(expand("%")) " Make a bare URL into a link to itself function! UrlLink() - normal yiW - execute "normal i0\">\" - normal E - execute "normal a\" + normal! yiW + execute "normal! i0\">\" + normal! E + execute "normal! a\" endfunction nnoremap r :call UrlLink() -- cgit v1.2.3 From 8c28246d507120174bcdcd455fdf56e289f5b81c Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Mon, 30 Oct 2017 17:54:47 +1300 Subject: 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 : >Avoid :s[ubstitute], as its behavior depends upon a number of local >settings. It also says : > 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: 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: 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. --- vim/config/whitespace.vim | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'vim') 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 x :call StripTrailingWhitespace() endif -- cgit v1.2.3