27

How can i collect the buffer's current line as a string value in elisp? i can do this,

(let (p1 p2 myLine)
 (setq p1 (line-beginning-position) )
  (setq p2 (line-end-position) )
  (setq myLine (buffer-substring-no-properties p1 p2))
)

but is there anyway i can do it in one line as,

(with-current-buffer get-current-line)
  • I'm not sure I understand. Do you simply want to do this without `p1` and `p2`, i.e. do it all inline, or are you looking for something special? – Wintermute Jan 17 '15 at 02:28
  • 3
    `let` doesn't just declare a variable, it also gives it a value. So your code creates vars, giving them the value `nil` only to immediately set them to something else. Better skip the intermediate step and do: `(let* ((p1 (line-beginning-position)) (p2 (line-end-position)) (myLine (buffer-substring-no-properties p1 p2))) ...)` – Stefan Jan 17 '15 at 04:03

1 Answers1

48

Use thing-at-point:

(thing-at-point 'line t)

but note that this also returns any newline at the end of the line.

Steve Vinoski
  • 19,847
  • 3
  • 31
  • 46
  • 3
    Another caveat: When point is at the end of the buffer, this call returns the same string, as if point were at the line aboce. – kdb Nov 27 '19 at 11:57