From 4e79b369c397feec786d52946597f45911b65af2 Mon Sep 17 00:00:00 2001 From: Julian Prein Date: Sun, 16 Jul 2023 13:11:14 +0200 Subject: [PATCH] vim:keys: Rewrite mapping functions to use dicts Instead of having a long list of `map` commands, define a dictionary for the mappings and take shortcuts were possible. --- .config/vim/vimrc.d/40-keys.vim | 114 ++++++++++---------------------- 1 file changed, 36 insertions(+), 78 deletions(-) diff --git a/.config/vim/vimrc.d/40-keys.vim b/.config/vim/vimrc.d/40-keys.vim index a238c53..4ba7cef 100644 --- a/.config/vim/vimrc.d/40-keys.vim +++ b/.config/vim/vimrc.d/40-keys.vim @@ -226,28 +226,26 @@ imap Q " Swap movement mappings that act on display lines with the normal ones, making " it easier to navigate long wrapped lines. function! MapWrapMovement() + let l:mappings = { + \ 'j': 'gj', + \ 'k': 'gk', + \ '0': 'g0', + \ '^': 'g^', + \ '$': 'g$', + \ 'gj': 'j', + \ 'gk': 'k', + \ 'g0': '0', + \ 'g^': '^', + \ 'g$': '$', + \ } if &wrap - noremap j gj - noremap k gk - noremap 0 g0 - noremap ^ g^ - noremap $ g$ - noremap gj j - noremap gk k - noremap g0 0 - noremap g^ ^ - noremap g$ $ + for [l:from, l:to] in items(l:mappings) + execute 'noremap ' .. l:from .. ' ' .. l:to + endfor else - noremap j j - noremap k k - noremap 0 0 - noremap ^ ^ - noremap $ $ - noremap gj gj - noremap gk gk - noremap g0 g0 - noremap g^ g^ - noremap g$ g$ + for l:key in keys(l:mappings) + execute 'silent! unmap ' .. l:key + endfor endif endfunction augroup WrapMovementMappings @@ -360,6 +358,10 @@ endfunction vmap j call ExpandVisualSelection(1) vmap k call ExpandVisualSelection(-1) +let g:macro_type_mappings = { + \ '': '_', +\ } + function! s:macro_type() if !exists('s:macro_type') let s:macro_type = 0 @@ -371,68 +373,24 @@ function! s:macro_type() " Disable on InsertLeave au! macro_type InsertLeave * call s:macro_type() - imap _ - imap a A - " {{{ - imap b B - imap c C - imap d D - imap e E - imap f F - imap g G - imap h H - imap i I - imap j J - imap k K - imap l L - imap m M - imap n N - imap o O - imap p P - imap q Q - imap r R - imap s S - imap t T - imap u U - imap v V - imap w W - imap x X - imap y Y - imap z Z - " }}} + for [l:from, l:to] in items(g:macro_type_mappings) + execute 'imap ' .. l:from .. ' ' .. l:to + endfor + + for l:key in "abcdefghijklmnopqrstuvwxyz" + execute 'imap ' .. l:key .. ' ' .. toupper(l:key) + endfor else let s:macro_type = 0 au! macro_type - iunmap - iunmap a - " {{{ - iunmap b - iunmap c - iunmap d - iunmap e - iunmap f - iunmap g - iunmap h - iunmap i - iunmap j - iunmap k - iunmap l - iunmap m - iunmap n - iunmap o - iunmap p - iunmap q - iunmap r - iunmap s - iunmap t - iunmap u - iunmap v - iunmap w - iunmap x - iunmap y - iunmap z - " }}} + for l:key in keys(g:macro_type_mappings) + execute 'iunmap ' .. l:key + endfor + + for l:key in "abcdefghijklmnopqrstuvwxyz" + execute 'iunmap ' .. l:key + endfor endif endfunction