If you want to get the directory that the currently open file is in: let g:session_default_name = expand('%:p:h')
. This works whether vim is opened with to a file or not, but will only do what you expect if the file has not changed since vim was opened.
If you want to get the directory of the first file in :argv
(the filename arguments when vim was opened): let g:session_default_name = fnamemodify(argv()[0], ':p:h')
. This will fail if vim was opened with no arguments.
To fallback on cwdgetcwd()
where there were no arguments:
" Returns the directory of the first file in `argv` or `cwd()``cwd` if it's empty
function FindSessionDirectory() abort
if len(argv()) > 0
return fnamemodify(argv()[0], ':p:h')
endif
return cwdgetcwd()
endfunction!
let g:session_default_name =FindSessionDirectory= FindSessionDirectory()