aboutsummaryrefslogtreecommitdiff
path: root/vim/plugin
diff options
context:
space:
mode:
Diffstat (limited to 'vim/plugin')
-rw-r--r--vim/plugin/big_file.vim (renamed from vim/plugin/bigfile.vim)25
-rw-r--r--vim/plugin/command_typos.vim19
-rw-r--r--vim/plugin/copy_linebreak.vim36
-rw-r--r--vim/plugin/fixed_join.vim29
-rw-r--r--vim/plugin/strip_trailing_whitespace.vim62
-rw-r--r--vim/plugin/toggle_option_flag.vim44
6 files changed, 202 insertions, 13 deletions
diff --git a/vim/plugin/bigfile.vim b/vim/plugin/big_file.vim
index fece3d9b..ec30158a 100644
--- a/vim/plugin/bigfile.vim
+++ b/vim/plugin/big_file.vim
@@ -1,26 +1,25 @@
"
-" bigfile.vim: When opening a large file, take some measures to keep things
+" big_file.vim: When opening a large file, take some measures to keep things
" loading quickly.
"
" Author: Tom Ryder <tom@sanctum.geek.nz>
-" Copyright: 2017
" License: Same as Vim itself
"
if has('eval') && has('autocmd')
" Default threshold is 10 MiB
- if !exists('g:bigfile_size')
- let g:bigfile_size = 10 * 1024 * 1024
+ if !exists('g:big_file_size')
+ let g:big_file_size = 10 * 1024 * 1024
endif
" Default to leaving syntax highlighting off
- if !exists('g:bigfile_syntax')
- let g:bigfile_syntax = 0
+ if !exists('g:big_file_syntax')
+ let g:big_file_syntax = 0
endif
" Cut 'synmaxcol' down to this or smaller for big files
- if !exists('g:bigfile_size_synmaxcol')
- let g:bigfile_size_synmaxcol = 256
+ if !exists('g:big_file_synmaxcol')
+ let g:big_file_synmaxcol = 256
endif
" Declare function for turning off slow options
@@ -40,21 +39,21 @@ if has('eval') && has('autocmd')
endif
" Limit the number of columns of syntax highlighting
- if exists('&synmaxcol') && &synmaxcol > g:bigfile_size_synmaxcol
- execute 'setlocal synmaxcol=' . g:bigfile_size_synmaxcol
+ if exists('&synmaxcol') && &synmaxcol > g:big_file_synmaxcol
+ execute 'setlocal synmaxcol=' . g:big_file_synmaxcol
endif
" Disable syntax highlighting if configured to do so
- if !g:bigfile_syntax
+ if !g:big_file_syntax
setlocal syntax=OFF
endif
endfunction
" Define autocmd for calling to check filesize
- augroup bigfile_options_bufreadpre
+ augroup big_file_options_bufreadpre
autocmd!
- autocmd BufReadPre * call s:BigFileOptions(expand('<afile>'), g:bigfile_size)
+ autocmd BufReadPre * call s:BigFileOptions(expand('<afile>'), g:big_file_size)
augroup end
endif
diff --git a/vim/plugin/command_typos.vim b/vim/plugin/command_typos.vim
new file mode 100644
index 00000000..32d194fb
--- /dev/null
+++ b/vim/plugin/command_typos.vim
@@ -0,0 +1,19 @@
+"
+" Tolerate typos like :Wq, :Q, or :Qa and do what I mean, including any
+" arguments or modifiers; I fat-finger these commands a lot because I type
+" them so rapidly, and they don't correspond to any other commands I use
+"
+" Author: Tom Ryder <tom@sanctum.geek.nz>
+" License: Same as Vim itself
+"
+if has('eval') && has('user_commands')
+ command! -bang -complete=file -nargs=? E e<bang> <args>
+ command! -bang -complete=file -nargs=? W w<bang> <args>
+ command! -bang -complete=file -nargs=? WQ wq<bang> <args>
+ command! -bang -complete=file -nargs=? Wq wq<bang> <args>
+ command! -bang Q q<bang>
+ command! -bang Qa qa<bang>
+ command! -bang QA qa<bang>
+ command! -bang Wa wa<bang>
+ command! -bang WA wa<bang>
+endif
diff --git a/vim/plugin/copy_linebreak.vim b/vim/plugin/copy_linebreak.vim
new file mode 100644
index 00000000..1dc537d4
--- /dev/null
+++ b/vim/plugin/copy_linebreak.vim
@@ -0,0 +1,36 @@
+"
+" Bind a user-defined key sequence to turn off linebreak and toggle the
+" showbreak characters and breakindent mode on and off, for convenience of
+" copying multiple lines from terminal emulators.
+"
+" Author: Tom Ryder <tom@sanctum.geek.nz>
+" License: Same as Vim itself
+"
+if has('eval')
+
+ " Define function
+ function! s:CopyLinebreak()
+
+ " If linebreak is on, turn it off
+ if &l:linebreak
+ setlocal nolinebreak linebreak?
+ setlocal showbreak=
+ if exists('&breakindent')
+ setlocal nobreakindent
+ endif
+
+ " If it's off, turn it on
+ else
+ setlocal linebreak linebreak?
+ setlocal showbreak<
+ if exists('&breakindent')
+ setlocal breakindent
+ endif
+ endif
+
+ endfunction
+
+ " Provide mapping proxy to the function just defined
+ noremap <Plug>CopyLinebreak
+ \ :<C-U>call <SID>CopyLinebreak()<CR>
+endif
diff --git a/vim/plugin/fixed_join.vim b/vim/plugin/fixed_join.vim
new file mode 100644
index 00000000..c002f667
--- /dev/null
+++ b/vim/plugin/fixed_join.vim
@@ -0,0 +1,29 @@
+"
+" User-defined key mapping to keep cursor in place when joining lines in
+" normal mode
+"
+" Author: Tom Ryder <tom@sanctum.geek.nz>
+" License: Same as Vim itself
+"
+if has('eval')
+
+ " Declare function
+ function! s:FixedJoin()
+
+ " Save current cursor position
+ let l:lc = line('.')
+ let l:cc = col('.')
+
+ " Build and execute join command
+ let l:command = '.,+' . v:count1 . 'join'
+ execute l:command
+
+ " Restore cursor position
+ call cursor(l:lc, l:cc)
+
+ endfunction
+
+ " Create mapping proxy to the function just defined
+ noremap <Plug>FixedJoin
+ \ :<C-U>call <SID>FixedJoin()<CR>
+endif
diff --git a/vim/plugin/strip_trailing_whitespace.vim b/vim/plugin/strip_trailing_whitespace.vim
new file mode 100644
index 00000000..17fff33f
--- /dev/null
+++ b/vim/plugin/strip_trailing_whitespace.vim
@@ -0,0 +1,62 @@
+"
+" User-defined key mapping to strip trailing whitespace in the whole document
+"
+" Author: Tom Ryder <tom@sanctum.geek.nz>
+" License: Same as Vim itself
+"
+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
+
+ " Create mapping proxy to the function just defined
+ noremap <Plug>StripTrailingWhitespace
+ \ :<C-U>call <SID>StripTrailingWhitespace()<CR>
+endif
diff --git a/vim/plugin/toggle_option_flag.vim b/vim/plugin/toggle_option_flag.vim
new file mode 100644
index 00000000..10b4fe7a
--- /dev/null
+++ b/vim/plugin/toggle_option_flag.vim
@@ -0,0 +1,44 @@
+"
+" toggle_option_flag.vim: Provide commands to toggle flags in single-char
+" grouped options like 'formatoptions', 'shortmess', 'complete' etc.
+"
+" This will fail hilariously if you try to set e.g. 'switchbuf' with it!
+"
+" Author: Tom Ryder <tom@sanctum.geek.nz>
+" License: Same as Vim itself
+"
+if has('eval') && has('user_commands')
+
+ " Internal function to do the toggling
+ function! s:Toggle(option, flag, local)
+
+ " Check for weird options, we don't want to eval() anything funny
+ if a:option =~# '[^a-z]'
+ echoerr 'Illegal option name'
+ return
+ endif
+
+ " Weird flags, too; should be a single inoffensive char
+ if a:flag !~# '^[a-z0-9.]$'
+ echoerr 'Illegal flag'
+ return
+ endif
+
+ " Choose which set command to use
+ let l:set = a:local ? 'setlocal' : 'set'
+
+ " Use eval() to assign -= or += to l:op for the option toggle
+ " (I couldn't get {curly braces} indirection to work)
+ let l:op = ''
+ execute 'let l:op = &'.a:option.' =~# a:flag ? "-=" : "+="'
+
+ " Use eval() to perform the option toggle and then print the value
+ execute l:set . ' ' . a:option . l:op . a:flag . ' ' . a:option . '?'
+
+ endfunction
+
+ " User commands wrapping around calls to the above function
+ command! -nargs=+ ToggleOptionFlag :call <SID>Toggle(<f-args>, 0)
+ command! -nargs=+ ToggleOptionFlagLocal :call <SID>Toggle(<f-args>, 1)
+
+endif