Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 

Repository files navigation

VimrcTipsTricks

A Collection Of Useful Mappings + Hacks

Feel free to submit a PR with an interesting line in your Vimrc. Hopefully this can become a tool for others. Ideally, come here -> search for what you're trying to do -> add relevant line to Vimrc. The mappings you see within these examples are optional. A mapping using X can easily be Y, so long as it doesn't conflict with any existing mapping.      

Statusline Tips -> Click Here

     

  • Editing files via SSH
vim scp://user@myserver[:port]//path/to/file.txt

 

  • Efficient workflow
# return to terminal, vim -> background
<Ctrl-z>

# return to vim from terminal
fg

 

  • Quick substitution
nnoremap <leader>s :%s/

 

  • (neovim): live search substitution
set inccomand=nosplit

 

  • Change in visual selected region
:'<,'>s/\%Vs/k/g

 

  • Remove search highlighting with delete/backspace key
nnoremap <silent> <BS> :nohlsearch<CR>

 

  • Quote words under cursor
nnoremap <leader>" viW<esc>a"<esc>gvo<esc>i"<esc>gvo<esc>3l
nnoremap <leader>' viW<esc>a'<esc>gvo<esc>i'<esc>gvo<esc>3l

 

  • Delete all Trailing space in file
nnoremap <Leader>T :%s/\s\+$//<CR>:let @/=''<CR>:nohlsearch<CR>

 

  • Count j & k as jumps
nnoremap <expr> k (v:count > 1 ? "m'" . v:count : '') . 'gk'
nnoremap <expr> j (v:count > 1 ? "m'" . v:count : '') . 'gj'

 

  • Execute shell commands in buffer
nnoremap Q !!$SHELL <CR>

 

  • Edit & Reload .vimrc
map <leader>ev :e $HOME/.vimrc
map <leader>rv :source ~/.vimrc<CR>

 

  • Grep word under cursor
nnoremap K :execute 'grep!"\b"'.expand('<cword>').'"\b"'<CR>:cw<CR>

 

  • Cursor only active in current window
augroup Cursoractive
    au!
    autocmd VimEnter, WinEnter, BufWinEnter * set local cursorline
    autocmd WinLeave * setlocal nocursorline
augroup END

 

  • Window splits automatically equalized
set equalalways

 

  • Open plugin Github page in browser
" Keybinding for visiting the GitHub page of the plugin defined on the current line
autocmd FileType vim nmap <silent> gp :call OpenPluginHomepage()<CR>

function! OpenPluginHomepage()
  " Get line under cursor
  let l:line = getline('.')

  " Matches for instance Plug 'tpope/surround' -> tpope/surround
  " Greedy match in order to not capture trailing comments
  let l:plugin_name = '\w\+ \([''"]\)\(.\{-}\)\1'
  let l:repository = matchlist(l:line, l:plugin_name)[2]

  " Open the corresponding GitHub homepage with $BROWSER
  " You need to set the BROWSER environment variable in order for this to work
  " For MacOS, you can set the following for opening it in your default
  " browser: 'export BROWSER=open'
  exec '!$BROWSER https://github.com/'.l:repository
endfunction

About

⚙️ A collection of useful mappings + hacks for your Vim config.

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors