DEV Community

Jeff Kreeftmeijer
Jeff Kreeftmeijer

Posted on • Originally published at updates.jeffkreeftmeijer.com

What's your favorite Vim trick?

What's that Vim trick that blew your mind the first time you learned about it? A feature that has a big (or small) impact on your workflow, or just a command you use a lot. Anything goes! 🤯

I'll start with my go-to trick: I usually only notice a match should be replaced after searching for it with / (/foo). After learning that substitutions with empty search patterns (%:s//bar/) replace the previously found matches, I've never had to re-type a pattern again.

Latest comments (31)

Collapse
 
pbnj profile image
Peter Benjamin (they/them) • Edited

Say you've changed a function signature, like:

// from
function add3(n) {
  return n+3;
}

// to
function add(n, m) {
  return n+m
}
Enter fullscreen mode Exit fullscreen mode

and you want to update all the references across all files of a project, you can do this with quickfix + :cdo:

  1. First, grep for the old function name in the src/ directory.
:grep "add3(" -R src/
Enter fullscreen mode Exit fullscreen mode

This will load all results into a quickfix list.

You can see this list by typing:

:copen
Enter fullscreen mode Exit fullscreen mode
  1. Now, you can run another vim command to simply update all the references in your quickfix list in one go:
:cdo s/add3\(/add\(3,\ /g | update
Enter fullscreen mode Exit fullscreen mode

This is a simple subsitituion which replaces all references to add3( to add(3, and saves the changes.

For more information about quickfix lists and what you can do with them, check out the vim docs and feel free to search "vim quickfix" online (lots of good blog posts & youtube videos dive into this feature of vim).

Collapse
 
brandonwallace profile image
brandon_wallace

I set up a shortcut to print in my .vimrc.

I run this command to see available printers.

$ lpstat -v
Enter fullscreen mode Exit fullscreen mode

I set a default printer.

$ lpoptions -d <printer_name>
Enter fullscreen mode Exit fullscreen mode

In my .vimrc I have this line to print the current file.

nnoremap <silent> <leader>p :%w !lp<cr>
Enter fullscreen mode Exit fullscreen mode
Collapse
 
voyeg3r profile image
Sérgio Araújo

To me one of the most usefull features I have discovered is how to manipulate varoius registers type like:

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

 " Copy the buffer to the clipboard
 :%y+

Load a function from the clipboard:

:@+

On insert mode, paste any register with Ctrl-r + Register, for example insert last search

Ctrl-r /
Collapse
 
gopherj profile image
Cheng JIANG • Edited
nnoremap <space><space> <c-^>
Enter fullscreen mode Exit fullscreen mode

then space space to switch between two files

Collapse
 
pbnj profile image
Peter Benjamin (they/them)

NeoVim has a "preview" functionality when you're doing substitutions.

First: set inccommand=split
Then, enjoy incremental highlighting of the text that will be impacted by any substitutions until you hit [ENTER]: :%s/foo/bar/g

Collapse
 
pbnj profile image
Peter Benjamin (they/them) • Edited

The operations you can do with the built-in file explorer (aka netrw):

  • Create a new directory: d
  • Create a new file: %
  • Delete file(s)/directory(ies): D
  • Execute file: X (upper-case)
  • For more: :help netrw-quickhelp
Collapse
 
erebos-manannan profile image
Erebos Manannán • Edited

<Esc><Esc><Esc>:q! 😆

Collapse
 
niorad profile image
Antonio Radovcic

cit

Collapse
 
ycmjason profile image
YCM Jason

Macro! Definitely macro! (Although it is a feature not a trick)

Collapse
 
mijohnst profile image
Mike Johnston

I like to encrypt files sometimes.

vim -x /path/to/file/somefile.txt

Collapse
 
jrop profile image
Jonathan Apodaca

Vim has a biult-in file explorer: :E

I use it to open existing files or just to explore the filesystem through the terminal

Collapse
 
saltaverde profile image
Ben Coleman

:%!python -m json.tool

Auto pretty formats json files :)

Collapse
 
timoteoponce profile image
Timoteo Ponce

Whenever I want to search for the current word, I just type:

#
Enter fullscreen mode Exit fullscreen mode

and then n or N to navigate through the results.

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
Collapse
 
safijari profile image
Jariullah Safi

My favorite Vim trick is Spacemacs 👀

Anywho, macros. I know it sounds a bit basic but boy are they powerful (and they turn heads, both in a technical and non technical setting).