Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Emacs: How to keep the indentation level of a very long wrapped line

If there are lines in a file that are too long to be displayed on the screen, we can use word wrap. Now long lines are split into chunks visible on the screen.

Usually the code is structured and indenting is used for readability. If a line is wrapped into two (or more) lines, only the first line has right indenting and the other lines begin at the beginning of a row. Is there a way to keep all of the wrapped lines with the same indent level (or more generally different indent level)?

I have searched for a long long time, but still couldn't find a solution. This question is similar to a post for vim, but I didn't find a post or answer for emacs.

like image 743
Chris Avatar asked Nov 26 '12 05:11

Chris


1 Answers

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-xvisual-line-modeRET (to wrap long lines)
  • M-xadaptive-wrap-prefix-modeRET (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)) 
like image 175
François Févotte Avatar answered Sep 16 '22 14:09

François Févotte