Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 706503
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:10:26+00:00 2026-05-14T04:10:26+00:00

I’m pretty confused by one of the subtleties of the vimscript extend() function. If

  • 0

I’m pretty confused by one of the subtleties of the vimscript extend() function.

If you use it to extend a list with another list, it does pretty much what you’d expect, which is to insert the second list into the first list at the index given by the third parameter:

      let list1 = [1,2,3,4,5,6] | echo extend(list1,[1,2,3,4,5,6],5)
      " [1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 6, 6]

However if you give it the same list twice it starts tripping out a bit.

      let list1 = [1,2,3,4,5,6] | echo extend(list1,list1,0)
      " [1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6]
      let list1 = [1,2,3,4,5,6] | echo extend(list1,list1,1)
      " [1, 1, 1, 1, 1, 1, 1, 2, 3, 4, 5, 6]
      let list1 = [1,2,3,4,5,6] | echo extend(list1,list1,2)
      " [1, 2, 1, 2, 1, 2, 1, 2, 3, 4, 5, 6]
      let list1 = [1,2,3,4,5,6] | echo extend(list1,list1,3)
      " [1, 2, 3, 1, 2, 3, 1, 2, 3, 4, 5, 6]
      let list1 = [1,2,3,4,5,6] | echo extend(list1,list1,4)
      " [1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 5, 6]
      let list1 = [1,2,3,4,5,6] | echo extend(list1,list1,5)
      " [1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 6]
      let list1 = [1,2,3,4,5,6] | echo extend(list1,list1,6)
      " [1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6]

Extra-confusingly, this behaviour applies when the list is referenced with two different variables:

      let list1 = [1,2,3,4,5,6] | let list2 = list1 | echo extend(list1,list2,4)
      " [1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 5, 6]

This is totally bizarre to me. I can’t fathom a use for this functionality, and it seems like it would be really easy to invoke it by accident when you just wanted to insert one list into another and didn’t realize that the variables were referencing the same array.

The documentation says the following:

If they are |Lists|: Append {expr2} to {expr1}.

If {expr3} is given insert the items of {expr2} before item {expr3} in {expr1}.

When {expr3} is zero insert before the first item.

When {expr3} is equal to len({expr1}) then {expr2} is appended.

Examples:

        :echo sort(extend(mylist, [7, 5]))
        :call extend(mylist, [2, 3], 1)

When {expr1} is the same List as {expr2} then the number of items copied is equal to the original length of the List.

E.g., when {expr3} is 1 you get N new copies of the first item (where N is the original length of the List).


Does this make sense in a way that I’m not getting, or is it just an eccentricity?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T04:10:26+00:00Added an answer on May 14, 2026 at 4:10 am

    I think I actually figured this out as I was posting, but since I had the question all edited and all, I figured I might as well go through with it.

    extend() modifies the first list in place. The second parameter is read-only. So it looks like what it’s doing is duplicating the elements of the second list one by one as it inserts them into the first one. When both of those are the same lists, this doesn’t work so well. But it’s not smart enough to deal with this edge case, so things get whack.

    I haven’t found any documentation to back this up, but the hypothesis matches the evidence. I guess I could go read the source code for more info.

    This is still kind of weird since I would have thought that since these are “lists” it would work a bit differently, eg duplicating the entire list first and then plunking it down into the first one by just swapping around the pointers for the head/tail. This probably wouldn’t be any faster but it would take care of the edge case. Maybe it’s a feature, though. I’m still open to suggestions of reasons that this could actually be useful.

    Otherwise I guess I’ll file a bug report. Anybody know if vim has a public bug tracker somewhere? I feel weird writing to the email address without checking to see if it’s a known issue first.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I used javascript for loading a picture on my website depending on which small
I am reading a book about Javascript and jQuery and using one of the
I want use html5's new tag to play a wav file (currently only supported
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have an autohotkey script which looks up a word in a bilingual dictionary

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.