public
Created

Simple bookmark management in Vim

  • Download Gist
bookmarks.vim
VimL
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
set viminfo+=!
 
if !exists('g:BOOKMARKS')
let g:BOOKMARKS = {}
endif
 
" Add the current [filename, cursor position] in g:BOOKMARKS under the given
" name
command! -nargs=1 Bookmark call s:Bookmark(<f-args>)
function! s:Bookmark(name)
let file = expand('%:p')
let cursor = getpos('.')
 
if file != ''
let g:BOOKMARKS[a:name] = [file, cursor]
else
echom "No file"
endif
 
wviminfo
endfunction
 
" Delete the user-chosen bookmark
command! -nargs=1 -complete=custom,s:BookmarkNames DelBookmark call s:DelBookmark(<f-args>)
function! s:DelBookmark(name)
if !has_key(g:BOOKMARKS, a:name)
return
endif
 
call remove(g:BOOKMARKS, a:name)
wviminfo
endfunction
 
" Go to the user-chosen bookmark
command! -nargs=1 -complete=custom,s:BookmarkNames GotoBookmark call s:GotoBookmark(<f-args>)
function! s:GotoBookmark(name)
if !has_key(g:BOOKMARKS, a:name)
return
endif
 
let [filename, cursor] = g:BOOKMARKS[a:name]
 
exe 'edit '.filename
call setpos('.', cursor)
endfunction
 
" Open all bookmarks in the quickfix window
command! CopenBookmarks call s:CopenBookmarks()
function! s:CopenBookmarks()
let choices = []
 
for [name, place] in items(g:BOOKMARKS)
let [filename, cursor] = place
 
call add(choices, {
\ 'text': name,
\ 'filename': filename,
\ 'lnum': cursor[1],
\ 'col': cursor[2]
\ })
endfor
 
call setqflist(choices)
copen
endfunction
 
" Completion function for choosing bookmarks
function! s:BookmarkNames(A, L, P)
return join(sort(keys(g:BOOKMARKS)), "\n")
endfunction

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.