Favorite Vim Tricks


These are some of my favorite or most easily forgotten vim tricks

Of course, there is a fantastic educational newsletter, VimTricks. You should subscribe and get all the tricks they have. These are some I've learned from them and elsewhere that I want to remember and highlight for myself.

Get filetype of current file

:set filename?

If you're running an LSP with nvim, you can also run :LspInfo, and that will show the detected filetype.

Copy to system clipboard

"*y

  • " - access a register
  • * - the name of the register, the system clipboard
  • y - yank/copy; or yy for a line, yiw, whatever

Copy lines by line number

  • :281t. – Copy line 281 and paste it below the current line
  • :-10t. – Copy the line 10 lines above the current line and paste it below the current line
  • :+8t. – Copy the line 8 lines after the current line and paste it below
  • :10,20t. – Copy lines 10 to 20 and paste them below
  • :t20 – Copy the current line and paste it below line 20

From VimTricks

Close all other buffers

:%bd|e#

  • %b - all buff
  • d - delete
  • | - pipe?
  • e# - open the last buffer for edit

Delete duplicate lines

Visual line select something, then:

:sort u

  • u - unique

Jump to tags in help files

I can actually get around to all those bookmarks to sections referenced in a help file. Big deal, I know.

:h holy-grail or any other help file.

Then while cursor is over a tag:

  • C-] - jump to that tag