Is there an apply-command-to-each-line-in-region command in emacs?

I have a bunch of links stored in an orgmode file, say ...

http://www.stackoverflow.com http://www.google.com http://www.github.com 

I can open each of them by pointing the cursor to the link and making Cc Co , and it conveniently opens my default browser and opens this link in a tab.

Now suppose I have 20 of these links. Is there a convenient way to apply such a function to each line in a selected region without recording an explicit macro?

I would think it looks like ...

 Select region Mx foreach-in-region Keystrokes to apply to each line: Cc Co 

And this is only for already defined functions. I guess the path would not be something like ...

 with cursor on first line of link F3 # to start record macro Cc Co down arrow F4 Select region (omitting the first line, since that now already opened in my browser) Cx Ck r 

Does it exist? If not, how would this be done? lisp this?

+5
source share
2 answers

You must write a macro for one line, and then use apply-macro-to-region-lines to execute it for all lines in the region. Cx ck r

Alternatively, you can use multiple cursors to create a cursor on each line and Cc Co to open everything. multiple-cursors will transform your usage patterns over time for the better if you give it a shot.

+7
source
 (defun do-lines (fun &optional start end) "Invoke function FUN on the text of each line from START to END." (interactive (let ((fn (intern (completing-read "Function: " obarray 'functionp t)))) (if (use-region-p) (list fn (region-beginning) (region-end)) (list fn (point-min) (point-max))))) (save-excursion (goto-char start) (while (< (point) end) (funcall fun (buffer-substring (line-beginning-position) (line-end-position))) (forward-line 1)))) 

Update after your comment -

Now it seems that you do not want to enter the name of the function, but press the key, and a binding to this key is applied to each line in the region (or buffer).

Something like the following will do this. However, keep in mind that a team often has special lines of behavior. For example, if you manage to press the Ck ( kill-lines ) key, then it already moves forward after each killed line. Since do-lines does not know which function (command) you will call, it goes to the next line after each call. For a command such as kill-lines , this will not do what it will: it will lead to the advancement of two lines, not one, thereby skipping lines. IOW, keep in mind that the code for do-lines cannot compensate for what a particular function can call, which it can call, which might not be as expected. Instead, he does what he says.

 (defun do-lines (command &optional start end) "Invoke COMMAND on the text of each line from START to END." (interactive (let* ((key (read-key-sequence-vector "Hit key sequence: ")) (cmd (lookup-key global-map key t))) (when (numberp cmd) (error "Not a valid key sequence")) (unless (commandp cmd) (error "Key `%s' is not defined" (key-description key))) (if (use-region-p) (list cmd (region-beginning) (region-end)) (list cmd (point-min) (point-max))))) (setq start (copy-marker start) end (copy-marker end)) (save-excursion (goto-char start) (while (< (point) end) (funcall command (buffer-substring (line-beginning-position) (line-end-position))) (forward-line 1)))) 
+5
source

All Articles