aboutsummaryrefslogtreecommitdiff
path: root/plugin/insert_cancel.vim
diff options
context:
space:
mode:
authorTom Ryder <tom@sanctum.geek.nz>2018-07-02 10:51:31 +1200
committerTom Ryder <tom@sanctum.geek.nz>2018-07-02 10:58:35 +1200
commitec0551228a91752d2be5f9d771e2cb5a624e6ba4 (patch)
treea8b9855a8378fca5d37f613aefa766a7a3702589 /plugin/insert_cancel.vim
downloadvim-insert-cancel-ec0551228a91752d2be5f9d771e2cb5a624e6ba4.tar.gz
vim-insert-cancel-ec0551228a91752d2be5f9d771e2cb5a624e6ba4.zip
First commit
Diffstat (limited to 'plugin/insert_cancel.vim')
-rw-r--r--plugin/insert_cancel.vim35
1 files changed, 35 insertions, 0 deletions
diff --git a/plugin/insert_cancel.vim b/plugin/insert_cancel.vim
new file mode 100644
index 0000000..c5af7e2
--- /dev/null
+++ b/plugin/insert_cancel.vim
@@ -0,0 +1,35 @@
+"
+" insert_cancel.vim: Cancel the current insert operation by undoing the last
+" change upon insert exit, if we made a change; intended for remapping
+" insert-mode Ctrl-C.
+"
+" Author: Tom Ryder <tom@sanctum.geek.nz>
+" License: Same as Vim itself
+"
+if exists('g:loaded_insert_cancel') || &compatible
+ finish
+endif
+if v:version < 600
+ finish
+endif
+let g:loaded_insert_cancel = 1
+
+" InsertEnter is only available from Vim 7
+if has('autocmd') && v:version > 700
+ augroup insert_cancel
+ autocmd!
+ autocmd InsertEnter *
+ \ let s:changenr = changenr()
+ augroup END
+endif
+
+" Try to figure out whether we made a change to undo, undo it if so
+function! s:InsertCancel()
+ if &modified && (!exists('s:changenr') || changenr() > s:changenr)
+ silent undo
+ endif
+endfunction
+
+" Provide plugin mapping
+inoremap <silent> <Plug>InsertCancel
+ \ <C-C>:<C-U>call <SID>InsertCancel()<CR>