7

How to open .org and .txt files with visual-line-mode automatically turned on?

Also, is there a way to configure cursor-movement so that cursor moves as if the visual line where a logical line? line-move-visual is not effecting Spacemacs evil mode keybindings: k (line up), j (line down).

I am running Emacs 25.2.1, with Spacemacs, evil mode, org-mode on Linux.

UPDATE 1

From .spacemacs file, after adding visual-line-mode

(defun dotspacemacs/user-config ()
  "Configuration function for user code.
This function is called at the very end of Spacemacs initialization after
layers configuration.
This is the place where most of your configurations should be done. Unless it is
explicitly specified that a variable should be set before a package is loaded,
you should place your code here."
  )
(add-hook 'org-mode-hook #'visual-line-mode)
(add-hook 'text-mode-hook #'visual-line-mode)

(setq org-use-speed-commands t)

;; Do not write anything past this comment. This is where Emacs will
;; auto-generate custom variable definitions.
(custom-set-variables

With emacs open to a txt file:

SPC h d k k

k runs the command evil-previous-line (found in evil-motion-state-map), which is
an interactive compiled Lisp function in ‘evil-commands.el’.

It is bound to <up>, k.

SPC h d v line-move-visual

line-move-visual is a variable defined in ‘simple.el’.
Its value is t

UPDATE 2

Tobias's advices code worked great; j and k move cursor as if the visual line where a logical line.

Then I installed org-drill. And org-drill worked fine until I restarted Emacs, then Emacs said:

Spacemacs encountered an error while loading your '~/.spacemaces' file.
Pick your editing style for recovery:
  vim
  emacs
  hybrid

When I completed the recovery, org-drill was missing from M-x, so I reinstall org-drill. Then restarting Emacs encountered the same error. So I removed Tobias's advices code from my .spacemacs file, and now Emacs and org-drill work fine.

UPDATE 3

I placed the new eval-after-load advices code in ~/.spacemacs. And Emacs started without errors. But long lines of text in .org file did not wrap at all.

So then I restored the previous advices code in ~/.spacemacs. And started Emacs from the terminal: emacs --debug-init. There where error messages. How to copy them to clipboard? Screen shot of error messages: enter image description here

wolfv
  • 1,403
  • 1
  • 13
  • 30

2 Answers2

9

You can switch on visual-line-mode in org-mode and text-mode by

(add-hook 'text-mode-hook #'visual-line-mode)

in your init-file (e.g. ~/.emacs). (Thanks to user lawlist for clarifying that org-mode bases on text-mode.) In emacs -Q the command visual-line-mode also sets line-move-visual buffer-locally to t therefore point movement should already work on visual lines without any customization. If you don't like that behavior set line-move-visual to nil.

emacs-version: GNU Emacs 25.1.50.2 (i686-pc-linux-gnu, GTK+ Version 3.10.8) of 2016-04-25

I cite here evil-previous-line from evil-commands.el:

(evil-define-motion evil-previous-line (count)
  "Move the cursor COUNT lines up."
  :type line
  (let (line-move-visual)
    (evil-line-move (- (or count 1)))))

You can see that line-move-visual is explicitly set to nil there. So the behavior you experience is intended.

On the other hand there is a command evil-next-visual-line defined right below evil-commands.el:

(evil-define-motion evil-next-visual-line (count)
  "Move the cursor COUNT screen lines down."
  :type exclusive
  (let ((line-move-visual t))
(evil-line-move (or count 1))))

There line-move-visual is explicitly set to t.

In evil-maps.el one finds the following two lines:

(define-key evil-motion-state-map "gj" 'evil-next-visual-line)
(define-key evil-motion-state-map "gk" 'evil-previous-visual-line)

So I guess you have just to press gj and gk instead of j and k for motion along visual lines.

Please, try that for yourself since I don't have evil installed.


You can paste the following advices into your init-file and try whether they fit your purpose. The functions evil-next-line and evil-previous-line are adviced such that they respect the value of line-move-visual as set by visual-line-mode.

(eval-after-load "evil-commands"
  '(progn
     (defun ad-evil-next-line (count) (evil-line-move (or count 1)))
     (advice-add #'evil-next-line :override #'ad-evil-next-line)
     (defun ad-evil-previous-line (count) (evil-line-move (- (or count 1))))
     (advice-add #'evil-previous-line :override #'ad-evil-previous-line)))
Tobias
  • 32,569
  • 1
  • 34
  • 75
  • 1
    The parent of `outline-mode` is `text-mode`, and the parent of `org-mode` is `outline-mode`. – lawlist Jun 07 '17 at 14:43
  • visual-line-mode soft-wraps the lines as expected, but line-move-visual is not effecting Spacemacs evil mode keybindings: k (line up), j (line down) – wolfv Jun 07 '17 at 15:57
  • @user2867994 Could you try `describe-key k` and `describe-key j`? Are those keys bound to `previous-line` and `next-line`? Maybe `evil-mode` is setting `line-move-visual`. Could you verify the value of that variable after loading a text file? – Tobias Jun 07 '17 at 17:20
  • @Tobias I appended **UPDATE** to the main question. – wolfv Jun 07 '17 at 20:32
  • @user2867994 I've added some text specific to `evil-mode`. Please try the suggested key sequences `gj` and `gk`. I do not have evil installed and also do not intend to install it, It is a relatively large package and I do not need it. – Tobias Jun 08 '17 at 01:51
  • @Tobias; My .spacemacs file was broken, now it's fixed and help works again. So I retested gj and gk. On my org-mode, gj and gk jump to the next heading. But my help describe-key gj and gk agree with your two lines from evil-maps.el. gj runs command evil-next-visual-line. gk runs command evil-previous-visual-line. – wolfv Jun 08 '17 at 05:31
  • @Tobias; Your advices worked great! And did not need the add-hook for visual-line-mode. j and k move cursor as if the visual line where a logical line. – wolfv Jun 08 '17 at 14:17
  • @Tobias; There seems to be a conflict with the advices code and org-drill. Details are in UPDATE 2, appended to main question. – wolfv Jun 09 '17 at 01:21
  • @user2867994 The error must occur when loading one of the packages. For a better diagnosis you should start emacs with `emacs --debug-init`. I changed `require` into `eval-after-load` in my answer. That changes the load-sequence and maybe it helps already. – Tobias Jun 09 '17 at 12:00
  • @Tobias; I appended **UPDATE 3** to the main question. – wolfv Jun 10 '17 at 01:26
  • Update: as of 2022 this is now done in `~/.spacemacs` in a function named `dotspacemacs/user-config()`. – midrare Apr 19 '22 at 02:15
  • What does the hash-mark in front of `'visual-line-mode` do? – Adam Mackler Aug 16 '22 at 12:58
0

If you are using general.el:

(general-swap-key nil 'motion
    ;; swap evil-next-line evil-next-visual-line
    "k" "gk"
    "j" "gj")