编辑:稍微更改了解决方案的界面.原始版本可以在要点历史中找到.Is there a command in vim that can bookmark a place (path to the file, line number in that file), so that I can go to that place easily later?It would be similar as NERDTree :Bookmark command. You can open your file with NERDTreeFromBookmark. I'm looking for the same functionality with the difference that bookmark is not only a file but file + line number.Thank you 解决方案 The viminfo setting can contain the option !, which makes it store any global variables with uppercase letters in the viminfo file. Using this, you can define a variable called g:BOOKMARKS and store your bookmarks in there.Here's some vimscript you could use to do that:set viminfo+=!if !exists('g:BOOKMARKS') let g:BOOKMARKS = {}endif" Add the current [filename, cursor position] in g:BOOKMARKS under the given" namecommand! -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 wviminfoendfunction" Delete the user-chosen bookmarkcommand! -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) wviminfoendfunction" Go to the user-chosen bookmarkcommand! -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" Completion function for choosing bookmarksfunction! s:BookmarkNames(A, L, P) return join(sort(keys(g:BOOKMARKS)), "\n")endfunctionI'm not sure how readable the code is, but basically, the Bookmark command accepts a single parameter to use as a name. It will store the current filename and cursor position to the g:BOOKMARKS dictionary. You can use the GotoBookmark command with a mark name to go to it. DelBookmark works in the same way, but deletes the given mark. Both functions are tab-completed.Another way to jump through them is by using this command:" Open all bookmarks in the quickfix windowcommand! 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) copenendfunctionCopenBookmarks will load the bookmarks in the quickfix window, which seems like a nice interface to me.This solution is similar to Eric's -- it uses the .viminfo file, so if something goes wrong with it, you'll probably lose your marks. And if you save your marks in one vim instance, they won't be immediately available in another.I don't know how comfortable your are with vimscript, so just in case -- to use this, you can put the code in a file under your plugin vimfiles directory, for example plugin/bookmarks.vim. Should be completely enough. Here's the entire code in a gist as well: https://gist.github.com/1371174EDIT: Changed the interface for the solution a bit. Original version can be found in the gist history. 这篇关于vim中最喜欢的地方的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-21 16:19