"{ 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 in insert mode inoremap jk " Turn the word under cursor to upper case inoremap viwUea " Turn the current word into title case inoremap b~lea " Paste non-linewise text above or below current cursor, " see https://stackoverflow.com/a/1346777/6064933 nnoremap p m`op`` nnoremap P m`Op`` " Shortcut for faster save and quit nnoremap w :update " Saves the file if modified and quit nnoremap q :x " Quit all opened buffers nnoremap Q :qa " Navigation in the location and quickfix list nnoremap [l :lpreviouszv nnoremap ]l :lnextzv nnoremap [L :lfirstzv nnoremap ]L :llastzv nnoremap [q :cpreviouszv nnoremap ]q :cnextzv nnoremap [Q :cfirstzv nnoremap ]Q :clastzv " Close location list or quickfix list if they are present, " see https://superuser.com/q/355325/736190 nnoremap \x :windo lclose cclose " Close a buffer and switching to another buffer, do not close the " window, see https://stackoverflow.com/q/4465095/6064933 nnoremap \d :bprevious bdelete # " Insert a blank line below or above current line (do not move the cursor), " see https://stackoverflow.com/a/16136133/6064933 nnoremap oo printf('m`%so``', v:count1) nnoremap OO printf('m`%sO``', v:count1) " nnoremap oo @='m`o``' " nnoremap OO @='m`O``' " the following two mappings work, but if you change double quote to single, it " will not work " nnoremap oo @="m`o\Esc>``" " nnoremap oo @="m`o\e``" " Insert a space after current character nnoremap ah " 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 j (v:count == 0 ? 'gj' : 'j') nnoremap 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 % " 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 h nnoremap l nnoremap j nnoremap 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 " When completion menu is shown, use to select an item and do not add an " annoying newline. Otherwise, is what it is. For more info , see " https://superuser.com/a/941082/736190 and " https://unix.stackexchange.com/q/162528/221410 inoremap ((pumvisible())?("\"):("\")) " Use to close auto-completion menu inoremap ((pumvisible())?("\"):("\")) " Tab-complete, see https://vi.stackexchange.com/q/19675/15292. inoremap pumvisible() ? "\" : "\" inoremap pumvisible() ? "\" : "\" " Edit and reload init.vim quickly nnoremap ev :tabnew $MYVIMRC tcd %:h nnoremap sv :silent update $MYVIMRC source $MYVIMRC \ echomsg "Nvim config successfully reloaded!" " Reselect the text that has just been pasted, see also https://stackoverflow.com/a/4317090/6064933. nnoremap v printf('`[%s`]', getregtype()[0]) " Search in selected region xnoremap / :call feedkeys('/\%>'.(line("'<")-1).'l\%<'.(line("'>")+1)."l") " Find and replace (like Sublime Text 3) nnoremap :%s/ xnoremap :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 cd :lcd %:p:h:pwd " Use Esc to quit builtin terminal tnoremap " Toggle spell checking (autosave does not play well with z=, so we disable it " when we are doing spell checking) nnoremap :set spell! :AutoSaveToggle inoremap :set spell! :AutoSaveToggle " Decrease indent level in insert mode with shift+tab inoremap < :call utils#StripTrailingWhitespaces() " check the syntax group of current cursor position nnoremap st :call utils#SynGroup() " Clear highlighting if maparg('', 'n') ==# '' nnoremap :nohlsearch=has('diff')?'diffupdate':'' endif " Copy entire buffer. nnoremap y :%y " Toggle cursor column nnoremap cl :call utils#ToggleCursorCol() " Move current line up and down nnoremap call utils#SwitchLine(line('.'), 'up') nnoremap call utils#SwitchLine(line('.'), 'down') " Move current visual-line selection up and down xnoremap :call utils#MoveSelection('up') xnoremap :call utils#MoveSelection('down') " Replace visual selection with text in register, but not contaminate the " register, see also https://stackoverflow.com/q/10723700/6064933. xnoremap p "_cp nnoremap gb :call GoToBuffer(v:count, 'forward') nnoremap gB :call GoToBuffer(v:count, 'backward') 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 h nnoremap l nnoremap k nnoremap j xnoremap iu :call URLTextObj() onoremap iu :call URLTextObj() function! s:URLTextObj() abort if match(&runtimepath, 'vim-highlighturl') != -1 " Note that we use https://github.com/itchyny/vim-highlighturl to get the URL pattern. let url_pattern = highlighturl#default_pattern() else let url_pattern = expand('') " Since expand('') also works for normal words, we need to check if " this is really URL using heuristics, e.g., URL length. if len(url_pattern) <= 10 return endif endif " We need to find all possible URL on this line and their start, end idx. " Then find where current cursor is, and decide if cursor is on one of the " URLs. let line_text = getline('.') let url_infos = [] let [_url, _idx_start, _idx_end] = matchstrpos(line_text, url_pattern) while _url !=# '' let url_infos += [[_url, _idx_start+1, _idx_end]] let [_url, _idx_start, _idx_end] = matchstrpos(line_text, url_pattern, _idx_end) endwhile " echo url_infos " If no URL is found, do nothing. if len(url_infos) == 0 return endif let [start_col, end_col] = [-1, -1] " If URL is found, find if cursor is on it. let [buf_num, cur_row, cur_col] = getcurpos()[0:2] for url_info in url_infos " echo url_info let [_url, _idx_start, _idx_end] = url_info if cur_col >= _idx_start && cur_col <= _idx_end let start_col = _idx_start let end_col = _idx_end break endif endfor " Cursor is not on a URL, do nothing. if start_col == -1 return endif " Now set the '< and '> mark call setpos("'<", [buf_num, cur_row, start_col, 0]) call setpos("'>", [buf_num, cur_row, end_col, 0]) normal! gv endfunction "}