Skip to content

Instantly share code, notes, and snippets.

@dansheffler
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dansheffler/f9a94f264d5b8690df23 to your computer and use it in GitHub Desktop.
Save dansheffler/f9a94f264d5b8690df23 to your computer and use it in GitHub Desktop.
Vim script for walking up or down a Scrivener drafts directory (or any directory really).
function! MyDirectoryNav(direction)
let theDirectory = fnamemodify(expand('%'),':p:h') " Should be the /drafts/ directory of a Scrivener project, but will work for anything.
let thisFile = fnamemodify(expand('%'),':p') " Path to the current file.
let theFiles = split(globpath(theDirectory,'*.md'),'\n') " A list of all the .md files in the directory.
let thisIndex = index(theFiles, thisFile) " the index of the current file in this list.
if a:direction == 'j'
let nextFile = get(theFiles, thisIndex + 1, theFiles[0]) " If the direction is down, get the next file or loop to the beginning if the current file is last.
else
let nextFile = get(theFiles, thisIndex - 1, theFiles[-1]) " Likewise for going up.
endif
execute "edit " . fnameescape(nextFile)
endfunction
nn <D-∆> :call MyDirectoryNav('j')<CR>
nn <D-˚> :call MyDirectoryNav('k')<CR>
@dansheffler
Copy link
Author

I sync Scrivener projects with a /drafts/ directory where each Scrivener document (usually an individual paragraph of my paper/dissertation) is its own markdown file. Each file is prepended with a number so all the documents are kept in order.

With this little function in my .vimrc file I can quickly go to the next or previous paragraph by hitting alt+cmd+j or alt+cmd+k.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment