mhinz / vim-startify

:link: The fancy start screen for Vim.
MIT License
5.3k stars 186 forks source link

Question about open bookmark using NERDTree #455

Closed lesar closed 2 years ago

lesar commented 3 years ago

Hallo, I try to better integrate NERDTree without dependencies to use on session and in bookmark pointing on folder.
I rewite this function only to make an example to show you what I need: (but is working)

let g:startify_open_folder_command='edit' "default for user not NERDTree aware or let g:startify_open_folder_command='NERDTree'

" Function: s:open_buffer {{{1
function! s:open_buffer(entry)
  if a:entry.type == 'special'
    execute a:entry.cmd
  elseif a:entry.type == 'session'
    execute a:entry.cmd a:entry.path
  elseif a:entry.type == 'file'
    if line2byte('$') == -1
      execute 'edit' a:entry.path
    else
      if a:entry.cmd == 'tabnew'
        wincmd =
      endif
      " execute a:entry.cmd a:entry.path
      " leonardo
      if isdirectory(a:entry.path)
        :enew
        let l:cmd = empty(g:startify_open_folder_cmd) ? 'edit' :  g:startify_open_folder_cmd
        execute l:cmd a:entry.path
        :echom 'leonardo'
      else
        execute a:entry.cmd a:entry.path
      endif
      " fine leonardo
    endif
    call s:check_user_options(a:entry.path)
  endif
  if exists('#User#StartifyBufferOpened')
    doautocmd <nomodeline> User StartifyBufferOpened
  endif
endfunction

My startify configuration:

let NERDTreeHijackNetrw = 1
let g:startify_session_dir = '~/.vim/session'
let g:startify_lists = [
          \ { 'type': 'sessions',  'header': ['   Sessions']       },
          \ { 'type': 'files',     'header': ['   Files']            },
          \ { 'type': 'dir',       'header': ['   Current Directory '. getcwd()] },
          \ { 'type': 'bookmarks', 'header': ['   Bookmarks']      },
          \ ]
let g:startify_bookmarks = [
            \ { 'i': '~/.vim/init' },
            \ { 'j': '~/.vim/plugged/vim-startify/autoload/startify.vim' },
            \ ]
let g:startify_session_autoload = 1
let g:startify_session_delete_buffers = 1
let g:startify_change_to_dir = 1
let g:startify_session_persistence = 1
let g:startify_session_before_save = [
            \ 'silent! NERDTreeClose'
            \ ]
let g:startify_open_folder_cmd = 'NERDTree'
"by the silent NERDTreeClose above I do
autocmd SessionLoadpost * call SessionLoadLightLine()
function SessionLoadLightLine()
  :NERDTree "these restore NERDTree on sessio load
  :wincmd p
  :call lightline#update()"I have lightline and need refresh
endfunction

" Start NERDTree when Vim starts with a directory argument.
" but not work on :edit so I modified plugin
autocmd StdinReadPre * let s:std_in=1
autocmd VimEnter * if argc() == 1 && isdirectory(argv()[0]) && !exists('s:std_in') |
     \ execute 'NERDTreeToggle' argv()[0] | wincmd p | enew | execute 'cd '.argv()[0] | endif

Is a bad Idea?
Other solution I try not fit well.

there is a more simple solution?

best regards, Leonardo

lesar commented 3 years ago

I have used it often and it work well

lesar commented 2 years ago

I have found a better solution that not involve in startify customization but in autocommand on NERDTree:

augroup NERDTreeStartify
  autocmd VimEnter * silent! autocmd! FileExplorer
  au BufEnter * call Stica(expand('<amatch>'))
  au VimEnter * if argc() == 1 && !exists('s:std_in') || v:this_session != '' | call Stica(expand('<amatch>')) | endif
augroup END

function! Stica(dir) abort
  if !isdirectory(a:dir)
    return
  endif
  execute 'NERDTreeToggle' a:dir | wincmd p | enew | wincmd p | execute 'cd '.a:dir
endfunction

" my startify conf:
"========================  Startify  =============================================
let g:startify_session_dir = '~/.vim/session'
let g:startify_lists = [
          \ { 'type': 'sessions',  'header': ['   Sessions']       },
          \ { 'type': 'files',     'header': ['   Files']            },
          \ { 'type': 'dir',       'header': ['   Current Directory '. getcwd()] },
          \ { 'type': 'bookmarks', 'header': ['   Bookmarks']      },
          \ ]
let g:startify_bookmarks = [
            \ { 'i': '~/.vim/init/' },
            \ ]
let g:startify_session_autoload = 1
let g:startify_session_delete_buffers = 1
let g:startify_change_to_dir = 1
let g:startify_session_persistence = 1
let g:startify_session_before_save = [
            \ 'silent! NERDTreeClose'
            \ ]
let g:startify_open_folder_cmd = 'NERDTree'

Now if on startify hit 'i' I onpen my path using NERDTree
if I do :e /path-to/folder go on NERDTree too if I do vim /path-to/folder go on NERDTree too if I start a saved session go on NERDTree too

I'm not a vim developer so if someone can make my code better is welcome: reopen the question and add. Forgive my poor English.

best regards, Leonardo