7

I want to be able to toggle soft-wrapped lines in org-mode. My .emacs file has this code:

;;;;;;;;;;;;;;
;; Org Mode ;;
;;;;;;;;;;;;;;

(global-set-key "\C-cl" 'org-store-link)
(global-set-key "\C-ca" 'org-agenda)
(global-set-key "\C-cc" 'org-capture)
(global-set-key "\C-cb" 'org-iswitchb)

;; Start-up with soft-wrap enabled
(setq org-startup-truncated nil)  ; This works
;; Toggle soft-wrap with super-q
(define-key org-mode-map "s-q" 'toggle-truncate-lines)  ; This doesn't

I would like to map it to something like super-q or F7. Is there something wrong with my syntax? Neither works.

4
  • 2
    (setq truncate-lines nil) equals wrap. (setq truncate-lines t) equals don't wrap. You can make a function (defun () "doc-string" (interactive) (message "This is my function)). You can make a keyboard shortcut . . . . Have fun exploring the possibilities and enjoy your Emacs experience. If you want to break words at word-boundaries at the window edge, then use word-wrap t
    – lawlist
    Commented Jan 24, 2017 at 5:33
  • Thanks. I'm trying a few things. (global-set-key (kbd "s-q") 'toggle-truncate-lines) seems to work for toggling it, but I'm not sure if that is the best way.
    – R891
    Commented Jan 24, 2017 at 5:35
  • 3
    It is only those two variables -- truncate-lines and the optional word-wrap. You may also enjoy using M-x visual-line-mode to toggle on and off.
    – lawlist
    Commented Jan 24, 2017 at 5:36
  • FWIW, even more solutions are proposed for this problem at this pre-e.s.c SuperUser post
    – TomRoche
    Commented Feb 19, 2021 at 20:23

3 Answers 3

3

A bit more manual per file but you can do do:

# -*- truncate-lines: nil -*-

At the top of the org-mode file.

1
  • This didn't work for me, which is a shame as I want to add this on a per-file basis. Is there something else I need to add to .emacs or somewhere to make this soft-wrap the lines? Commented Jun 14, 2023 at 15:28
3

To avoid spelling mistakes you should try to use the kbd function for writing the keybinding. My setting therefor looks like:

(global-set-key (kbd "C-x p") 'toggle-truncate-lines)

because I use that keybinding globally (e.g. also in any prog-mode). For your org-mode-map -only change it would look like this:

(define-key org-mode-map (kbd "s-p") 'toggle-truncate-lines)

otherwise you will have to consider regular expression syntax.

Therefor see https://www.gnu.org/software/emacs/manual/html_node/elisp/Key-Binding-Commands.html:

(global-set-key (kbd "C-x C-\\") 'next-line)
(global-set-key [?\C-x ?\C-\\] 'next-line)
(global-set-key [(control ?x) (control ?\\)] 'next-line)
1

If you want soft-warp applied to all org files on permanent basis, just put the code below in your init file.

(add-hook 'org-mode-hook
      (lambda ()
        (toggle-truncate-lines nil) ))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.