O'Reilly logo

Mastering Vim by Ruslan Osipov

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

File-related commands

Since Vim is a text editor, much of what you do operates on files. Vim provides a number of file-related functions.

You can manipulate file path information using expand:

echom 'Current file extension is ' . expand('%:e')

When passed a filename (through %, #, or shortcuts such as <cfile>), expand lets you parse the path using these modifiers:

  • :p expand to full path
  • :h head (last path component removed)
  • :t tail (last path component only)
  • :r root (one extension removed)
  • :e extension only

See :help expand() for more information about these.

You can check the file exists (aka can be read) by using filereadable:

if filereadable(expand('%'))  echom 'Current file (' . expand('%:t') . ') is readable!'endif

When executed from ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required