vi, emacs quick macros

vi

Some may be used to the magical “.” (period) command in vi, which magically repeats “the last thing you did”. Since this last thing can be quite complicated, this is some kind of easy macro in practice. Imagine you want to add a period at the end of every sentence of a list. Simple: type “A-<esc>” (i.e. A-hyphen-escape key) on the first sentence. Then go j.j.j.j.j.j.j. like mad. You can also type “4.” to do the thing 4 times (not very useful in this case).

emacs

But you know all this if you use vi anyway (why do you use it otherwise?). What I guess many people don’t know is that emacs also has quick macros. Type “C-x (” (i.e. control-x open parenthesis) and read the warning “defining keyboard macro”. “Keyboard” means this is not defined in some other way (e.g. in a config file). Now, type your macro. In the previous case you could type now C-a, “-“, down-arrow. Them “C-x )” to end your macro. Notice this includes the “down-arrow”, which moves to the next line (C-n is a more traditional way to do this). Type now C-e to execute the macro once. Type C-n C-x to do it several times (n is a number).

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s