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 files.vim, the output would be:

Current file (files.vim) is readable!

Similarly, you can check you have write permissions to the file using filewritable.

You can perform the rest of the file operations using the execute command. For example, you'd write the following to open the animals.py file:

execute 'edit animals.py'
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.183.137