The package adaptive-wrap, which can be installed via the ELPA packaging system, should do what you want.
After having installed the package, just run the following commands:
- M-x
visual-line-mode
RET (to wrap long lines) - M-x
adaptive-wrap-prefix-mode
RET (to make wrapped lines indent nicely)
I also have the following snippet in my init.el
file to automatically activate adaptive-wrap-prefix-mode
along with visual-line-mode
:
(when (fboundp 'adaptive-wrap-prefix-mode)
(defun my-activate-adaptive-wrap-prefix-mode ()
"Toggle `visual-line-mode' and `adaptive-wrap-prefix-mode' simultaneously."
(adaptive-wrap-prefix-mode (if visual-line-mode 1 -1)))
(add-hook 'visual-line-mode-hook 'my-activate-adaptive-wrap-prefix-mode))