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

How do I append the current filename to a named register from Ex command-line?

Answer

:let @a .= expand('%:t') . "\n"

Explanation

Named registers are not just for yanks and deletes. You can build them programmatically from Ex, which is very useful when assembling reusable snippets, macro input, or ad-hoc file lists. Appending the current filename into a register gives you a lightweight way to accumulate context while hopping through buffers.

How it works

  • :let assigns values to Vim variables and registers
  • @a targets register a
  • .= appends instead of replacing existing contents
  • expand('%:t') resolves the current buffer's tail filename
  • ."\n" adds a newline so repeated runs create one filename per line

Example

Run this in each file you want to collect:

:let @a .= expand('%:t') . "\n"

After visiting several buffers, inspect with:

:reg a

Then paste the list anywhere with "ap or execute further transformations against it.

Tips

  • Use %:p instead of %:t to append full absolute paths
  • Switch to another register by changing @a to @b, @c, and so on
  • Reset before a new collection pass with :let @a = ''

Next

How do I open a vertical diff between index and working tree for the current file in vim-fugitive?