I want to get contents of the current line(the line with cursor) for my function. What command allows me to achieve this?
2 Answers
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))
-
4
-
1
Alternative to buffer-substring
:
(thing-at-point 'line)
-
3Be 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. Aug 28, 2015 at 15:45
point
. (It should never be a problem, but who knows...)