I have a piece of code like this in yaml.

security_group_sig_egress_ipv6: "allowed_remote_sig_egress_ipv6"
security_group_sig_ingress_ipv6: "allowed_remote_sig_ingress_ipv6"
security_group_int_egress_ipv4: "allowed_remote_int_egress_ipv4"
security_group_int_ingress_ipv4: "allowed_remote_int_ingress_ipv4"
security_group_int_egress_ipv6: "allowed_remote_int_egress_ipv6"
security_group_int_ingress_ipv6: "allowed_remote_int_ingress_ipv6"
security_group_om_egress_ipv4: "allowed_remote_om_egress_ipv4"
security_group_om_ingress_ipv4: "allowed_remote_om_ingress_ipv4"
security_group_om_egress_ipv6: "allowed_remote_om_egress_ipv6"
security_group_om_ingress_ipv6: "allowed_remote_om_ingress_ipv6"

as you can see the ":" is the delimiter between the two expressions. I want to transpose/swap the "key:value" pair in following way for the entire rectangular region:

allowed_remotexxxx: "security_groupxxx"
allowed_remotexxxx: "security_groupxxx"
allowed_remotexxxx: "security_groupxxx"
allowed_remotexxxx: "security_groupxxx"

I can use Anchored Transpose to transpose each line separately but not the whole rectangular region. Is there a way in Emacs to achieve this with minimum keystrokes?

  • Using a keyboard macro comes to mind. – Drew Sep 15 '17 at 16:16

Multiple cursors works well for these things. You can select : " and then invoke mc/mark-all-like-this.

Here's a demo for your use-case


You can use replace-regexp with the following patterns:

Replace 2 groups of characters separated by colon, space and quotes:

 ^\(.*\):\ "\(.*\)"$

With the capture groups swapped and reformatted:

\2: "\1"
  • Thanks for the tip. Can I run all these commands under a rectangular mark? – Black_Zero Sep 18 '17 at 11:15

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.