2

Idea I will like to construct a simple emacs function that copies the yy functionality in vim. The function should yank a visual-line into kill ring without deleting the line.

Function Here is the function I came up with.


(defun vim-line-yank-func()
    (interactive)
    (beginning-of-visual-line)
    (mark-end-of-sentence 1)
    (end-of-visual-line)
    (copy-region-as-kill (mark) (point))
)

Let us run this program on some data. Consider the following text with original line breaks (► is position of the mark "\n" = newline mark).


Pellentesque dapibus suscipit ligula. ► Donec posuere augue in quam.  Etiam \n
vel tortor sodales tellus ultricies commodo.  Suspendisse potenti. \n
Aenean in sem ac leo mollis blandit. 

The expected answer is


Pellentesque dapibus suscipit ligula.  Donec posuere augue in quam.  Etiam
but the answer I keep getting is the following:

 Donec posuere augue in quam.  Etiam

I am befuddled as to why this is happening. When I run these commands in succession things seem to work fine. Thank you for your help!

  • 1
    You set the mark after the first sentence. Then you moved point to the end of the visual line. The region you copied is between point (end of v. line) and mark (end of the first sentence). You shouldn't have put mark at the end of the first sentence. – Drew Jan 6 '17 at 2:45
  • Thank you for the comment. Apologize for the dumb question. Doesn't the first line (beginning-of-visual-line) move the point to the beginning of the visual line? – DBS Jan 6 '17 at 21:49
  • 2
    Yes. But then you unnecessarily call mark-end-of-sentence, which sets the mark at the end of the sentence (forward from point). And then you move point to the end of the line. So the region is between point (end of line) and mark (end of the first sentence). See Stefan's answer, which just removes the call to mark-end-of-sentence. – Drew Jan 7 '17 at 1:23
5

Why mark-end-of-sentence?

I'd do it like this:

(defun vim-line-yank-func()
  (interactive)
  (save-excursion
    (beginning-of-visual-line)
    (copy-region-as-kill (point)
                         (progn (end-of-visual-line) (point)))))
  • Thank you for the function. Sorry for late response. I was using (mark-end-of-sentence) because I wanted to implement the vim visual-line function too. – DBS Jan 11 '17 at 19:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.