Workaround for vi “Terminal too wide” problem
Use the Unix command line and sooner or later, you will be editing text files. One of the best ways of doing that is with the vi editor. It is available as standard on almost every unix/linux system. While other editors are available (ed, emacs, vim, etc), vi is quick and convenient. It offers a good balance between usability and ubiquity.
This article offers a workaround for the annoying “Terminal too wide” problem encountered by vi users on Solaris.
Vi was originally written for screens (terminals) which were 80 characters wide. In a modern windowing environment, the terminal has been replaced by virtual terminal apps – xterm, lterm, Terminal and many others. The width of a virtual terminal depends on how much big you make the window. On a large screen it could easily be 200 characters or more.
Weakness of Solaris Vi Distribution
Linux systems handle this fine. But on Solaris, vi will fail if your terminal is more than about 160 characters wide:
Terminal too wide
:
1 more file to edit:q
Stty Adjustment
One workaround is to adjust the terminal “columns” setting before the edit, 163 being the maximum that vi seems willing to accept. Eg:
# stty columns 160 # vi myfile (do edit, quit vi)
However, this will leave you with a terminal 160 columns wide, even if your terminal window (eg. xterm) is much wider. Text will wrap at 160 characters, leaving the right side of the terminal window unused, and generally making a mess. No problem, just change back the setting. For example:
# stty columns 200
And things look normal again. To save typing every time you use vi in a wide terminal, the following aliases can be set up.
Automated stty Adjustment
Write a background function to do it. This is for bash:
# myvi () { ocols=`tput cols`; stty columns 160; /usr/bin/vi $1; stty columns $ocols; echo myvi done; } # alias vi=myvi
Now every time you invoke “vi”, the alias and the “myvi” function act to set the width narrow enough for vi (160 characters) before your edit, and then put it back to its original value afterwards. Add the above definitions to ~/.bash_profile (or wherever) to make them permanent. NB On your system vi may be located somewhere else, eg /bin/vi. Adjust the function accordingly.
Improvement Suggestion
Oracle/Sun could edit a few lines of source to remove this annoyance altogether. Update November 2013 – it seems they have done this, as the problem does not seem to occur in later releases of Solaris 10.
Working with Multiple Files
If you often use vi to edit more than one file at the same time, take note of Joe’s suggestion below.
In your function (alias), you could first use “tput cols” to get the number of columns currently in use (usually set when opening the terminal window). Once you are done, set the width back to what it was instead.
Good stuff Pascal. Article updated to use your “tput cols” idea to get the window width.
You should probably also test to make sure that the current number of columns is greater than 160 before setting it, otherwise bad things might happen on small terminals; the code I’m using (which also overrides vi rather than defining a myvi alias) is
VI_COLS=163
vi () {
local original_cols=$(tput cols)
if [ original_cols -gt VI_COLS ]; then
stty columns “$VI_COLS”
“$(which vi)”
stty columns “$original_cols”
else
“$(which vi)”
fi
}
Great solution thanks 🙂
I’d suggest changing /usr/bin/vi $1 to /usr/bin/vi $* so if your editing multiple files myvi will handle it.
Cheers Joe. I have never used vi for editing multiple files, but for those that do, a note has been added to the end of the article.
alias vi=’COLUMNS=160 vi’
isn’t that a touch more elegant?
Hi Ben. Not exactly. It doesn’t work on Solaris, for which the article was written:
$ uname -a
SunOS unknown 5.10 Generic_118855-19 i86pc i386 i86pc
$ COLUMNS=160 vi
Terminal too wide
:q
You perhaps tried the command on a modern Linux system, where “vi” is just a soft link to the vim binary. If so, you were actually invoking vim, which doesn’t have the issue anyway.
Vim also appears to have replaced vi on Solaris from version 11 onward.
Jim.