Skip to content
Sidney Liebrand edited this page Aug 27, 2018 · 2 revisions

This is the Tips & Tricks page for codi.vim. It is a place to put features that may not fit the plugin itself, but would otherwise be good standalone enhancements that may be added to one's .vimrc.

Fullscreen scratch

Taken from this medium post. This snippet allows you to work with codi.vim always in fullscreen mode, it also enhances buffer names and remembers scratch contents (by not deleting the buffer but hiding it). In this example the mapping Leader+Leader is used but this can be customized to be anything you like.

The nmap <buffer> ... which gets mapped from within the buffer overwrites the outer mapping and instead, hides the current scratchpad instead of spawning a new instance from within a scratchpad, when this scratchpad is hidden the outer nmap <buffer> ... will be executed again, showing the scratchpad if it existed or creating a new empty one if it didn't.

" since it is fullscreen, I'd like a 50/50 split
let g:codi#width = 50.0

" instead of destroying buffers, hide them and
" index them by filetype into this dictionary
let s:codi_filetype_tabs = {}

fun! s:FullscreenScratch()
  " store filetype and bufnr of current buffer
  " for later reference
  let current_buf_ft  = &ft
  let current_buf_num = bufnr('%')

  " check if a scratch buffer for this filetype already exists
  let saved_scratch = get(s:codi_filetype_tabs, current_buf_ft, -1)

  " if a tabpage exists for current_buf_ft, go to it instead of
  " creating a new scratch buffer
  if saved_scratch != -1
    if index(map(gettabinfo(), 'v:val.tabnr'), saved_scratch) == -1
      unlet s:codi_filetype_tabs[current_buf_ft]
    else
      exe 'tabn' saved_scratch
      return
    endif
  endif

  " create a new empty tab, set scratch options and give it a name
  tabe
  setlocal buftype=nofile noswapfile modifiable buflisted bufhidden=hide
  exe ':file scratch::' . current_buf_ft

  " set filetype to that of original source file
  " e.g. ruby / python / w/e Codi supports
  let &filetype = current_buf_ft

  " store the tabpagenr per filetype so we can return
  " to it later when re-opening from the same filetype
  let s:codi_filetype_tabs[&filetype] = tabpagenr()

  " create a buffer local mapping that overrides the
  " outer one to delete the current scratch buffer instead
  " when the buffer is destroyed, this mapping will be
  " destroyed with it and the next <Leader><Leader>
  " will spawn a new fullscreen scratch window again
  nmap <silent><buffer> <Leader><Leader> :tabprevious<Cr>

  " everything is setup, filetype is set
  " let Codi do the rest :)
  Codi
endfun

" create a mapping to call the fullscreen scratch wrapper
nmap <silent> <Leader><Leader> :call <SID>FullscreenScratch()<Cr>
Clone this wiki locally