Search code examples
vimvim-plugin

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


I want to make function that will duplicate selection to end of selection

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

then I select with V content of this function and press , echo lines output:

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

if I comment append line, output will be

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

Why?


Solution

  • :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.
    

    By pressing Ctrl+d function DuplicateBlock() will of course start treating first line of visual selection and when excuting append(line('.'), lines) it will add that line (added to the list) after the current line (which is actually a duplication of line treated). The second invocation of function will do the same thing for the line previously added and so on.