Reputation: 5617
I'm usually using GVim with multiple tabs opened, and I usually switch manually my working directory on whatever buffer I'm working on.
Each tab then gets as label the relative path of their respective files from my buffer. However this only works when the other files are further down in folders, while other files display a full path starting from my home folder.
Is it possible to make vim display ALL paths relative to :pwd, using ..
as necessary?
Example:
:pwd -> /home/files/folder1
tab1label = file1
tab2label = ../folder2/file2
EDIT:
I actually just realized that the default behaviour of GVim is exactly the one I want, however as soon as I :cd
to another folder the ..
go away.
Upvotes: 2
Views: 1224
Reputation: 10455
This solution works almost twice as fast, comparing to Svalorzen's:
fu! RelativePathString(file)
if strlen(a:file) == 0
retu "[No Name]"
en
let common = getcwd()
let result = ""
while substitute(a:file, common, '', '') ==# a:file
let common = fnamemodify(common, ':h')
let result = ".." . (empty(result) ? '' : '/' . result)
endw
let forward = substitute(a:file, common, '', '')
if !empty(result) && !empty(forward)
retu result . forward
elsei !empty(forward)
retu forward[1:]
en
endf
Upvotes: 1
Reputation: 5617
I've coded my own solution.. since it is my first attempt at vim coding is probably not the best, but it's working correctly.
function RelativePathString(file)
let filelist=split(a:file,'/')
if len(filelist) == 0
return "[No name]"
endif
let dir=getcwd()
let dirlist=split(dir,'/')
let finalString=""
let i = 0
for str in dirlist
if str !=# filelist[i]
break
else
let i += 1
endif
endfor
let j=0
let k=len(dirlist)-i
while j < k
let finalString .= "../"
let j += 1
endwhile
let j=len(filelist)-1
while i < j
let finalString .= filelist[i] . "/"
let i += 1
endwhile
let finalString .= filelist[i]
return finalString
endfunction
let &guitablabel="%!RelativePathString(expand('%:p'))"
Upvotes: 1
Reputation: 196789
substitute(expand('%:p'), getcwd(), '..', '')
With
/home/romainl
as working directory, the short snippet above turns
/home/romainl/.vim/helpers/functions.vim
into
../.vim/helpers/functions.vim
Obviously, it won't be very useful when you edit a file that is outside of the working directory.
Upvotes: 0
Reputation: 22734
Have you checked the mailing list? I found two helpful answers, but maybe you will find more.
ShortGuiTabLabel()
function and a reminder for the pathshorten()
Vim function.I'm not sure if these answer your question but they might help steer you in the right direction.
Upvotes: 1