(or emacs irrelevant)

Swiper and visual-line-mode

In case you encounter files with very long lines, the built-in M-x visual-line-mode might come in handy. Although 1MB long one-line files are a disaster for Emacs, there's no problem handling smaller files with long lines that result from bad markdown renderers (notably Github). They way visual-line-mode works, is it inserts virtual newlines in appropriate positions, so that no line is truncated. The advantage is that these newlines aren't written to the file when the buffer is saved (otherwise, a simple M-q (fill-paragraph) would work too).

Today I've added visual-line-mode support to swiper, so handling these files should be even easier now. The change was surprisingly simple: just two checks for visual-line-mode variable and these basic replacements:

  • forward-line -> line-move,
  • line-beginning-position -> beginning-of-visual-line,
  • line-end-position -> end-of-visual-line.

By the way, visual-line-mode is one of the small number of commands that I call by name instead of by key chord. But thanks to counsel-M-x and smex calling this command by name is as short as C-t v RET. To explain a bit more, here's my setup:

;; make sure to install smex
(global-set-key (kbd "C-t") 'counsel-M-x)

The default command bound to C-t is transpose-chars - a highly situational and ultimately counter-productive command (M-DEL and re-typing the word is faster than surgically navigating to the transpose point and calling transpose-chars, plus it trains the hands to type the word correctly next time). Anyway, C-t is a prime binding for launching interactive commands. And since smex is installed it remembers that my most used command that starts with "v" is visual-line-mode - so it's the first one that gets selected after C-t v. This means that I can just press RET and be done with it.

swiper-visual-line-mode.png

Finally, related to visual-line-mode, here's a command I use to negate M-q (fill-paragraph) (source):

(global-set-key (kbd "C-M-q") 'ora-unfill-paragraph)

(defun ora-unfill-paragraph ()
  "Transform a paragraph into a single line."
  (interactive)
  (let ((fill-column (point-max)))
    (fill-paragraph nil t)))

Thanks to @joostkremers for the suggestion in #227.