8

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

Drew
  • 77,472
  • 10
  • 114
  • 243
MatthewRock
  • 1,483
  • 13
  • 28
  • "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

2 Answers2

17

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))
MatthewRock
  • 1,483
  • 13
  • 28
14

Alternative to buffer-substring:

(thing-at-point 'line)
Dan
  • 32,980
  • 7
  • 102
  • 169
  • 4
    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