Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enabling and disabling word wrap automatically on different file extensions on Vim

I usually have to read .txt files with long lines, and at the same time edit some source file, and I like to see word wrap on the .txt files, and not in the ones that aren't.

Of course I can :set wrap and :set linebreak, but is there any way to make it automatucally, and dependent of the file extension?

like image 755
Martín Fixman Avatar asked Jun 30 '10 04:06

Martín Fixman


People also ask

How do I turn off word wrap in Vim?

Command to toggle word wrap in Vim While :set wrap will turn on word wrap in Vim and :set nowrap will turn off word wrapping, you can also use both commands with the ! (bang) symbol to toggle word wrap.

How do I turn on line wrap in Vim?

If you want to wrap lines in a specific area, move the cursor to the text you want to format and type gq followed by the range. For example, gqq wraps the current line and gqip wraps the current paragraph.

What is toggle word wrap?

The phrasing of “Toggle Word Wrap” is descriptive, but it can sound confusing at a cursory glance. It simply takes any text that runs off the side of the screen and consolidates it within the window and a nicely formatted style!


2 Answers

There are two options that I can think of. Firstly, you can use an autocmd as suggested by Tassos:

:au BufNewFile,BufRead *.txt set wrap

See:

:help autocmd

An alternative (that is probably more applicable if you've got multiple settings as you have suggested): create a file in the after/ftplugin directory of your vim configuration folder (see below) called txt.vim and it will be sourced whenever you open a .txt file. You can put it in the plain ftplugin directory (rather than after/ftplugin), but it any built-in settings for .txt files will then not be loaded.

Put any commands you want in this file:

" This is txt.vim in the ftplugin directory
set wrap
set linebreak

See:

:help after-directory
:help ftplugin

Vim Configuration Folder

On Windows this would typically be something like:

C:\Documents and Settings\%USERNAME%\vimfiles\after\ftplugin\txt.vim

(I think), or

C:\Program Files\Vim\vimfiles\after\ftplugin\txt.vim

or even:

C:\vim\vimfiles\after\ftplugin\txt.vim

On Linux, it is:

~/.vim/after/ftplugin/txt.vim

For more info, see:

:help runtimepath
like image 179
DrAl Avatar answered Oct 15 '22 01:10

DrAl


I guess :autocmd BufNewFile,BufRead *.txt set wrap should do the trick

like image 43
Tassos Avatar answered Oct 15 '22 02:10

Tassos