sponsor Vim development Vim logo Vim Book Ad

FilePathConvert : Convert filespec between absolute, relative, and URL formats.

 script karma  Rating 0/0, Downloaded by 1287  Comments, bugs, improvements  Vim wiki

created by
Ingo Karkat
script type
This plugin converts a file path and name that is inserted into the buffer
between absolute and relative paths. This is especially useful after pasting
in or completing (i_CTRL-X_CTRL-F) filespecs.

- html_FileCompletion.vim (vimscript #4070) is insert mode completion of
  URL-escaped file links relative to a document root and base URL.

<Leader>sf              Convert the file whose name is under or after the
                        cursor to a more local one:
                        URL -> [ UNC -> ] absolute -> relative (-> absolute)
{Visual}<Leader>sf      Convert the selected file name.

<Leader>sF              Convert the file whose name is under or after the
                        cursor to a more global one:
                        relative -> absolute  -> [ UNC -> ] URL (-> UNC / abs)
{Visual}<Leader>sF      Convert the selected file name.

                        A relative file path (../foo) is converted to an
                        absolute one (/home/user/dir/foo).
                        An absolute file path (/home/user/dir/foo) is
                        converted to a relative one (../foo).
                        On Windows, an absolute filespec starts with a drive
                        letter (C:\foo).
                        A URL starts with a protocol like file://; the actual
                        protocol is ignored here. Or it is a Windows file
                        share in UNC notation (\\host\share\foo).
                        Unless a mapping of URL prefix to mount point exists
                        (see g:FilePathConvert_UrlMappings), this can only
                        be mapped to a generic share in UNC notation.

Here's a simple illustration:
    :cd C:\Windows\Temp
    :edit test.txt
The text
is turned into:
is turned into:
install details
This script is packaged as a vimball. If you have the "gunzip" decompressor
in your PATH, simply edit the *.vmb.gz package in Vim; otherwise, decompress
the archive first, e.g. using WinZip. Inside Vim, install by sourcing the
vimball or via the :UseVimball command.
    vim FilePathConvert*.vmb.gz
    :so %
To uninstall, use the :RmVimball command.

- Requires Vim 7.0 or higher.
- Requires the ingo-library.vim plugin (vimscript #4433), version 1.019 or
- Requires the TextTransform plugin (vimscript #4005), version 1.22 or higher.

For a permanent configuration, put the following commands into your vimrc:

To be able to convert custom URLs, non-file:// protocols, and UNC paths to a
local file system path, you need to specify the mapping of the base URL to
mount point(s; you can configure a list of filespecs to cater for symbolic
links etc.):
    let g:FilePathConvert_UrlMappings = {
    \   '//win/share': 'D:/data',
    \   'file://///lin/www/htdocs': '/var/srv/htdocs',
    \   'http://lin/': '/var/srv/htdocs',
    \   'file://///srv/user': ['/var/srv/user', '/home/user']
As the filespecs are system-specific, you probably want to define them for the
current host; the hostname() function can help to write a conditional in
case you're using a .vimrc shared across multiple systems.
You can override the global mapping with a buffer-local one, too.

If you just want to add a single mapping for the current buffer, you can do
that by defining both b:baseurl and b:basedir variables.

If you want to use different mappings, map your keys to the
<Plug>TextTFilePathConvert#... mapping targets _before_ sourcing the script
(e.g. in your vimrc):
    nmap <Leader>sf <Plug>TextTFilePathConvert#FilePathConvertLine
    xmap <Leader>sf <Plug>TextTFilePathConvert#FilePathConvertVisual

rate this script Life Changing Helpful Unfulfilling 
script versions (upload new version)

Click on the package to download.

package script version date Vim version user release notes
FilePathConvert-2.00.vmb.gz 2.00 2014-05-31 7.0 Ingo Karkat - Add <Leader>sF variant that converts into more global filespecs to support file:// URLs.
- Handle conversion to / from UNC and URL paths via URL mappings configuation g:FilePathConvert_UrlMappings. *** You need to update to ingo-library (vimscript #4433) version 1.019! ***
FilePathConvert-1.00.vmb.gz 1.00 2014-03-06 7.0 Ingo Karkat Initial upload
ip used for rating:

If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to the maillist. Help Bram help Uganda.