How do I jump to markers within different tabs in vim?

和自甴很熟 提交于 2019-12-10 20:08:43

问题


I'm using MacVim and I usually have a number of tabs open. I'd like to be able to drop marks in any of my open files and jump between them. mK and K work great when the mark is in the same tab but I've got to use gt to find the tab and thenK to find the marker... there must be a better way?


回答1:


Here is a quick and dirty hack that answers your need.

let s:marks = {}

function! s:Mark(name)
  echomsg "new mark: " a:name
  " todo: record the winnr/bufnr as well
  let s:marks[a:name] = tabpagenr()
  exe 'normal! m'.a:name
endfunction

function! s:Jump(how, name)
  if has_key(s:marks, a:name)
    let nr = s:marks[a:name]
    tabfirst
    let first = tabpagenr()
    while tabpagenr() != nr
      tabnext
      if tabpagenr() == first
 break
      endif
    endwhile
    if tabpagenr() == nr
      exe 'normal! '.a:how.a:name
      " nominal termination
      return
    endif
  endif
  echoerr "tab-mark " . a:name . " not set"
endfunction

nnoremap m :call <sid>Mark(nr2char(getchar()))<cr>
nnoremap ` :call <sid>Jump('`', nr2char(getchar()))<cr>
nnoremap ' :call <sid>Jump("'", nr2char(getchar()))<cr>

Issues:

  • marks are different for each buffer normally. Here, all the marks are global. May be, we should instead provide mappings to \m, \', ang \*backtick*

  • This does not take split windows into account.



来源:https://stackoverflow.com/questions/4603745/how-do-i-jump-to-markers-within-different-tabs-in-vim

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!