4

I want to get contents of the current line(the line with cursor) for my function. What command allows me to achieve this?

  • "the line with the cursor" depends on the selected window, since different windows can have different values for point. (It should never be a problem, but who knows...) – YoungFrog Aug 28 '15 at 15:48
12

Alternative to buffer-substring:

(thing-at-point 'line)
  • 3
    Be aware that this includes the final newline character if there is one (i.e. always, except maybe for the last line of the buffer) as well as the text properties. – YoungFrog Aug 28 '15 at 15:45
12

Use buffer-substring:

(buffer-substring (line-beginning-position) (line-end-position))

As Tobias noted, this also copies properties of text. If you want to avoid that, you might want to go with buffer-substring-no-properties instead:

(buffer-substring-no-properties (line-beginning-position) (line-end-position))
  • 3
    Often it is better to use buffer-substring-no-properties. – Tobias Aug 28 '15 at 14:05
  • @Tobias Included your insight in answer. – MatthewRock Aug 28 '15 at 14: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.