DEV Community

Discussion on: What's your favorite Vim trick?

Collapse
 
voyeg3r profile image
Sérgio Araújo • Edited

I have many tricks on at my "init.vim"

"Make search faster
:nnoremap <space> /

"Alternate "nonumber | number | relativenumber"
:nnoremap <C-n> :let [&nu, &rnu] = [!&rnu, &nu+&rnu==1]<cr>

" allows me to use a smarter cgn
nnoremap c* *<c-o>cgn
nnoremap c# #<C-o>cgn
" Tab and Shift tab to go next and previous buffers
nnoremap <Tab> :bnext<CR>:normal <C-g><CR>
nnoremap <S-Tab> :bprevious<CR>:normal <C-g><CR>

" allows to type gf to "go to file"
set path+=.,**
Enter fullscreen mode Exit fullscreen mode

I also like "Ctrl-6" to jump to alternate buffer ":%y+" to copy the entire buffer to the clipboard. "gx" to open links. "gv" to reselect. "gi" to start insert at the last inserting point.

" Use whole "words" when opening URLs.
" This avoids cutting off parameters (after '?') and anchors (after '#'). 
" See http://vi.stackexchange.com/q/2801/1631
let g:netrw_gx="<cWORD>"    

" copy the last command to clipboard
:let @+=@:

" avoid clipboard hacking security issue
" http://thejh.net/misc/website-terminal-copy-paste
inoremap <C-R>+ <C-R><C-R>+
Enter fullscreen mode Exit fullscreen mode

On my ~/.zshrc and ~/.bashrc I have

" lvim opens the last edited file on vim
alias lvim='vim -c "normal '\''0"'

# Edit clipboard on a vim scratch buffer
alias vimscratch="vim -c 'setlocal buftype=nofile bufhidden=wipe nobuflisted noswapfile'"
alias pbpaste='xclip -i -selection clipboard -o'
alias pbcopy='xclip -selection clipboard'
alias vcb="pbpaste | vimscratch -"

# Open files very quicly using fzy
alias nfu='nvim $(find ~/.dotfiles -type f | fzy)'
Enter fullscreen mode Exit fullscreen mode

Currently I have an autoloaded zsh function that goes like this:

    # source:https://stackoverflow.com/a/65375231/2571881
function vif() {
    local fname
    local current_dir=$PWD
    cd ~/.dotfiles
    fname=$(fzy) || return
    vim "$fname"
    cd $current_dir
}
   # zsh key-binding to run the vif function Ctrl-o
   bindkey -s '^o' 'vif^M'
Enter fullscreen mode Exit fullscreen mode