0

I recently discovered the world of vim session files, and they are awesome for working with multi-file code projects.

However, when opening a man page I don't need a session file created for viewing man pages.

I like using Neovim to view man pages because of the color and trackpad support when reading a man page.

I recently cobbled the below together to view man pages using a function called vman

function vman --description 'use vim / nvim to read man pages'
  man $argv | col -bp | iconv -c | nvim 'set ft=man nomod nolist' -;
end

However, when I invoke, nvim it loads the below session.vim file from $HOME/.vim/plugin directory

" CREDIT: https://stackoverflow.com/a/47656092/708807

function! MakeSession(overwrite)
  let b:sessiondir = $HOME . "/.vim/sessions" . getcwd()
  if (filewritable(b:sessiondir) != 2)
    exe 'silent !mkdir -p ' b:sessiondir
    redraw!
  endif
  let b:filename = b:sessiondir . '/session.vim'
  if a:overwrite == 0 && !empty(glob(b:filename))
    return
  endif
  exe "mksession! " . b:filename
endfunction

function! LoadSession()
  let b:sessiondir = $HOME . "/.vim/sessions" . getcwd()
  let b:sessionfile = b:sessiondir . "/session.vim"
  if (filereadable(b:sessionfile))
    exe 'source ' b:sessionfile
  else
    echo "No session loaded."
  endif
endfunction

" Adding automatons for when entering or leaving Vim
if(argc() == 0)
  au VimEnter * nested :call LoadSession()
  au VimLeave * :call MakeSession(1)
else
  au VimLeave * :call MakeSession(0)
endif

Is there some a way I can check to see if the buffer is loading a .man file and not create a session file?


Per Rich's answer, I ended up modifying the above posted sessions.vim file to the below,

" sessions.vim

" CREDIT: https://stackoverflow.com/a/47656092/708807

    function! MakeSession(overwrite)
      if &filetype ==? 'man'
        return
      else
        let b:sessiondir = $HOME . "/.vim/sessions" . getcwd()
        if (filewritable(b:sessiondir) != 2)
          exe 'silent !mkdir -p ' b:sessiondir
          redraw!
        endif
        let b:filename = b:sessiondir . '/session.vim'
        if a:overwrite == 0 && !empty(glob(b:filename))
          return
        endif
        exe "mksession! " . b:filename
      endif
    endfunction

    function! LoadSession()
      if &filetype ==? 'man'
        return
      else
        let b:sessiondir = $HOME . "/.vim/sessions" . getcwd()
        let b:sessionfile = b:sessiondir . "/session.vim"
        if (filereadable(b:sessionfile))
          exe 'source ' b:sessionfile
        else
          echo "No session loaded."
        endif
      endif
    endfunction

    " Adding automatons for when entering or leaving Vim
    if(argc() == 0)
      au VimEnter * nested :call LoadSession()
      au VimLeave * :call MakeSession(1)
    else
      au VimLeave * :call MakeSession(0)
    endif
1

So long as you don't open lots of other files inside the instance of Vim that you're using to view man pages, you can achieve this by checking the file type of the current buffer before saving the session:

function! MakeSession(overwrite)
  if &filetype ==? 'man'
    return
  endif

  " The rest of your code...
  • Thanks for 🚣🏿 steering me in the right direction. 👍 I ended up modifying my sessions.vim a little different from what you posted above. And btw, is there no syntax highlighting for Vim script? – ipatch Mar 29 '18 at 17:57
  • I wouldn't have thought the guard would be necessary in the LoadSession() function since you're already checking argc() before calling it? As for syntax highlighting of Vimscript, if you're asking about in Vim, the answer is yes. If you mean on this site, then the answer is no. :( – Rich Mar 29 '18 at 22:34
  • yeah I meant on this site. 🤷‍♀️ – ipatch Mar 30 '18 at 22:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.