Use relative paths?

I am trying to save my session in Vim with relative paths for opening files. With cur_dir in sessionoptions , the file paths will be relative. the current file, but the session file contains the cd /path/to/base/directory :

 ... cd /path/to/base badd +0 relpath1/file badd +0 relpath2/file ... 

If I leave curdir out of sessions, the cd will disappear, but the file paths will be absolute:

 badd +0 /path/to/base/relpath1/file badd +0 /path/to/base/relpath2/file 

Is there a way to have only relative wrt paths. to whatever current directory when creating a session - without plugins or scripting? To have a session file only:

 badd +0 relpath1/file badd +0 relpath2/file 

My ultimate goal is to have a session file that I can copy, for example. from checking svn to another.

+5
source share
2 answers

You cannot do this without setting up a wrapper function for it, AFAIK.

eg. sort of:

 function! MakeSession() let b:sessiondir = getcwd() let b:filename = b:sessiondir . '/session.vim' exe "mksession! " . b:filename exe "edit! " . b:filename exe "g:^cd :d" exe "x" endfunction 
+6
source

I modified Botykai's answer with an extra line to remove the global absolute path.

 function! MakeSession() let b:sessiondir = getcwd() let b:filename = b:sessiondir . '/_vimsession' exe "mksession! " . b:filename exe "edit! " . b:filename " Delete the line start with 'cd ...' exe "g:^cd :d" " Vim complains about b:sessiondir being undefined. So I use getcwd() directly " exe "%s:" . b:sessiondir . "::g". Use ':' to avoid path escape exe "%s:" . getcwd() . "/::g" " Save with 'x' exe "x" endfunction 

If someone can improve the function above to narrow the lines down to those starting only with badd , that would be better.

0
source

Source: https://habr.com/ru/post/1445218/


All Articles