Created
August 1, 2022 18:11
-
-
Save dubgeiser/f706b9198d7597ef538727908e125fac to your computer and use it in GitHub Desktop.
VimScript function to get a shorter full path of a file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| " Display a short path where the first directory is displayed with its | |
| " full name, and the subsequent directories are shortened to their | |
| " first letter, i.e. `/home/user/foo/foo/bar/baz.vim` becomes | |
| " `~/foo/f/b/baz.vim` | |
| " | |
| " This kinda duplicates the behavior of `pathshorten()` but it differs because | |
| " `pathshorten()` also truncates the first directory name in the path, which | |
| " is undesired behavior since a lot of info is in there, plus, it also helps | |
| " in differentiating between Git buffer and its corresponding file when doing | |
| " `:GDiff` and the likes. | |
| function! file#ShortPath() abort | |
| let dirsep = has('win32') && ! &shellslash ? '\' : '/' | |
| let filepath = expand('%:p') | |
| if empty(filepath) | |
| return '' | |
| endif | |
| let mod = (exists('+acd') && &acd) ? ':~' : ':~:.' | |
| let fpath = split(fnamemodify(filepath, mod), dirsep) | |
| let filename = remove(fpath, -1) | |
| " Empty fpath happens when editing a file in '/' or in current directory on | |
| " Unix-based systems. | |
| " This behavior is untested under Windows! | |
| if empty(fpath) | |
| return empty(filename) ? dirsep : filename | |
| endif | |
| let fpath_shortparts = map(fpath[1:], 'v:val[0]') | |
| let path = join(extend([fpath[0]], fpath_shortparts), dirsep) . dirsep | |
| if path == ('.' . dirsep) | |
| let path = '' | |
| endif | |
| return path . filename | |
| endfunction |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment