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