How to Wrap Text in Emacs

Emacs Text Wrap

Emacs is a high level open source cross platform text editor. The first time you open Emacs you might start typing and realize your text rolls right past the right margin of your editor frame and continues into nowhere. For most users, wrapping text (or, soft wrap) is more visually appealing, and helps keep a natural flow.

There are two ways to enable text wrapping in emacs:

Emacs is an old text editor with its sort of dialect. In Emacs speak, the “Alt” key on your keyboard is known as the “Meta” or “M” key. So if you see instructions that involve the meta key in a keybinding you’ll see it written as M-<key>.

How to wrap text in current file

To enable text wrapping only in the file you’re working with, use the M-x function call and type visual-line-mode. (Remember, you can use tab auto completion to add the rest the of the command.)

Bear in mind, visual line mode will disable itself when you close this file or quit emacs.

How to wrap text in all files

In order to add text wrapping to all of your emacs files, you can enable the function globally in your emacs configuration file (~/.emacs) with this snippet of emacs lisp code:

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

That’s it!

Well done! You now know how to wrap text in emacs. Be sure to leave a comment or question below if you have any further questions.

Why or Why Not To Wrap Text in Emacs

When it comes to displaying how your text sits on a page, you can either soft wrap or hard wrap. A hard wrap, using “auto-fill-mode” instead of “visual-line-mode” will break your lines at a certain character limit.

Here is what visual line mode looks like in action, notice how text wraps itself neatly at the edge of the frame and continues below itself:

Emacs visual line mode

The danger of auto fill mode is that it will fill your document with carriage returns. In some circumstances this is fine. But you may find, depending on where your text is going, you might have to go back and remove those carriage returns.

Here’s what auto fill mode looks like in action, notice how the text breaks itself off before reaching the edge of the frame:

Emacs auto fill mode

Retaining long lines with visual line mode ensures against having a “do or die” situation involving erasing hundreds of line returns.

Christopher Maiorana Content Writer II

Christopher Maiorana joined the InMotion community team in 2015 and regularly dispenses tips and tricks in the Support Center, Community Q&A, and the InMotion Hosting Blog.

More Articles by Christopher

Was this article helpful? Join the conversation!