Skip to content

Instantly share code, notes, and snippets.

@AndrewRadev
Created November 16, 2011 20:02
Show Gist options
  • Save AndrewRadev/1371174 to your computer and use it in GitHub Desktop.
Save AndrewRadev/1371174 to your computer and use it in GitHub Desktop.
Simple bookmark management in Vim
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment