Scnvim Tricks and Tips



Published June 11, 2020

The last six years or so I have used vim as my text editor, gradually weaning myself off of anything that does not allow the use of hjkl to move around in the world of unix. SuperCollider is no exception. I started out using the scvim plugin, but a couple of years ago I switched to neovim, a fork of vim with less baggage. This gave me the possibility to play with scnvim, a plugin that addresses some of scvim’s shortcomings and, like neovim itself, brings new and interesting possibilities to the table. First of all, let’s install it. Since I use vim-plug as my plugin manager, I need to put this into my init.vim:

call plug#begin() 
Plug 'davidgranstrom/scnvim', { 'do': {-> scnvim#install() } }
Plug 'tpope/vim-surround'
Plug 'tpope/vim-commentary'
Plug 'tpope/vim-fugitive'
Plug 'jiangmiao/auto-pairs' 
Plug 'itchyny/lightline.vim'
Plug 'Shougo/deoplete.nvim', { 'do': ':UpdateRemotePlugins' }
Plug 'ervandew/supertab' 
Plug 'SirVer/ultisnips'
call plug#end 

Strictly speaking you only need scnvim, but the other plugins are great tools that can be integrated into my workflow.

Let us define a function that will run whenever a SuperCollider file is opened. Put this into init.vim

" get information about git and server status in your statusline
function! s:startSCNvim()
    let g:lightline = {}
    let g:lightline.component_function = {
                \ 'server_status': 'scnvim#statusline#server_status',
                \ 'gitbranch': 'FugitiveHead'
                \ }

    function! s:set_sclang_lightline_stl()
        let g:lightline.active = {
                    \ 'left':  [ [ 'mode', 'paste' ],
                    \          [ 'gitbranch', 'readonly', 'filename', 'modified' ] ],
                    \ 'right': [ [ 'lineinfo' ],
                    \            [ 'percent' ],
                    \            [ 'server_status'] ]
                    \ }
    endfunction 
    call s:set_sclang_lightline_stl()

    " whenever you hit <cr> you will toggle the post window
    let g:scnvim_postwin_auto_toggle = 1
    " make sure the post window doesn't eat half your window
    let g:scnvim_postwin_size = 50 
    " and wrap the output to make it easier to read
    autocmd FileType scnvim setlocal wrap 

    " enable sc-help right in your nvim session!
    let g:scnvim_scdoc = 1 

    " ensure that your comments look good
    setlocal commentstring=//%s

    " map your function keys to useful functions
    nnoremap <F2> :SCNvimStart<cr>
    nnoremap <F4> :SCNvimRecompile<cr>

    " see what the server is doing
    nnoremap <silent><buffer> <F5> :call scnvim#sclang#send('s.plotTree')<CR>
    nnoremap <silent><buffer> <F6> :call scnvim#sclang#send('s.meter')<CR>

    " record output of the default server to 
    " ~/sc-rec/<name-of-current-document>/<timestamp>.wav
    nnoremap <silent><buffer> <F7> :call scnvim#sclang#send('s.record("~/sc-rec"+/+PathName(thisProcess.nowExecutingPath).fileNameWithoutExtension+/+Date.localtime.stamp++".wav")')<CR>
    " stop recording
    nnoremap <silent><buffer> <F8> :call scnvim#sclang#send('s.stopRecording')<CR>

    " include your supercollider project root in nvim's search path
    set path+="~/sc"

    " make sure argument lists are properly closed when using auto-pairs
    let b:AutoPairs = {'(':')', '[':']', '{':'}',"'":"'",'"':'"', '|':'|'}

    " make sure deoplete starts up to enable autocompletion of code 
    let g:deoplete#enable_at_startup = 1

endfunction 

" finally, call the function whenever a SuperCollider file opens up
augroup sclang
    autocmd FileType supercollider call s:startSCNvim()
augroup end

" and set the `ultisnips` directory to pick up scnvim's included snippets:
```vimrc 
let g:UltiSnipsSnippetDirectories=["/home/<user>/UltiSnips", "/home/<user>/.config/nvim/plugged/scnvim/scnvim-data", "plugged/vim-snippets"]

Run :PlugUpdate in nvim to install scnvim, hit F2 to start sclang and code away! Blocks of code are evaluated by pressing Ctrl-e, but this can be changed to your needs and preferences.