A quick way to repeatedly enter a variable name in Emacs?

廉价感情. 提交于 2019-12-06 12:47:15

yasnippet can actually be used to create a snippet on-the-fly:

(defun yas-one-line ()
  (interactive)
  (let ((snippet (delete-and-extract-region
                  (line-beginning-position)
                  (line-end-position))))
    (yas-expand-snippet snippet)))

Now just type:

menu.add_item($1,"$1")

and call yas-one-line. The above snippet is expanded by yasnippet!

You could try

(defvar sm-push-id-last nil)
(defun sm-push-id ()
  (interactive)
  (if (not sm-push-id-last)
      (setq sm-push-id-last (point))
    (text-clone-create sm-push-id-last sm-push-id-last
                       t "\\(?:\\sw\\|\\s_\\)*")
    (setq sm-push-id-last nil)))

after which you can do M-x sm-push-id RET , SPC M-x sm-push-id RET toto and that will insert toto, toto. Obviously, this would make more sense if you bind sm-push-id to a convenient key-combo. Also this only works to insert a duplicate pair of identifiers. If you need to insert something else, you'll have to adjust the regexp. Using too lax a regexp means that the clones will tend to overgrow their intended use, so they may become annoying (e.g. you type foo") and not only foo but also ") gets mirrored on the previous copy).

Record a macro. Hit F3 (or possibly C-x (, it depends) to begin recording. Type whatever you want and run whatever commands you need, then hit F4 (or C-x )) to finish. Then hit F4 again the next time you want to run the macro. See chapter 17 of the Emacs manual for more information (C-h i opens the info browser, the Emacs manual is right at the top of the list).

So, for example, you could type the beginning of the line:

menu.add_item(spamspamspam

Then, with point at the end of that line, record this macro:

F3 C-SPC C-left M-w C-e , SPC " C-y " ) ; RET F4

This copies the last word on the line and pastes it back in, but inside of the quotes.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!