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

  • Home
  • SEARCH
  • 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 8753829
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:30:06+00:00 2026-06-13T13:30:06+00:00

As part of learning ELisp, I am trying to make a function that makes

  • 0

As part of learning ELisp, I am trying to make a function that makes a copy of the current line below the current line (duplicates the line). What I have so far works pretty well, except on the last line of the buffer. If on the last line, then the line just gets pasted at the end of the line rather than below it.

Here is my code:

(defun duplicate-line ()
  "duplicate the current line"
  (interactive)
  (save-excursion
    (kill-ring-save (line-beginning-position) (line-beginning-position 2))
    (goto-char (line-beginning-position 2)) ; goto the start of the next line
    (yank)
   )
  (next-line)
)

Is there a better way of doing this? I would also appreciate any other advice concerning writing elisp.

  • 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-06-13T13:30:07+00:00Added an answer on June 13, 2026 at 1:30 pm

    I guess this happens only when the last line doesn’t end with a newline character.

    The following function inserts a newline if necessary, and avoids using the kill-ring.

    (defun duplicate-line ()
      (interactive)
      (let* ((pos-end (line-beginning-position 2))
             (line    (buffer-substring (line-beginning-position) pos-end)))
        (goto-char pos-end)
        (unless (bolp) (newline))
        (save-excursion ;; leave point before the duplicate line
          (insert line))))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have had to start to learning C as part of a project that
query level: beginner As part of a learning exercise I have written code that
I'm trying to pick up Python. As part of the learning process I'm porting
I have been trying to learn about classes in PHP and part of my
I'm learning to use the write function and am trying to print only a
I am learning to use HTML5 WebSockets and as part of that I am
As part of my constant learning curve into what you can do to make
I just started learning Scala, and as part of the process, I've been trying
I have a problem. I'm learning from MTaulty's video: http://channel9.msdn.com/blogs/mtaulty/prism--silverlight-part-2-dependency-injection-with-unity . And I have
As part of learning Haskell, I am trying to implement my own version of

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.