I've recently challenged myself to learn VIM, a text editor popular with computer programmers. One of the biggest obstacles was a feature that automatically truncates the length of lines in text files. You're typing, then the new line is magically (so it seems) started when the typing nears the right margin. This default behavior was quite different from other text editors that I've used. It introduced line breaks that were unwanted. This post was written for new users explains how to turn this feature on or off. The next level of setting default behavior is also explained.
The command that controls this length is textwidth or tw when abbreviated. To see your current textwidth, use this command. The question at the end provides the current status of a variety of commands.
:set tw?
The installed value of gVim (9.1.744) is textwidth=78. This will wrap any text beyond 78 characters by starting a new line. Sometimes this is desirable. However, it can also introduce problems if these line breaks are unwanted. It's important to know how to turn this feature on and off to get the behavior you want.
Let's say your current value is the default of 78. To turn it completely off, use this command:
:set tw=0
Conversely, the automatic breaks can be set back to 78 or whatever the desired length is by specifying a value.
:set tw=78
Turning this feature on and off was a big step forward. The default though was always 78. This caused problems because it would start breaking at unexpected times when I didn't remember to reset the value to zero. It was annoying to unexpectedly have a line break, then time was wasted fixing the break.
The next step to address this problem is change the default configuration. Tracking this down took some significant detective work. Here's how to track it down and change it to off or change it your desired break length.
This command will reveal the location of the VIM configuration file, _vimrc on Windows.
:echo $MYVIMRC
Here's an example response. On my Windows PC, the default location is:
C:\Program Files\Vim\_vimrc
Setting textwidth in this configuration file is not enough though, contrary to what some online discussions might suggest. The plot runs deeper. The top of the _vimrc file contains a reference to another file. Here's the relevant part of _vimrc.
" Vim with all enhancements
source $VIMRUNTIME/vimrc_example.vim
On my PC, this file is located in C:\Program Files\Vim\vim91\vimrc_example.vim. This second configuration file contains some enhancements that beginners might find useful or attractive. Here's the critical command from this example configuration file.
" For all text files set 'textwidth' to 78 characters.
autocmd FileType text setlocal textwidth=78
This understanding - two configuration files - can lead to a couple of options for changing the default behavior. One possibility is to comment out the reference to vimrc_example.vim in the _vimrc file and add tw=0. This works, but it also causes some possibly desirable default behaviors from the example file to be lost. Another solution is to change the textwidth=78 command in the vimrc_example.vim file to zero or the desired value. This is what I opted to do. Another possibility would be to copy the desired parts of the example file to _vimrc and then remove the reference to the example file. This might be the most elegant solution.
I hope this detective work will be useful for some VIM beginner who is stumped, like I was.
No comments:
Post a Comment