(shtarkov)

Transpose regions

#emacs

2024-10-20

Often times I find myself indecisive about how to lay out function arguments.

dump(repository, file);

Which one should go first? It's easy enough to reorder them with C-M-t (transpose-sexps) by placing point between the two.

But, until now, I never had a good solution for the function declaration:

void dump(repository::Repository &repository, const std::filesystem::path &file);

transpose-sexps does not produce the desired result:

void dump(repository::Repository &const, repository std::filesystem::path &file);

A Web search reveals that the usual approaches are defining Elisp commands in advance, installing packages or mucking about with the kill ring. Same goes for Vim!

Well, there's also transpose-regions, unbound by default. Let's see how that can work:

Did you see it? The trick is to save the mark 3 times — at the beginning and end of the first region, and at the beginning of the second region. Then leave point at the end of the second region and do transpose-regions, which I've bound to C-c t. Isearch comes in handy here, because it saves the mark where the search starts. I've modified it to leave point at the start of the match, which makes it even more convenient. If you find Isearch too cumbersome you can use something like iy-go-to-char.el or do-not-zap-up-to-char.