如何遍历我的vimscript中的寄存器?

时间:2012-06-17 20:12:26

标签: vim

我需要将所有没有空字符串的寄存器放入列表中。

首先,它们是否已存在于某个列表中?

其次,如果他们没有更简单的方法将他们放入列表而不是手动遍历所有并检查它们是否为空?

也许我可以创建一个已定义寄存器的列表并在该列表上使用getreg(),但我想我希望列表已经存在,所以我没有必要创建它。

2 个答案:

答案 0 :(得分:2)

以下是我提出的建议:

" Description:
"   Get a list of all the non-empty register indexes
" Example Usage:
"   :call GetNonEmptyRegsIndex( 'print' )    """ To print the registers
"   for n in GetNonEmptyRegsIndex() | echo nr2char(n)." = ".getreg(nr2char(n)) | endfor
" Resources:
"   http://www.ibm.com/developerworks/linux/library/l-vim-script-3/index.html
"   irs channel #vim <jamessan>
function! GetNonEmptyRegsIndex( ... )
  " Create the list of register 'indexes' where the the elements are in char2nr form
  let regnum =  range(char2nr('a'), char2nr('z'))
  let regnum += range(char2nr('0'), char2nr('9'))
  let regstr =  ['"','-','*','%','/','.','#',':']
  let regnum += map(regstr, 'char2nr(v:val)')

  " Remove the registers that are empty
  "let regnum = filter( regnum, 'getreg(nr2char(v:val)) != ""' )
  let regnum = filter( regnum, 'getreg(nr2char(v:val)) !~ "^$"' )

  " Remove the registers that are just spaces
  let regnum = filter( regnum, 'getreg(nr2char(v:val)) !~ "^\s\+$"' )

  " Remove the registers that have no alpha-num
  "let regnum = filter( regnum, 'getreg(nr2char(v:val)) !~ "^\W\+$"' )
  return regnum
endfunction

必须从字符串转换为num并再次返回会使代码变得有点难看,但它有效。

这是用于自动填充注册信息的完整工作解决方案。我可能会稍后调整它,但它给了我想要的基础知识。

inoremap <F5> <C-R>=AutoCompleteRegs()<CR>

function! AutoCompleteRegs()
  call complete( col('.'), GetNonEmptyRegs() )
  return ''
endfunction

" Description:
"   Get a list of all the non-empty registers.
function! GetNonEmptyRegs()
  " Create the list of register 'indexes' where the the elements are in char2nr form
  let regIndexNum = GetNonEmptyRegsIndex()

  " Convert the list of indexes to values
  let regs = map(regIndexNum, 'getreg(nr2char(v:val))')
  "let regs = map(regs, 'substitute(v:val,  "^(.*)", "\\U\\1", "g")')
  "let regs = map(regs, "echo substitute(v:val,  '^(.*)', '\U\1', 'g')")
  return regs
endfunction

" Description:
"   Get a list of all the non-empty register indexes
" Example Usage:
"   :call GetNonEmptyRegsIndex( 'print' )    """ To print the registers
"   for n in GetNonEmptyRegsIndex() | echo nr2char(n)." = ".getreg(nr2char(n)) | endfor
" Resources:
"   http://www.ibm.com/developerworks/linux/library/l-vim-script-3/index.html
"   irs channel #vim <jamessan>
function! GetNonEmptyRegsIndex( ... )
  " Create the list of register 'indexes' where the the elements are in char2nr form
  let regnum =  range(char2nr('a'), char2nr('z'))
  let regnum += range(char2nr('0'), char2nr('9'))
  let regstr =  ['"','-','*','%','/','.','#',':']
  let regnum += map(regstr, 'char2nr(v:val)')

  " Remove the registers that are empty
  "let regnum = filter( regnum, 'getreg(nr2char(v:val)) != ""' )
  let regnum = filter( regnum, 'getreg(nr2char(v:val)) !~ "^$"' )

  " Remove the registers that are just spaces
  let regnum = filter( regnum, 'getreg(nr2char(v:val)) !~ "^\s\+$"' )

  " Remove the registers that have no alpha-num
  "let regnum = filter( regnum, 'getreg(nr2char(v:val)) !~ "^\W\+$"' )
  return regnum
endfunction

答案 1 :(得分:1)

没有这样的列表,至少不能用vimscript访问。您可以解析由:silent reg捕获的silent:redir:无可见输出但仍可捕获)的输出,但我不太喜欢此命令(由于“没有嵌套重定向“以及虽然目标变量将在第一个redir发生的地方创建,但它会在第二个redir发生的地方填充并在那里通过名称引用,这样就可以将数据附加到任何变量或抛出错误,如{ {1}})。我只是不喜欢这种不可预知的命令。