When doing this:
vim -S Session.vim OTHER_FILE
The shown buffer contains the last opened file in the Session.vim
and not the file OTHER_FILE
. I find this behavior a bit disturbing, I would have found it more coherent to show the content of the file OTHER_FILE
. (OTHER_FILE
does not appear in the Session.vim
file)
After reading the Vim documentation, it appears that session files are loaded after having opened other files provided by arguments. So if the session contains an edit
statement, this is logical that the shown buffer contains the file of that edit
statement.
With the help of the following function, it now shows the last file provided by argument:
function! GoToLastArgumentFile()
let l:current_buf_id = bufnr('%')
let l:highest_buf_id = l:current_buf_id
for buf_id in range(1, bufnr('$'))
if bufexists(buf_id) && l:current_buf_id != buf_id && bufloaded(buf_id)
let l:highest_buf_id = buf_id
endif
endfor
if l:highest_buf_id < l:current_buf_id
execute ":buffer ".l:highest_buf_id
endif
endfunction
And also by adding this line in the .vimrc file:
autocmd! VimEnter * :call GoToLastArgumentFile()
I am pretty new at vim script, I found that solution a bit complicated and I was wondering if there was any better way to change the default behavior ?
vim OTHER_FILE
and when ready to work on the session-based stuff do:source Session.vim
.SessionLoadPost
autocmd event. If it works as one would expect that's probably a more appropriate trigger than the generalVimEnter
.SessionLoadPost
. It is a way better idea thanVimEnter
in this case. I am going to to do a few tests then I will update my question.