From 854b2273e58f3e831297336dcaa2e2646461b5ce Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Fri, 3 Nov 2017 20:43:38 +1300 Subject: Refactor HTML tidy(1) mapping Move the logic into a script function. Use single quotes for the strings, too, since we don't need interpolation. --- vim/ftplugin/html.vim | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/vim/ftplugin/html.vim b/vim/ftplugin/html.vim index 309b7132..b305a223 100644 --- a/vim/ftplugin/html.vim +++ b/vim/ftplugin/html.vim @@ -1,5 +1,8 @@ " Run tidy -eq -utf8 on file for the current buffer -nnoremap v :exe "!tidy -eq -utf8 " . shellescape(expand("%")) +function s:HTMLTidy() + execute '!tidy -eq -utf8 ' . shellescape(expand('%')) +endfunction +nnoremap v :exe :call HTMLTidy() " Make a bare URL into a link to itself function! s:UrlLink() -- cgit v1.2.3 From 4a7b3b3cc29ed48a96535284446d921be414fb0b Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Fri, 3 Nov 2017 20:47:42 +1300 Subject: Use single quotes for HTML link mapping :execute Saves some backslashing, just like in shell and Perl! --- vim/ftplugin/html.vim | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/vim/ftplugin/html.vim b/vim/ftplugin/html.vim index b305a223..b4eb9e6b 100644 --- a/vim/ftplugin/html.vim +++ b/vim/ftplugin/html.vim @@ -7,8 +7,8 @@ nnoremap v :exe :call HTMLTidy() " Make a bare URL into a link to itself function! s:UrlLink() normal! yiW - execute "normal! i0\">\" + execute 'normal! i\" + execute 'normal! a\' endfunction nnoremap r :call UrlLink() -- cgit v1.2.3 From 810cb9d53c09ccb8d65a4e996f088b7af3b9a40b Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Fri, 3 Nov 2017 21:55:48 +1300 Subject: Refactor UrlLink() function normal! commands Tidy up the 'normal!' commands and comment them in appropriate groups. Take advantage of the natural command-termination at the end of a 'normal!' string to end insert mode. It would be better to do all of this with pure VimL functions, but I don't know how yet. --- vim/ftplugin/html.vim | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/vim/ftplugin/html.vim b/vim/ftplugin/html.vim index b4eb9e6b..7400326c 100644 --- a/vim/ftplugin/html.vim +++ b/vim/ftplugin/html.vim @@ -6,9 +6,17 @@ nnoremap v :exe :call HTMLTidy() " Make a bare URL into a link to itself function! s:UrlLink() + + " Yank this whole whitespace-separated word normal! yiW - execute 'normal! i + " Paste the URL into the quotes + normal! hP + " Move to the end of the link text URL normal! E - execute 'normal! a\' + " Close the link tag + normal! a + endfunction nnoremap r :call UrlLink() -- cgit v1.2.3 From ffb5cbc7c681e2fdcb780dbdc51cf3458a937791 Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Fri, 3 Nov 2017 22:20:09 +1300 Subject: Adjust UrlLink() to yank word without text objects --- vim/ftplugin/html.vim | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/vim/ftplugin/html.vim b/vim/ftplugin/html.vim index 7400326c..64cf3175 100644 --- a/vim/ftplugin/html.vim +++ b/vim/ftplugin/html.vim @@ -8,7 +8,9 @@ nnoremap v :exe :call HTMLTidy() function! s:UrlLink() " Yank this whole whitespace-separated word - normal! yiW + normal! W + normal! B + normal! yE " Open a link tag normal! i " Paste the URL into the quotes -- cgit v1.2.3 From a6b6f843eea0d101bf533c1e0894a95e13dd1c75 Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Fri, 3 Nov 2017 22:22:56 +1300 Subject: Revert "Adjust UrlLink() to yank word without t... I thought text objects were introduced to Vim a lot later than they actually were; this works fine even in Vim 6, so I'll leave it as it's nicer. This reverts commit ffb5cbc7c681e2fdcb780dbdc51cf3458a937791. --- vim/ftplugin/html.vim | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/vim/ftplugin/html.vim b/vim/ftplugin/html.vim index 64cf3175..7400326c 100644 --- a/vim/ftplugin/html.vim +++ b/vim/ftplugin/html.vim @@ -8,9 +8,7 @@ nnoremap v :exe :call HTMLTidy() function! s:UrlLink() " Yank this whole whitespace-separated word - normal! W - normal! B - normal! yE + normal! yiW " Open a link tag normal! i " Paste the URL into the quotes -- cgit v1.2.3 From 04031093dd873153b8af734fae5122bf27dcdb19 Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Fri, 3 Nov 2017 22:47:59 +1300 Subject: Check for availability of Vim shellescape() It doesn't seem to be in very old Vims; worth testing for to avoid errors if I try to use the function. --- vim/ftplugin/html.vim | 10 ++++++---- vim/ftplugin/perl.vim | 17 +++++++++++------ 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/vim/ftplugin/html.vim b/vim/ftplugin/html.vim index 7400326c..5505dbaa 100644 --- a/vim/ftplugin/html.vim +++ b/vim/ftplugin/html.vim @@ -1,8 +1,10 @@ " Run tidy -eq -utf8 on file for the current buffer -function s:HTMLTidy() - execute '!tidy -eq -utf8 ' . shellescape(expand('%')) -endfunction -nnoremap v :exe :call HTMLTidy() +if exists('*shellescape') + function s:HTMLTidy() + execute '!tidy -eq -utf8 ' . shellescape(expand('%')) + endfunction + nnoremap v :exe :call HTMLTidy() +endif " Make a bare URL into a link to itself function! s:UrlLink() diff --git a/vim/ftplugin/perl.vim b/vim/ftplugin/perl.vim index 53341183..c4923051 100644 --- a/vim/ftplugin/perl.vim +++ b/vim/ftplugin/perl.vim @@ -1,6 +1,11 @@ -" Run perl -c on file for the current buffer -nnoremap pc :exe "!perl -c " . shellescape(expand("%")) -" Run perlcritic on the file for the current buffer -nnoremap pl :exe "!perlcritic " . shellescape(expand("%")) -" Run the current buffer through perltidy -nnoremap pt :%!perltidy +" External commands for Perl files +if exists('*shellescape') + + " Run perl -c on file for the current buffer + nnoremap pc :exe "!perl -c " . shellescape(expand("%")) + " Run perlcritic on the file for the current buffer + nnoremap pl :exe "!perlcritic " . shellescape(expand("%")) + " Run the current buffer through perltidy + nnoremap pt :%!perltidy + +endif -- cgit v1.2.3 From 381812518e1625ffa3d000332e5198b5ac219f73 Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Fri, 3 Nov 2017 22:52:46 +1300 Subject: Use full ':execute' not just ':exe' in VimL We should probably avoid this sort of abbreviation in scripts. --- vim/ftplugin/html.vim | 2 +- vim/ftplugin/perl.vim | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/vim/ftplugin/html.vim b/vim/ftplugin/html.vim index 5505dbaa..d705bd71 100644 --- a/vim/ftplugin/html.vim +++ b/vim/ftplugin/html.vim @@ -3,7 +3,7 @@ if exists('*shellescape') function s:HTMLTidy() execute '!tidy -eq -utf8 ' . shellescape(expand('%')) endfunction - nnoremap v :exe :call HTMLTidy() + nnoremap v :execute :call HTMLTidy() endif " Make a bare URL into a link to itself diff --git a/vim/ftplugin/perl.vim b/vim/ftplugin/perl.vim index c4923051..310ffe7e 100644 --- a/vim/ftplugin/perl.vim +++ b/vim/ftplugin/perl.vim @@ -2,9 +2,9 @@ if exists('*shellescape') " Run perl -c on file for the current buffer - nnoremap pc :exe "!perl -c " . shellescape(expand("%")) + nnoremap pc :execute "!perl -c " . shellescape(expand("%")) " Run perlcritic on the file for the current buffer - nnoremap pl :exe "!perlcritic " . shellescape(expand("%")) + nnoremap pl :execute "!perlcritic " . shellescape(expand("%")) " Run the current buffer through perltidy nnoremap pt :%!perltidy -- cgit v1.2.3 From e6bfb8f7065ded88ebc1af4dec865a715f33f03f Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Fri, 3 Nov 2017 22:55:39 +1300 Subject: Use direct :write !cmd instead of shellescape() This is a much better method of calling external programs on the buffer's contents, not just because it avoids the mess of :execute evaluation but also because it doesn't require that there actually be a filename for the current buffer. This drastically simplifies the HTML tidy(1) call in particular. --- vim/ftplugin/html.vim | 7 +------ vim/ftplugin/perl.vim | 17 ++++++----------- 2 files changed, 7 insertions(+), 17 deletions(-) diff --git a/vim/ftplugin/html.vim b/vim/ftplugin/html.vim index d705bd71..4d3991f3 100644 --- a/vim/ftplugin/html.vim +++ b/vim/ftplugin/html.vim @@ -1,10 +1,5 @@ " Run tidy -eq -utf8 on file for the current buffer -if exists('*shellescape') - function s:HTMLTidy() - execute '!tidy -eq -utf8 ' . shellescape(expand('%')) - endfunction - nnoremap v :execute :call HTMLTidy() -endif +nnoremap v :write !tidy -eq -utf8 " Make a bare URL into a link to itself function! s:UrlLink() diff --git a/vim/ftplugin/perl.vim b/vim/ftplugin/perl.vim index 310ffe7e..eeed2bb7 100644 --- a/vim/ftplugin/perl.vim +++ b/vim/ftplugin/perl.vim @@ -1,11 +1,6 @@ -" External commands for Perl files -if exists('*shellescape') - - " Run perl -c on file for the current buffer - nnoremap pc :execute "!perl -c " . shellescape(expand("%")) - " Run perlcritic on the file for the current buffer - nnoremap pl :execute "!perlcritic " . shellescape(expand("%")) - " Run the current buffer through perltidy - nnoremap pt :%!perltidy - -endif +" Run perl -c on file for the current buffer +nnoremap pc :write !perl -c +" Run perlcritic on the file for the current buffer +nnoremap pl :write !perlcritic +" Run the current buffer through perltidy +nnoremap pt :%!perltidy -- cgit v1.2.3 From 95e5976068183cf0f5b1601b7b159dfc6fa2cab8 Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Fri, 3 Nov 2017 23:54:52 +1300 Subject: Use long form options for tidy(1) Vim call --- vim/ftplugin/html.vim | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/vim/ftplugin/html.vim b/vim/ftplugin/html.vim index 4d3991f3..3bd1dbdf 100644 --- a/vim/ftplugin/html.vim +++ b/vim/ftplugin/html.vim @@ -1,5 +1,5 @@ -" Run tidy -eq -utf8 on file for the current buffer -nnoremap v :write !tidy -eq -utf8 +" Run `tidy -errors -quiet` for the current buffer +nnoremap c :write !tidy -errors -quiet -utf8 " Make a bare URL into a link to itself function! s:UrlLink() -- cgit v1.2.3 From f1a27833c012d3c0550f2d97839663a5e5ec7171 Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Fri, 3 Nov 2017 23:58:29 +1300 Subject: Improve comments on check/lint/tidy maps --- vim/ftplugin/html.vim | 2 +- vim/ftplugin/perl.vim | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/vim/ftplugin/html.vim b/vim/ftplugin/html.vim index 3bd1dbdf..520e4fe2 100644 --- a/vim/ftplugin/html.vim +++ b/vim/ftplugin/html.vim @@ -1,4 +1,4 @@ -" Run `tidy -errors -quiet` for the current buffer +" Run `tidy -errors -quiet` over buffer nnoremap c :write !tidy -errors -quiet -utf8 " Make a bare URL into a link to itself diff --git a/vim/ftplugin/perl.vim b/vim/ftplugin/perl.vim index eeed2bb7..3355227e 100644 --- a/vim/ftplugin/perl.vim +++ b/vim/ftplugin/perl.vim @@ -1,6 +1,6 @@ -" Run perl -c on file for the current buffer +" Run `perl -c` over buffer nnoremap pc :write !perl -c -" Run perlcritic on the file for the current buffer +" Run `perlcritic` over buffer nnoremap pl :write !perlcritic -" Run the current buffer through perltidy +" Filter buffer through `perltidy` nnoremap pt :%!perltidy -- cgit v1.2.3 From 703c63113d6e089136259ab077422b51f636309a Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Sat, 4 Nov 2017 00:31:04 +1300 Subject: Make all lint/check/tidy maps local and silent That is, apply and to each of them, to make them only apply to the current buffer and to prevent them from echoing the command they're running. --- vim/ftplugin/html.vim | 4 ++-- vim/ftplugin/perl.vim | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/vim/ftplugin/html.vim b/vim/ftplugin/html.vim index 520e4fe2..f16bc8cf 100644 --- a/vim/ftplugin/html.vim +++ b/vim/ftplugin/html.vim @@ -1,5 +1,5 @@ " Run `tidy -errors -quiet` over buffer -nnoremap c :write !tidy -errors -quiet -utf8 +nnoremap c :write !tidy -errors -quiet " Make a bare URL into a link to itself function! s:UrlLink() @@ -16,4 +16,4 @@ function! s:UrlLink() normal! a endfunction -nnoremap r :call UrlLink() +nnoremap r :call UrlLink() diff --git a/vim/ftplugin/perl.vim b/vim/ftplugin/perl.vim index 3355227e..38c3308b 100644 --- a/vim/ftplugin/perl.vim +++ b/vim/ftplugin/perl.vim @@ -1,6 +1,6 @@ " Run `perl -c` over buffer -nnoremap pc :write !perl -c +nnoremap c :write !perl -c " Run `perlcritic` over buffer -nnoremap pl :write !perlcritic +nnoremap l :write !perlcritic " Filter buffer through `perltidy` -nnoremap pt :%!perltidy +nnoremap t :%!perltidy -- cgit v1.2.3 From ccabcefafef457e7d488ef2ee1b27b4b56e03267 Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Sat, 4 Nov 2017 00:32:04 +1300 Subject: Break long lines in check/lint/tidy mappings --- vim/ftplugin/html.vim | 6 ++++-- vim/ftplugin/perl.vim | 11 ++++++++--- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/vim/ftplugin/html.vim b/vim/ftplugin/html.vim index f16bc8cf..a56ae778 100644 --- a/vim/ftplugin/html.vim +++ b/vim/ftplugin/html.vim @@ -1,5 +1,6 @@ " Run `tidy -errors -quiet` over buffer -nnoremap c :write !tidy -errors -quiet +nnoremap c + \ :write !tidy -errors -quiet " Make a bare URL into a link to itself function! s:UrlLink() @@ -16,4 +17,5 @@ function! s:UrlLink() normal! a endfunction -nnoremap r :call UrlLink() +nnoremap r + \ :call UrlLink() diff --git a/vim/ftplugin/perl.vim b/vim/ftplugin/perl.vim index 38c3308b..2ea4676b 100644 --- a/vim/ftplugin/perl.vim +++ b/vim/ftplugin/perl.vim @@ -1,6 +1,11 @@ " Run `perl -c` over buffer -nnoremap c :write !perl -c +nnoremap c + \ :write !perl -c + " Run `perlcritic` over buffer -nnoremap l :write !perlcritic +nnoremap l + \ :write !perlcritic + " Filter buffer through `perltidy` -nnoremap t :%!perltidy +nnoremap t + \ :%!perltidy -- cgit v1.2.3 From 374ceec9af29401a183b3be44f5dc75e096abae9 Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Sat, 4 Nov 2017 00:32:44 +1300 Subject: Add tidy mapping for HTML This mapping mirrors the one for Perl that passes the content of the buffer through a program to tidy it (i.e. not merely check but actively change it). The tidy(1) option chosen here, -quiet, is the bare minimum to make this invocation useful. We would never want the boilerplate it otherwise emits to be in the buffer after a call. Everything else should be applied in a configuration file, which I'll do in a separate feature. --- vim/ftplugin/html.vim | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/vim/ftplugin/html.vim b/vim/ftplugin/html.vim index a56ae778..c756eb80 100644 --- a/vim/ftplugin/html.vim +++ b/vim/ftplugin/html.vim @@ -2,6 +2,10 @@ nnoremap c \ :write !tidy -errors -quiet +" Filter buffer through `tidy` +nnoremap t + \ :%!tidy -quiet + " Make a bare URL into a link to itself function! s:UrlLink() -- cgit v1.2.3 From ad80283eec33af208d6bf24834809e351f54f90f Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Sat, 4 Nov 2017 00:32:58 +1300 Subject: Add check and lint mappings for shell script The commands to use in this case are dependent on the particular shell being used. --- vim/ftplugin/sh.vim | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/vim/ftplugin/sh.vim b/vim/ftplugin/sh.vim index a6dd62eb..c09e4fe8 100644 --- a/vim/ftplugin/sh.vim +++ b/vim/ftplugin/sh.vim @@ -24,3 +24,25 @@ endif if exists('b:is_bash') && executable('han') setlocal keywordprg=han endif + +" Map checker based on shell family +if exists('b:is_bash') && b:is_bash + let b:check = 'bash -n' +elseif exists('b:is_ksh') && b:is_ksh + let b:check = 'ksh -n' +else + let b:check = 'sh -n' +endif +nnoremap c + \ :execute ':write !' . b:check + +" Map linter based on shell family +if exists('b:is_bash') && b:is_bash + let b:lint = 'shellcheck -s bash -' +elseif exists('b:is_ksh') && b:is_ksh + let b:lint = 'shellcheck -s ksh -' +else + let b:lint = 'shellcheck -s sh -' +endif +nnoremap l + \ :execute ':write !' . b:lint -- cgit v1.2.3 From acec3d790704ab65a0c4229f4355e99fafcbdde5 Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Sat, 4 Nov 2017 00:45:41 +1300 Subject: Use underscore as local map leader This should allow me to mentally separate actions specific to a buffer type from actions that apply to buffers in general. It also removes the overlap of l for 'list' toggling and filetype linting. From ":help maplocalleader": > is just like , except that it uses > "maplocalleader" instead of "mapleader". is to be used > for mappings which are local to a buffer. Example: > > :map A oanother line > > In a global plugin should be used and in a filetype plugin > . "mapleader" and "maplocalleader" can be equal. > Although, if you make them different, there is a smaller chance of > mappings from global plugins to clash with mappings for filetype > plugins. For example, you could keep "mapleader" at the default > backslash, and set "maplocalleader" to an underscore. --- vim/config/leader.vim | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 vim/config/leader.vim diff --git a/vim/config/leader.vim b/vim/config/leader.vim new file mode 100644 index 00000000..2d7b98ae --- /dev/null +++ b/vim/config/leader.vim @@ -0,0 +1,3 @@ +" Use different keys for global and local leaders +let mapleader = '\' +let maplocalleader = '_' -- cgit v1.2.3 From 55699ddf44405c16f5ad6fbad6022aaeb4373683 Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Sat, 4 Nov 2017 00:57:28 +1300 Subject: Specify scope of mapleader variables `vint -s` says: vim/config/leader.vim:2:5: Make the scope explicit like `g:mapleader` (see Anti-pattern of vimrc (Scope of identifier)) vim/config/leader.vim:3:5: Make the scope explicit like `g:maplocalleader` (see Anti-pattern of vimrc (Scope of identifier)) This does still seem to work with the prefixes, despite not being the way the documentation specifies the variables. --- vim/config/leader.vim | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/vim/config/leader.vim b/vim/config/leader.vim index 2d7b98ae..9ca8f762 100644 --- a/vim/config/leader.vim +++ b/vim/config/leader.vim @@ -1,3 +1,3 @@ " Use different keys for global and local leaders -let mapleader = '\' -let maplocalleader = '_' +let g:mapleader = '\' +let g:maplocalleader = '_' -- cgit v1.2.3 From 43165b683c25a198f73304cca8b76cb9f1fae2ce Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Sat, 4 Nov 2017 00:59:23 +1300 Subject: Add lint mapping for Vimscript Runs `vint -s`; the -s includes stylistic suggestions. --- vim/ftplugin/vim.vim | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 vim/ftplugin/vim.vim diff --git a/vim/ftplugin/vim.vim b/vim/ftplugin/vim.vim new file mode 100644 index 00000000..e023553e --- /dev/null +++ b/vim/ftplugin/vim.vim @@ -0,0 +1,5 @@ +" Run `vint` over buffer +" /dev/stdin is not optimal here; it's widely implemented, but not POSIX. +" `vint` does not seem to have another way to parse standard input. +nnoremap l + \ :write !vint -s /dev/stdin -- cgit v1.2.3 From 0f5fb5ec1b50a52e245ef0d1b46550091f222c31 Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Sat, 4 Nov 2017 01:02:06 +1300 Subject: Update documentation to reflect ftplugin changes --- README.md | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 028a42fa..c3ffbf44 100644 --- a/README.md +++ b/README.md @@ -342,8 +342,12 @@ The configuration is broken into subfiles in `~/.vim/config/*.vim`, included by extensively commented, mostly because I was reading through it one day and realised I'd forgotten what half of it did. -Plugins are in submodules in `~/.vim/bundle`, loaded using Tim Pope's -[pathogen.vim](https://github.com/tpope/vim-pathogen). +I define a few custom per-filetype rules for stuff I often edit in +`~/.vim/ftplugin`, including some local mappings for checking, linting, and +tidying. + +Third-party plugins are in submodules in `~/.vim/bundle`, loaded using Tim +Pope's [pathogen.vim](https://github.com/tpope/vim-pathogen). Scripts ------- -- cgit v1.2.3