From d5b19040fc69f0784f375a7573b3a81decbb2cf5 Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Wed, 6 Jun 2018 15:33:15 +1200 Subject: Refactor vim/vimrc Rearranging order where logical, adjust comments, remove line continuation guard and just join all relevant lines. --- vim/vimrc | 299 ++++++++++++++++++++++++++------------------------------------ 1 file changed, 127 insertions(+), 172 deletions(-) diff --git a/vim/vimrc b/vim/vimrc index c4965d09..db6e7f47 100644 --- a/vim/vimrc +++ b/vim/vimrc @@ -1,9 +1,19 @@ +" Tom Ryder (tejr)'s vimrc: +" +" This file is not truly self-contained; it should run without errors on its +" own without the accompanying plugins to which it refers near the end of this +" file, but you'll get errors for some of the leader maps, for example. + " If we're in compatible mode, ensure that the 'C' option that disallows line -" continuations is stripped out, as they're heavily used in this -" configuration for readability; we'll put it back later -if &compatible - let s:cpoptions_save = &cpoptions - set cpoptions-=C +" continuations is stripped out, as they're heavily used in this configuration +" for readability; we'll put it back later. Yes, this is necessary, for +" example if this file is :sourced while &compatible, or if Vim is invoked +" under its other name--we are out there, we ex(1)ist! + +" Use UTF-8 by default wherever possible +if has('multi_byte') + set encoding=utf-8 + scriptencoding utf-8 endif " Use different keys for global and local leaders @@ -12,6 +22,11 @@ if has('eval') let g:maplocalleader = '_' endif +" Load filetype settings, including filetype plugins and indent settings +if has('autocmd') + filetype plugin indent on +endif + " Let me backspace over pretty much anything set backspace= " Allow backspacing over autoindent @@ -42,12 +57,8 @@ if v:version >= 701 endif " Cycle back and forth through buffers. -nnoremap - \ [b - \ :bprevious -nnoremap - \ ]b - \ :bnext +nnoremap [b :bprevious +nnoremap ]b :bnext " Keep plenty of command and search history, because disk space is cheap set history=2000 @@ -70,20 +81,10 @@ if has('cmdline_info') endif " \d inserts the current local date from date(1) -nnoremap - \ d - \ :read !date -" \D inserts the current UTC date from date(1) -nnoremap - \ D - \ :read !date -u +nnoremap d :read !date -" \m in visual/select mode starts a mail message with the selected lines -vmap m MailMuttSelected -" \m in normal mode starts a mail message with the current line -nmap m MailMuttLine -" \M in normal mode starts a mail message with the whole buffer -nmap M MailMuttBuffer +" \D inserts the current UTC date from date(1) +nnoremap D :read !date -u " Don't try to complete strings from included files, just use the strings in " the open buffers; I'll open the file if I want to complete from it @@ -153,56 +154,14 @@ set shortmess+=x " Don't show whitespace characters or end-of-line characters visually by " default, but make \l toggle between them set nolist -nnoremap - \ l - \ :set list! list? +nnoremap l :set list! list? " Don't show line numbers by default, but \n toggles them set nonumber -nnoremap - \ n - \ :set number! number? - -" Disable most core plugin stuff that I don't use; after/plugin/dist.vim -" clears these variables later -if has('eval') - - " 2html.vim is often useful, so keep that - " matchparen.vim I use constantly - - " I handle versioning plugins manually, and have never used .vba - let g:loaded_getscriptPlugin = 1 - let g:loaded_vimballPlugin = 1 - - " This is what grep, sed, Awk, and Perl are for - let g:loaded_logiPat = 1 - - " ^Z, my dudes - let g:loaded_netrwPlugin = 1 - - " Vim servers? What is this, Emacs? - let g:loaded_rrhelper = 1 - - " System dictionaries plus custom per-machine spell files are fine - let g:loaded_spellfile_plugin = 1 - - " If I want to read a file or a file archived within it I'll decompress or - " unarchive it myself; a text editor should not do this - let g:loaded_gzip = 1 - let g:loaded_tarPlugin = 1 - let g:loaded_zipPlugin = 1 - -endif - -" Load plugins for file types -if has('autocmd') - filetype plugin indent on -endif +nnoremap n :set number! number? " Bind \p to show filetype -nnoremap - \ p - \ :set filetype? +nnoremap p :set filetype? " Use UTF-8 by default wherever possible if has('multi_byte') @@ -229,12 +188,8 @@ set nomodeline " I really like ZZ and ZQ, so I wrote a couple more mappings; ZW forces a " write of the current buffer, but doesn't quit, and ZA forces a write of all " buffers but doesn't quit -nnoremap - \ ZW - \ :write! -nnoremap - \ ZA - \ :wall! +nnoremap ZW :write! +nnoremap ZA :wall! " Don't assume a number with a leading zero is octal; it's far more likely a " zero-padded decimal, so increment and decrement with ^A and ^X on that basis @@ -244,55 +199,12 @@ set nrformats-=octal " leaders when joining lines silent! set formatoptions+=j -" Show the current formatoptions at a glance -nnoremap - \ f - \ :setlocal formatoptions? - -" Use toggle_option_flag.vim plugin to bind quick toggle actions for some -" 'formatoptions' flags -if has('user_commands') - - " a: Reformat paragraphs to 'textwidth' on all insert or delete operations - nnoremap - \ a - \ :ToggleOptionFlagLocal formatoptions a - - " c: Reformat comments to 'textwidth' - nnoremap - \ c - \ :ToggleOptionFlagLocal formatoptions c - - " j: Delete comment leaders when joining lines - nnoremap - \ j - \ :ToggleOptionFlagLocal formatoptions j - - " t: Reformat non-comment text to 'textwidth' - nnoremap - \ t - \ :ToggleOptionFlagLocal formatoptions t - -endif +" \f shows the current formatoptions at a glance +nnoremap f :setlocal formatoptions? " Match all forms of brackets in pairs (including angle brackets) set matchpairs+=<:> -" Fedora's default environment adds a few auto commands that I don't like, -" including the 'return to previous position in buffer' one; fortunately -" they're nice enough to group the commands, so I can just clear them -if has('autocmd') - augroup fedora - autocmd! - augroup END -endif - -" Add the packaged version of matchit.vim included in the distribution, if -" possible; plugin/macros.vim loads this for older Vims -if has('packages') - silent! packadd! matchit -endif - " Change and delete with C and D both cut off the remainder of the line from " the cursor, but Y yanks the whole line, which is inconsistent (and can be " done with yy anyway); this fixes it so it only yanks the rest of the line @@ -312,22 +224,16 @@ if has('extra_search') " Searching as I enter my pattern, \i toggles this set incsearch - nnoremap - \ i - \ :set incsearch! incsearch? + nnoremap i :set incsearch! incsearch? " Highlight search results, \h toggles this set hlsearch - nnoremap - \ h - \ :set hlsearch! hlsearch? + nnoremap h :set hlsearch! hlsearch? " Pressing ^L will clear highlighting until the next search-related " operation; quite good because the highlighting gets distracting after " you've found what you wanted - nnoremap - \ - \ :nohlsearch + nnoremap :nohlsearch endif @@ -336,34 +242,24 @@ if has('spell') " Don't check spelling by default, but bind \s to toggle this set nospell - nnoremap - \ s - \ :setlocal spell! spell? + nnoremap s :setlocal spell! spell? " Use New Zealand English for spelling by default (it's almost identical " to British English), but bind \u to switch to US English and \z to " switch back set spelllang=en_nz - nnoremap - \ u - \ :setlocal spelllang=en_us spelllang? - nnoremap - \ z - \ :setlocal spelllang=en_nz spelllang? + nnoremap u :setlocal spelllang=en_us spelllang? + nnoremap z :setlocal spelllang=en_nz spelllang? endif " Preserve the flags for a pattern when repeating a substitution with &; I " don't really understand why this isn't a default, but there it is -nnoremap - \ & - \ :&& +nnoremap & :&& " Same again for visual mode; we use vnoremap rather than xnoremap to stay " compatible with old Vims without doing :execute dances -vnoremap - \ & - \ :&& +vnoremap & :&& " Default to no swap files at all, in a way that even ancient/tiny Vims will " understand; the auto_cache_dirs.vim plugin will take care of re-enabling @@ -375,9 +271,11 @@ set noswapfile if has('autocmd') augroup dotfiles_swap_skip autocmd! - autocmd BufNewFile,BufReadPre - \ /tmp/*,$TMPDIR/*,$TMP/*,$TEMP/*,*/shm/* - \ setlocal noswapfile + autocmd BufNewFile,BufReadPre /tmp/* setlocal noswapfile + autocmd BufNewFile,BufReadPre $TMPDIR/* setlocal noswapfile + autocmd BufNewFile,BufReadPre $TMP/* setlocal noswapfile + autocmd BufNewFile,BufReadPre $TEMP/* setlocal noswapfile + autocmd BufNewFile,BufReadPre */shm/* setlocal noswapfile augroup END endif @@ -451,9 +349,11 @@ if has('persistent_undo') if has('autocmd') augroup dotfiles_undo_skip autocmd! - autocmd BufWritePre - \ /tmp/*,$TMPDIR/*,$TMP/*,$TEMP/*,*/shm/* - \ setlocal noundofile + autocmd BufWritePre /tmp/* setlocal noundofile + autocmd BufWritePre $TMPDIR/* setlocal noundofile + autocmd BufWritePre $TMP/* setlocal noundofile + autocmd BufWritePre $TEMP/* setlocal noundofile + autocmd BufWritePre */shm/* setlocal noundofile augroup END endif @@ -465,9 +365,11 @@ endif if has('viminfo') && has('autocmd') augroup dotfiles_viminfo_skip autocmd! - autocmd BufNewFile,BufReadPre - \ /tmp/*,$TMPDIR/*,$TMP/*,$TEMP/*,*/shm/* - \ setlocal viminfo= + autocmd BufNewFile,BufReadPre /tmp/* setlocal viminfo= + autocmd BufNewFile,BufReadPre $TMPDIR/* setlocal viminfo= + autocmd BufNewFile,BufReadPre $TMP/* setlocal viminfo= + autocmd BufNewFile,BufReadPre $TEMP/* setlocal viminfo= + autocmd BufNewFile,BufReadPre */shm/* setlocal viminfo= augroup END endif @@ -498,13 +400,6 @@ set shiftround " despite the noble Steve Losh's exhortations set nojoinspaces -" \x strips trailing whitespace via a custom plugin -nmap x StripTrailingWhitespace - -" Insert blank lines above and below via a custom plugin -nmap [ PutBlankLinesAbove -nmap ] PutBlankLinesBelow - " Configuration for window features if has('windows') @@ -536,9 +431,7 @@ endif " Don't wrap by default, but use \w to toggle it on or off quickly set nowrap -nnoremap - \ w - \ :set wrap! wrap? +nnoremap w :set wrap! wrap? " When wrapping text, if a line is so long that not all of it can be shown on " the screen, show as much as possible anyway; by default Vim fills the left @@ -570,18 +463,80 @@ if has('linebreak') set breakindent endif - " \b toggles copy-pasteable linebreak settings - nmap b CopyLinebreakToggle +endif + +" Disable most core plugin stuff that I don't use; after/plugin/dist.vim +" clears these variables later +if has('eval') + + " 2html.vim is often useful, so keep that + " matchparen.vim I use constantly + + " I handle versioning plugins manually, and have never used .vba + let g:loaded_getscriptPlugin = 1 + let g:loaded_vimballPlugin = 1 + + " This is what grep, sed, Awk, and Perl are for + let g:loaded_logiPat = 1 + + " ^Z, my dudes + let g:loaded_netrwPlugin = 1 + + " Vim servers? What is this, Emacs? + let g:loaded_rrhelper = 1 + + " System dictionaries plus custom per-machine spell files are fine + let g:loaded_spellfile_plugin = 1 + + " If I want to read a file or a file archived within it I'll decompress or + " unarchive it myself; a text editor should not do this + let g:loaded_gzip = 1 + let g:loaded_tarPlugin = 1 + let g:loaded_zipPlugin = 1 + +endif + +" Insert blank lines above and below via my custom unimpaired.vim rip +nmap [ PutBlankLinesAbove +nmap ] PutBlankLinesBelow + +" \b toggles copy-pasteable linebreak settings +nmap b CopyLinebreakToggle + +" \m in visual/select mode starts a mail message with the selected lines +vmap m MailMuttSelected +" \m in normal mode starts a mail message with the current line +nmap m MailMuttLine +" \M in normal mode starts a mail message with the whole buffer +nmap M MailMuttBuffer + +" \x strips trailing whitespace via a custom plugin +nmap x StripTrailingWhitespace + +" Use toggle_option_flag.vim plugin to bind quick toggle actions for some +" 'formatoptions' flags +if has('user_commands') + + " \a: Reformat paragraphs to 'textwidth' on all insert or delete operations + nnoremap a :ToggleOptionFlagLocal formatoptions a + + " \c: Reformat comments to 'textwidth' + nnoremap c :ToggleOptionFlagLocal formatoptions c + + " \j: Delete comment leaders when joining lines + nnoremap j :ToggleOptionFlagLocal formatoptions j + + " \t: Reformat non-comment text to 'textwidth' + nnoremap t :ToggleOptionFlagLocal formatoptions t endif +" Add the packaged version of matchit.vim included in the distribution, if +" possible; plugin/macros.vim loads this for older Vims +if has('packages') + silent! packadd! matchit +endif + " Source all .vim files from ~/.vim/config, which may override any of the " above runtime! config/*.vim - -" If we're in compatible mode, put 'cpoptions' back the way we found it at the -" start of this configuration, even though it's the current year -if exists('s:cpoptions_save') - let &cpoptions = s:cpoptions_save - unlet s:cpoptions_save -endif -- cgit v1.2.3