vim中最喜欢的地方 [英] Favourite places in vim
问题描述
vim
中是否有一个命令可以为某个位置(该文件的路径,该文件中的行号)添加书签,以便以后可以轻松地转到该位置?
它与NERDTree
:Bookmark
命令类似.您可以使用NERDTreeFromBookmark
打开文件.我正在寻找相同的功能,不同之处在于书签不仅是文件,而且是文件+行号.
谢谢
viminfo
设置可以包含选项!
,这使它可以在viminfo文件中存储所有带有大写字母的全局变量.使用此功能,您可以定义一个名为g:BOOKMARKS
的变量,并将书签存储在其中.
以下是一些vimscript,您可以用来执行此操作:
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
" Completion function for choosing bookmarks
function! s:BookmarkNames(A, L, P)
return join(sort(keys(g:BOOKMARKS)), "\n")
endfunction
我不确定代码的可读性,但是基本上,Bookmark
命令接受一个参数作为名称.它将当前文件名和光标位置存储到g:BOOKMARKS
词典中.您可以将GotoBookmark
命令与标记名称一起使用. DelBookmark
以相同的方式工作,但是删除给定的标记.这两个功能都已完成制表符.
另一种跳过它们的方法是使用以下命令:
" 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
CopenBookmarks
将在quickfix窗口中加载书签,这对我来说似乎是一个不错的界面.
此解决方案类似于Eric的解决方案-它使用.viminfo
文件,因此,如果出现问题,您可能会丢掉分数.而且,如果您将标记保存在一个vim实例中,则它们将不会立即在另一个vim实例中使用.
我不知道您对vimscript有多满意,以防万一-为了使用它,您可以将代码放在plugin
vimfiles目录下的文件中,例如plugin/bookmarks.vim
.应该完全足够了.这也是要点中的整个代码: https://gist.github.com/1371174 >
编辑:稍微更改了解决方案的界面.原始版本可以在要点历史中找到.
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
" 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
" Completion function for choosing bookmarks
function! s:BookmarkNames(A, L, P)
return join(sort(keys(g:BOOKMARKS)), "\n")
endfunction
I'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 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
CopenBookmarks
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/1371174
EDIT: Changed the interface for the solution a bit. Original version can be found in the gist history.
这篇关于vim中最喜欢的地方的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!