为什么 getline 输出会根据我是否调用 append 而改变

Why getline output is changed depending on is I calling append or not

我想制作将 selection 复制到 selection

末尾的函数
function! DuplicateBlock() "{{{
  let lines = getline(line('v'), line('.'))
  echo lines
  call append(line('.'), lines)
endfunction "}}}
vnoremap <C-D> :call DuplicateBlock()<CR>

然后我select用这个函数的V内容按下,echo lines输出:

['  let lines = getline(line('v'), line('.'))']
['  let lines = getline(line('v'), line('.'))']
['  let lines = getline(line('v'), line('.'))']

如果我注释附加行,输出将是

['  let lines = getline(line('v'), line('.'))']
['  echo lines']
['  call append(line('.'), lines)']

为什么?

:help append()


append({lnum}, {expr})                                  append()

When {expr} is a List: Append each item of the List as a text line below line {lnum} in the current buffer.

                Otherwise append {expr} as one text line below line {lnum} in
                the current buffer.

Ctrl+d函数DuplicateBlock()当然会开始处理视觉选择的第一行,当执行append(line('.'), lines) 它将在当前行之后添加该行(添加到列表中)(这实际上是处理过的行的重复)。第二次调用函数将对之前添加的行执行相同的操作,依此类推。

改用 :copy/:t 命令。

:copy '>

或作为映射:

xnoremap <C-D> :copy '><CR>

如果您希望光标位于新复制的块的顶部,您可以这样做:

xnoremap <C-D> :copy '><CR>'[

如需更多帮助,请参阅:

:h :copy
:h marks
:h '>
:h '[