TIL: display path to current file in Vim

2021-01-16 00:00:00 +0000 UTC

To output the path to the file you’re currently editing in Vim, press Ctrl-G in normal mode. In the status bar at the bottom of the window Vim will output the path to the current file and other information about the current buffer.

This is a normal mode shortcut to the ex-command :f, itself short for :file. To print just the current path in the status line, use :echo expand('%'). You can print the location of the current file into your buffer using a set of commands like :redir @a | echo expand('%') | redir END | put a.

A bit wordy, but this command temporarily redirects output to the register a, echoes the output of expand('%') into that register, ends our redirect, and the puts the content of register a.

Tags: vim til