2

How can I navigate to a certain paragraph number?

If I want to move to the 33rd line in a file, I execute M-x goto-line <RET> 33, but there does not seem to be an equivalent goto-paragraph function.

I could write a function to do this for me (i.e. go to beginning of file, then execute forward-paragraph X number of times), but I am wondering if there is a built-in function for this already.

Ben
  • 587
  • 4
  • 11

2 Answers2

5

I'm not aware of a built-in function for this, but as you already noted you can write one:

(defun goto-paragraph (arg)
  "Go to paragraph ARG."
  (interactive "nGoto paragraph: ")
  (goto-char (point-min))
  (forward-paragraph arg))
glucas
  • 20,175
  • 1
  • 51
  • 83
  • Out of curiosity, is there a way to launch emacs and have it automatically navigate to a paragraph in the passed file, like you can do with line numbers? For example, I can do `emacs +33 file.txt` to open `file.txt` at line 33. Could I do this for paragraph 33? – Ben Mar 15 '17 at 14:46
  • I think so, but that is quite a bit more complicated and probably worth its own question! The short answer is that there is support for processing your own command-line options during Emacs startup. You might also be able to use `--funcall` or `--eval`. – glucas Mar 15 '17 at 14:51
2

Not sure if you were already aware of this or not, but you can interactively do this without writing a function per se, using the universal argument -- in my emacs forward-paragraph is bound to "M-}", so you can go to beginning of buffer, "C-u-33-M-}" and it will jump forward 33 paragraphs. You could even capture this to a keyboard macro with a kbd-macro-query inside to ask you how many paragraphs, and then save that macro off and bind it to a key. Just to let you know that there are ways to do this without writing any Emacs Lisp at all.

Willy Lee
  • 460
  • 4
  • 10