Vim Tips Wiki
No edit summary
(8 intermediate revisions by 6 users not shown)
Line 3: Line 3:
 
|previous=416
 
|previous=416
 
|next=419
 
|next=419
|created=February 3, 2003
+
|created=2003
 
|complexity=basic
 
|complexity=basic
|author=Dave
+
|author=
 
|version=6.0
 
|version=6.0
 
|rating=157/106
 
|rating=157/106
Line 11: Line 11:
 
|category2=
 
|category2=
 
}}
 
}}
  +
Vim may beep or may flash its window if an error occurs. This tip explains the settings that control these functions, and shows how to disable beeping and flashing in both terminal Vim and GUI Vim (gvim). See [[#Disable beep and flash with an autocmd|below]] if you just want the solution.
How do you stop gvim from beeping on error?
 
   
==Comments==
+
==Settings==
  +
The <code>'errorbells'</code> (abbreviated as <code>'eb'</code>) option is off by default. If it is on (that is, if <code>:set errorbells</code> is used), Vim will either beep or flash its screen when an error message is displayed. For example, the command <code>:set xyz</code> displays an error message because <code>'xyz'</code> is not a valid option. If <code>'errorbells'</code> is on, the error message will attempt to either sound a beep or flash Vim's screen.
:set vb
 
   
  +
The <code>'errorbells'</code> option has no effect on the beeps that Vim makes when an error message is not displayed (for example, on pressing Esc when in normal mode).
----
 
I tried :set vb. In a GUI, it's just murder on the eyes. I'd much rather suffer through the bells.
 
   
  +
The <code>'visualbell'</code> (abbreviated as <code>'vb'</code>) option is off by default.
----
 
  +
If it is on (that is, if <code>:set visualbell</code> is used), Vim will flash its screen instead of sounding a beep (that is, at times when a beep would have occurred, the screen will flash instead).
Paste the following function into your vimrc:
 
   
  +
The <code>'t_vb'</code> option, by default, is set to a code that will cause the screen to flash. Starting the GUI (which occurs after vimrc is read) resets <code>'t_vb'</code> to its default value. If <code>'t_vb'</code> is cleared, Vim will never flash the screen—however, it has to be cleared ''after'' the GUI has started.
  +
  +
If <code>'t_vb'</code> is cleared and <code>'visualbell'</code> is set, no beep and no flash will ever occur. That is achieved by entering the command <code>:set vb t_vb=</code> after the GUI has started—see [[#Disable beep and flash with an autocmd|method 1]] or [[#Disable beep and flash with gvimrc|method 2]] below.
  +
  +
==Demonstration==
  +
A demonstration is useful to show how the options work. Start Vim with no initializations from vimrc or gvimrc, but using <code>'nocompatible'</code> mode:
 
<pre>
 
<pre>
  +
vim -N -u NONE
function! Errorbells_off(...)
 
" control Vim's audio and visual warnings
 
" * Arguments:
 
" "beep": turn off just beeping
 
" "flash": turn off just flashing
 
" (empty): both off
 
" * Must be initialized after the GUI starts!
 
" off
 
if a:0 == 0
 
let myeb = ""
 
else
 
let myeb = a:1
 
endif
 
if myeb ==? "flash"
 
" audibly beep on error messages
 
set errorbells
 
" Screen flash on error (See also 't_vb')
 
set novisualbell
 
set t_vb=
 
elseif myeb ==? "beep"
 
" audibly beep on error messages
 
set noerrorbells
 
" Screen flash on error (See also 't_vb')
 
set visualbell
 
set t_vb&
 
elseif myeb ==? ""
 
" audibly beep on error messages
 
set noerrorbells
 
" Screen flash on error (See also 't_vb')
 
set visualbell
 
set t_vb=
 
endif
 
endfunction
 
 
</pre>
 
</pre>
   
  +
In Vim, press Esc. If your sound system is working, you should hear your system's version of a "bell" (often a beep). Enter the command <code>:set xyz</code> and observe that an error message is displayed, but no beep occurs. Display the default settings by entering:
Re-start Vim and enter
 
  +
<pre>
 
:set eb? vb? t_vb?
  +
</pre>
   
  +
Vim will display something like (the <code>^[...</code> depends on your system):
:call Errorbells_off("beep") -- turns off beeps
 
  +
<pre>
:call Errorbells_off("flash") -- turns off flashes
 
 
noerrorbells
:call Errorbells_off() -- turns off both
 
 
novisualbell
  +
t_vb=^[...
  +
</pre>
   
  +
Due to <code>noerrorbells</code>, no beep occurs when an error message is displayed. However, other "errors" such as pressing Esc when in normal mode will cause a beep.
We use this the Cream for Vim project (http://cream.sourceforge.net).
 
   
  +
Enter the command <code>:set vb</code> to set <code>visualbell</code>. Entering <code>:set xyz</code> will give the same result as before. However, pressing Esc should flash Vim's screen (a "visual bell").
----
 
  +
Removing complexity of the program. Just type following in your vimrc:
 
  +
Enter the command <code>:set t_vb=</code> to clear the <code>t_vb</code> option (so it will do nothing). Entering <code>:set xyz</code> will give the same result as before (a message but no beep or flash). However, pressing Esc should also do nothing (no beep and no flash). The <code>t_vb</code> option controls how Vim flashes the screen (on both Unix and Windows systems, for both terminal and gui Vim). If the option is empty, Vim does not flash the screen.
   
  +
==Disable beep and flash with an autocmd==
  +
To disable beeping (aka "bell") and window flashing, put the following in your [[vimrc]]. This works on Unix and Windows systems for both terminal and GUI Vim.
 
<pre>
 
<pre>
set noerrorbells
+
set noerrorbells visualbell t_vb=
set visualbell
+
autocmd GUIEnter * set visualbell t_vb=
set t_vb=
 
 
</pre>
 
</pre>
   
  +
The same commands can be entered using abbreviations:
----
 
 
<pre>
 
<pre>
set noerrorbells
+
set noeb vb t_vb=
  +
au GUIEnter * set vb t_vb=
  +
</pre>
  +
  +
The above will give an error message if run on a Vim which was not compiled with the <code>+autocmd</code> feature. To avoid that possibility, use:
  +
<pre>
  +
set noerrorbells visualbell t_vb=
 
if has('autocmd')
 
if has('autocmd')
autocmd GUIEnter * set vb t_vb=
+
autocmd GUIEnter * set visualbell t_vb=
 
endif
 
endif
 
</pre>
 
</pre>
   
  +
==Disable beep and flash with gvimrc==
The "autocmd" tip worked for me when nothing else did on VIM 6.3 / Sparc / Solaris 8.
 
  +
Another method to disable beeping and flashing is to put a command into your vimrc to handle terminal Vim, and another command into your gvimrc to handle gvim. This works on Unix and Windows systems.
   
 
Put the following in vimrc:
  +
<pre>
  +
set noeb vb t_vb=
  +
</pre>
  +
  +
Put the following in gvimrc:
  +
<pre>
 
set vb t_vb=
  +
</pre>
  +
  +
The gvimrc file is in the same directory as vimrc; see [[vimrc]] for the proper file name.
  +
  +
==Comments==
  +
Superb. Worked well for me.
 
----
  +
Wow. Nothing feels better than disabling those bells when you hit Esc if you're already outside of insert mode. That bell noise is even more annoying than those alarm clocks from the 80's. [[User:sjohnson|sjohnson]]
 
----
 
----
  +
Thanks so much. Very clear and helpful. But why am I unable to do a cut and paste from this wiki? What is the point of making people type up the commentary? --March 3, 2012
  +
:You should be able to copy text from the wiki! I just tried it (again) in both Firefox 10 and Internet Explorer 8 and they worked fine. At some time it was hard to see that text had been selected because the "text selected" colour was almost exactly the same as the normal colour. However, when I tried it now, the selected text was very easy to see, and Ctrl-C copied the selection. If you still have a problem, please reply here with details of what browser you are using, and what you did. [[User:JohnBeckett|JohnBeckett]] 00:48, March 4, 2012 (UTC)

Revision as of 06:30, 18 May 2013

Tip 418 Printable Monobook Previous Next

created 2003 · complexity basic · version 6.0


Vim may beep or may flash its window if an error occurs. This tip explains the settings that control these functions, and shows how to disable beeping and flashing in both terminal Vim and GUI Vim (gvim). See below if you just want the solution.

Settings

The 'errorbells' (abbreviated as 'eb') option is off by default. If it is on (that is, if :set errorbells is used), Vim will either beep or flash its screen when an error message is displayed. For example, the command :set xyz displays an error message because 'xyz' is not a valid option. If 'errorbells' is on, the error message will attempt to either sound a beep or flash Vim's screen.

The 'errorbells' option has no effect on the beeps that Vim makes when an error message is not displayed (for example, on pressing Esc when in normal mode).

The 'visualbell' (abbreviated as 'vb') option is off by default. If it is on (that is, if :set visualbell is used), Vim will flash its screen instead of sounding a beep (that is, at times when a beep would have occurred, the screen will flash instead).

The 't_vb' option, by default, is set to a code that will cause the screen to flash. Starting the GUI (which occurs after vimrc is read) resets 't_vb' to its default value. If 't_vb' is cleared, Vim will never flash the screen—however, it has to be cleared after the GUI has started.

If 't_vb' is cleared and 'visualbell' is set, no beep and no flash will ever occur. That is achieved by entering the command :set vb t_vb= after the GUI has started—see method 1 or method 2 below.

Demonstration

A demonstration is useful to show how the options work. Start Vim with no initializations from vimrc or gvimrc, but using 'nocompatible' mode:

vim -N -u NONE

In Vim, press Esc. If your sound system is working, you should hear your system's version of a "bell" (often a beep). Enter the command :set xyz and observe that an error message is displayed, but no beep occurs. Display the default settings by entering:

:set eb? vb? t_vb?

Vim will display something like (the ^[... depends on your system):

noerrorbells
novisualbell
  t_vb=^[...

Due to noerrorbells, no beep occurs when an error message is displayed. However, other "errors" such as pressing Esc when in normal mode will cause a beep.

Enter the command :set vb to set visualbell. Entering :set xyz will give the same result as before. However, pressing Esc should flash Vim's screen (a "visual bell").

Enter the command :set t_vb= to clear the t_vb option (so it will do nothing). Entering :set xyz will give the same result as before (a message but no beep or flash). However, pressing Esc should also do nothing (no beep and no flash). The t_vb option controls how Vim flashes the screen (on both Unix and Windows systems, for both terminal and gui Vim). If the option is empty, Vim does not flash the screen.

Disable beep and flash with an autocmd

To disable beeping (aka "bell") and window flashing, put the following in your vimrc. This works on Unix and Windows systems for both terminal and GUI Vim.

set noerrorbells visualbell t_vb=
autocmd GUIEnter * set visualbell t_vb=

The same commands can be entered using abbreviations:

set noeb vb t_vb=
au GUIEnter * set vb t_vb=

The above will give an error message if run on a Vim which was not compiled with the +autocmd feature. To avoid that possibility, use:

set noerrorbells visualbell t_vb=
if has('autocmd')
  autocmd GUIEnter * set visualbell t_vb=
endif

Disable beep and flash with gvimrc

Another method to disable beeping and flashing is to put a command into your vimrc to handle terminal Vim, and another command into your gvimrc to handle gvim. This works on Unix and Windows systems.

Put the following in vimrc:

set noeb vb t_vb=

Put the following in gvimrc:

set vb t_vb=

The gvimrc file is in the same directory as vimrc; see vimrc for the proper file name.

Comments

Superb. Worked well for me.


Wow. Nothing feels better than disabling those bells when you hit Esc if you're already outside of insert mode. That bell noise is even more annoying than those alarm clocks from the 80's. sjohnson


Thanks so much. Very clear and helpful. But why am I unable to do a cut and paste from this wiki? What is the point of making people type up the commentary? --March 3, 2012

You should be able to copy text from the wiki! I just tried it (again) in both Firefox 10 and Internet Explorer 8 and they worked fine. At some time it was hard to see that text had been selected because the "text selected" colour was almost exactly the same as the normal colour. However, when I tried it now, the selected text was very easy to see, and Ctrl-C copied the selection. If you still have a problem, please reply here with details of what browser you are using, and what you did. JohnBeckett 00:48, March 4, 2012 (UTC)