diff options
Diffstat (limited to 'core/mappings.vim')
| -rw-r--r-- | core/mappings.vim | 213 |
1 files changed, 213 insertions, 0 deletions
diff --git a/core/mappings.vim b/core/mappings.vim new file mode 100644 index 0000000..26bb3f3 --- /dev/null +++ b/core/mappings.vim @@ -0,0 +1,213 @@ +"{ Custom key mappings +" Save key strokes (now we do not need to press shift to enter command mode). +" Vim-sneak has also mapped `;`, so using the below mapping will break the map +" used by vim-sneak +nnoremap ; : +xnoremap ; : + +" Quicker way to open command window +nnoremap q; q: + +" Quicker <Esc> in insert mode +inoremap jk <Esc> + +" Turn the word under cursor to upper case +inoremap <c-u> <Esc>viwUea + +" Turn the current word into title case +inoremap <c-t> <Esc>b~lea + +" Paste non-linewise text above or below current cursor, +" see https://stackoverflow.com/a/1346777/6064933 +nnoremap <leader>p m`o<ESC>p`` +nnoremap <leader>P m`O<ESC>p`` + +" Shortcut for faster save and quit +nnoremap <silent> <leader>w :<C-U>update<CR> +" Saves the file if modified and quit +nnoremap <silent> <leader>q :<C-U>x<CR> +" Quit all opened buffers +nnoremap <silent> <leader>Q :<C-U>qa<CR> + +" Navigation in the location and quickfix list +nnoremap <silent> [l :<C-U>lprevious<CR>zv +nnoremap <silent> ]l :<C-U>lnext<CR>zv +nnoremap <silent> [L :<C-U>lfirst<CR>zv +nnoremap <silent> ]L :<C-U>llast<CR>zv +nnoremap <silent> [q :<C-U>cprevious<CR>zv +nnoremap <silent> ]q :<C-U>cnext<CR>zv +nnoremap <silent> [Q :<C-U>cfirst<CR>zv +nnoremap <silent> ]Q :<C-U>clast<CR>zv + +" Close location list or quickfix list if they are present, +" see https://superuser.com/q/355325/736190 +nnoremap<silent> \x :<C-U>windo lclose <bar> cclose<CR> + +" Close a buffer and switching to another buffer, do not close the +" window, see https://stackoverflow.com/q/4465095/6064933 +nnoremap <silent> \d :<C-U>bprevious <bar> bdelete #<CR> + +" Insert a blank line below or above current line (do not move the cursor), +" see https://stackoverflow.com/a/16136133/6064933 +nnoremap <expr> oo printf('m`%so<ESC>``', v:count1) +nnoremap <expr> OO printf('m`%sO<ESC>``', v:count1) + +" nnoremap oo @='m`o<c-v><Esc>``'<cr> +" nnoremap OO @='m`O<c-v><Esc>``'<cr> + +" the following two mappings work, but if you change double quote to single, it +" will not work +" nnoremap oo @="m`o\<lt>Esc>``"<cr> +" nnoremap oo @="m`o\e``"<cr> + +" Insert a space after current character +nnoremap <Space><Space> a<Space><ESC>h + +" Yank from current cursor position to the end of the line (make it +" consistent with the behavior of D, C) +nnoremap Y y$ + +" Move the cursor based on physical lines, not the actual lines. +nnoremap <expr> j (v:count == 0 ? 'gj' : 'j') +nnoremap <expr> k (v:count == 0 ? 'gk' : 'k') +nnoremap ^ g^ +nnoremap 0 g0 + +" Do not include white space characters when using $ in visual mode, +" see https://vi.stackexchange.com/q/12607/15292 +xnoremap $ g_ + +" Jump to matching pairs easily in normal mode +nnoremap <Tab> % + +" Go to start or end of line easier +nnoremap H ^ +xnoremap H ^ +nnoremap L g_ +xnoremap L g_ + +" Fast window switching, inspiration from +" https://stackoverflow.com/a/4373470/6064933 +nnoremap <M-left> <C-w>h +nnoremap <M-right> <C-w>l +nnoremap <M-down> <C-w>j +nnoremap <M-up> <C-w>k + +" Continuous visual shifting (does not exit Visual mode), `gv` means +" to reselect previous visual area, see https://superuser.com/q/310417/736190 +xnoremap < <gv +xnoremap > >gv + +" When completion menu is shown, use <cr> to select an item and do not add an +" annoying newline. Otherwise, <enter> is what it is. For more info , see +" https://superuser.com/a/941082/736190 and +" https://unix.stackexchange.com/q/162528/221410 +inoremap <expr> <cr> ((pumvisible())?("\<C-Y>"):("\<cr>")) +" Use <esc> to close auto-completion menu +inoremap <expr> <esc> ((pumvisible())?("\<C-e>"):("\<esc>")) + +" Tab-complete, see https://vi.stackexchange.com/q/19675/15292. +inoremap <expr> <tab> pumvisible() ? "\<c-n>" : "\<tab>" +inoremap <expr> <s-tab> pumvisible() ? "\<c-p>" : "\<s-tab>" + +" Edit and reload init.vim quickly +nnoremap <silent> <leader>ev :<C-U>tabnew $MYVIMRC <bar> tcd %:h<cr> +nnoremap <silent> <leader>sv :<C-U>silent update $MYVIMRC <bar> source $MYVIMRC <bar> + \ echomsg "Nvim config successfully reloaded!"<cr> + +" Reselect the text that has just been pasted, see also https://stackoverflow.com/a/4317090/6064933. +nnoremap <expr> <leader>v printf('`[%s`]', getregtype()[0]) + +" Search in selected region +vnoremap / :<C-U>call feedkeys('/\%>'.(line("'<")-1).'l\%<'.(line("'>")+1)."l")<CR> + +" Find and replace (like Sublime Text 3) +nnoremap <C-H> :%s/ +xnoremap <C-H> :s/ + +" Change current working directory locally and print cwd after that, +" see https://vim.fandom.com/wiki/Set_working_directory_to_the_current_file +nnoremap <silent> <leader>cd :<C-U>lcd %:p:h<CR>:pwd<CR> + +" Use Esc to quit builtin terminal +tnoremap <ESC> <C-\><C-n> + +" Toggle spell checking (autosave does not play well with z=, so we disable it +" when we are doing spell checking) +nnoremap <silent> <F11> :<C-U>set spell! <bar> :AutoSaveToggle<cr> +inoremap <silent> <F11> <C-O>:<C-U>set spell! <bar> :AutoSaveToggle<cr> + +" Decrease indent level in insert mode with shift+tab +inoremap <S-Tab> <ESC><<i + +" Change text without putting it into the vim register, +" see https://stackoverflow.com/q/54255/6064933 +nnoremap c "_c +nnoremap C "_C +nnoremap cc "_cc +xnoremap c "_c + +" Remove trailing whitespace characters +nnoremap <silent> <leader><Space> :<C-U>call utils#StripTrailingWhitespaces()<CR> + +" check the syntax group of current cursor position +nnoremap <silent> <leader>st :<C-U>call utils#SynGroup()<CR> + +" Clear highlighting +if maparg('<C-L>', 'n') ==# '' + nnoremap <silent> <C-L> :<C-U>nohlsearch<C-R>=has('diff')?'<Bar>diffupdate':''<CR><CR><C-L> +endif + +" Copy entire buffer. +nnoremap <silent> <leader>y :<C-U>%y<CR> + +" Toggle cursor column +nnoremap <silent> <leader>cl :<C-U>call utils#ToggleCursorCol()<CR> + +" Move current line up and down +nnoremap <silent> <A-k> <Cmd>call utils#SwitchLine(line('.'), 'up')<CR> +nnoremap <silent> <A-j> <Cmd>call utils#SwitchLine(line('.'), 'down')<CR> + +" Move current visual-line selection up and down +xnoremap <silent> <A-k> :<C-U>call utils#MoveSelection('up')<CR> +xnoremap <silent> <A-j> :<C-U>call utils#MoveSelection('down')<CR> + +" Replace visual selection with text in register, but not contaminate the +" register, see also https://stackoverflow.com/q/10723700/6064933. +xnoremap p "_c<ESC>p + +nnoremap <silent> gb :<C-U>call <SID>GoToBuffer(v:count, 'forward')<CR> +nnoremap <silent> gB :<C-U>call <SID>GoToBuffer(v:count, 'backward')<CR> + +function! s:GoToBuffer(count, direction) abort + if a:count == 0 + if a:direction ==# 'forward' + bnext + elseif a:direction ==# 'backward' + bprevious + else + echoerr 'Bad argument ' a:direction + endif + return + endif + " Check the validity of buffer number. + if index(s:GetBufNums(), a:count) == -1 + echohl WarningMsg | echomsg 'Invalid bufnr: ' a:count | echohl None + return + endif + + " Do not use {count} for gB (it is less useful) + if a:direction ==# 'forward' + silent execute('buffer' . a:count) + endif +endfunction + +function! s:GetBufNums() abort + return map(copy(getbufinfo({'buflisted':1})), 'v:val.bufnr') +endfunction + +nnoremap <Left> <C-W>h +nnoremap <Right> <C-W>l +nnoremap <Up> <C-W>k +nnoremap <Down> <C-W>j +"} |
