vimtricks.wiki Concise Vim tricks, one at a time.

How do I insert the filename under the cursor directly into the command line?

Answer

<C-r><C-f>

Explanation

While typing a command, <C-r><C-f> inserts the filename under the cursor in the buffer at the command-line prompt. It reads the isfname characters at the cursor position and drops the path straight into your Ex command — no copying, no switching back to Normal mode.

How it works

  • <C-r> in command-line mode opens a register insertion prompt
  • <C-f> (after <C-r>) inserts the filename under the cursor in the current buffer
  • Other <C-r><C-x> shortcuts in command-line mode:
    • <C-r><C-w> — insert the word under the cursor (already a common idiom)
    • <C-r><C-a> — insert the WORD (whitespace-delimited token) under the cursor
    • <C-r><C-l> — insert the entire current line
    • <C-r><C-p> — insert the expanded filename under cursor (resolves relative paths)

Example

Your buffer contains a log entry:

Failed to load: config/database.yml

Place the cursor on config/database.yml, then type:

:e <C-r><C-f>

Vim inserts the path and completes to :e config/database.yml, opening the file immediately.

Tips

  • Works anywhere a filename is referenced in text: Makefiles, log files, import statements, shell scripts
  • <C-r><C-p> is useful when the path under the cursor is relative and you need the absolute form in the command
  • Combine with :split, :tabedit, :read, or any other file-accepting command
  • If the filename contains spaces, Vim may truncate at the first space — wrap it manually with quotes in such cases

Next

How do I run a search and replace only within a visually selected region?