0

I want to get the first line of the buffer I'm actually in. More generally, I'm looking for a utility that returns Nth line from a supplied buffer. Is there some built-in for that?

  • FWIW, this operation is not provided by any built-in, because it's not something you'd normally ever need. So I suspect that there's another way to do what you want which does not require such an operation. – Stefan Jul 11 '16 at 14:14
  • @Stefan I want to do something with data in file, and for that I need some information that is stored in the first line. – MatthewRock Jul 11 '16 at 14:27
  • That much I could guess. – Stefan Jul 11 '16 at 17:38
4

This could be done with the next function (with or without narrowing):

(defun get-nth-line (number)
  "Get line with NUMBER"
  (save-restriction
    (widen)
    (save-excursion
      (goto-line number)
      (buffer-substring-no-properties (line-beginning-position)
                                      (line-end-position)))))

To get the first line execute

(get-nth-line 1)

If you want to get text properties, then use buffer-substring instead of buffer-substring-no-properties.

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.