2

Is there any way to enable the following behavior. Let's say i have some code:

def main(args: List[String]): Unit = {

}

Of cause the blank line between braces is indented, but when i move cursor to that position i need to hit tab to place it on the proper column according to my indentation settings, can i enable such behavior by default? E.g in the given example, if i move cursor up or down, the cursor would be in this position:

def main(args: List[String]): Unit = {
|    
}

But i want it to be in this:

def main(args: List[String]): Unit = {
  |    
}
2

The command back-to-indentation (bound to M-m by default) moves point to the first non-whitespace character on the current line.

Arranging for it to be called whenever you move the cursor is a matter of advising the correct function. For example, the following will cause back-to-indentation to be called whenever you move up:

(advice-add 'previous-line :after #'back-to-indentation)


However, I strongly advise against such a customization: you most probably want to go to the indentation only in a limited set of occasions (maybe for whitespace-only lines?). Most of the time, you probably expect your cursor to stay on the same column when moving up or down.

|improve this answer|||||
  • The wrinkle is that back-to-indentation will not work on a whitespace-only line precisely because it moves to the first non-whitespace character on the line -- of which there are none. – Dan Nov 15 '14 at 23:28
  • @Dan Well, I tested that before posting and it seems to work on my setup (i.e. in whitespace-only lines, back-to-indentation moves to end of line). Doesn't it work that way in your setup? – François Févotte Nov 16 '14 at 9:50
  • you're right! It turns out that what I observed is a quirk of evil-mode rather than a quirk in back-to-indentation. – Dan Nov 16 '14 at 11:39
2

Old question, but:

You can make it so that all the common ways of moving around do an auto-indent, like this:

(advice-add 'previous-line :after #'indent-according-to-mode)
(advice-add 'next-line :after #'indent-according-to-mode)
(advice-add 'scroll-up-command :after #'indent-according-to-mode)
(advice-add 'scroll-down-command :after #'indent-according-to-mode)
(add-hook 'isearch-mode-end-hook #'indent-according-to-mode)

This has side effects; as you move around, (a) indentation on non-blank lines will be fixed up, and (b) if you move to a non-blank line within the indentation, the cursor will be automatically pushed to the beginning of the text as with back-to-indentation. Maybe these are good side effects. However, there is a shortcoming that you can arrive on lines in other ways, like for example if the blank line happens to be at the end of the buffer and you do Alt->, or when you pop the mark ring. But the above might cover enough of the movement scenarios to meet the requirement. I couldn't find a hook that is called after cursor movement of any kind, so if you wanted to be more thorough you might have to use post-command-hook which starts to seem like a pretty invasive change.

A bigger issue is that this approach leaves trailing whitespace everywhere in your document. (To see it, do Ctrl-Alt-s [[:space:]]+$ or turn on global-whitespace-mode). So you could also patch the navigation commands to remove the trailing whitespace before moving away, but even if you do there are ways in which the cleanup will fail to be invoked, like if you save while positioned on an indented blank line. Probably a better approach is not to bother with incremental clean-up, and just sweep the whole document on save, with something like (add-hook 'before-save-hook 'delete-trailing-whitespace) in your config [ref].)

In all, this is probably not a good thing to do to Emacs because it coarsens your control over the content of the document.

|improve this answer|||||
  • No worries, thank you for a great answer, appreciate it – 4lex1v Jan 14 at 14:47

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.