History Report a problem
Article Edit this page Discussion

Toggle spellcheck with function keys

From Vim Tips Wiki

(Redirected from VimTip1224)
Jump to: navigation, search

Tip 1224 Previous TipNext Tip

Created: May 9, 2006 Complexity: basic Author: Alex N Minimum version: 7.0 Karma: 27/14 Imported from: Tip#1224


If you have gvim toolbars disabled, here's an easier and quicker way to toggle the spellcheck functionality:

:map <F5> :setlocal spell! spelllang=en_us<cr>

Remap to whichever Fx key you require, or switch the dictionary to use.

[edit] Comments

Also for when you're in insert mode:

:imap <F5> <ESC>:setlocal spell! spelllang=en_us<cr>i

This works better. The previous tip's :imap will change the position of the cursor if the cursor was at the very end of the linee.

:imap <F5> <C-o>:setlocal spell! spelllang=en_us<cr>

I just used the following:

nn <F7> :setlocal spell! spell?<CR>

<F7> is the same as most Windows applications. spelllang=en_us is by default?


" For those with sadly no function keys available
" toggle spelling use \s
imap <Leader>s <C-o>:setlocal spell! spelllang=en_gb<cr>
nmap <Leader>s :setlocal spell! spelllang=en_gb<cr>

let g:myLang = 0
let g:myLangList = [ "nospell", "de_de", "en_gb" ]
function! MySpellLang()
  "loop through languages
  if g:myLang == 0 | set nospell | endif
  if g:myLang == 1 | setlocal spell spelllang=de_de | endif
  if g:myLang == 2 | setlocal spell spelllang=en_gb | endif
  echo "language:" g:myLangList[g:myLang]
  let g:myLang = g:myLang + 1
  if g:myLang >= len(g:myLangList) | let g:myLang = 0 | endif
endf
map <F7> :call MySpellLang()<CR>

Someone can maybe implement it a better way, but so I can toggle through.


Fiddled around a little, now it does not move the cursor anymore, and I don't have the slightest idea why. Here is that part from my .vimrc:

"switch spellcheck languages
let g:myLang = 0
let g:myLangList = [ "nospell", "de_de", "en_us" ]
function! MySpellLang()
  "loop through languages
  let g:myLang = g:myLang + 1
  if g:myLang >= len(g:myLangList) | let g:myLang = 0 | endif
  if g:myLang == 0 | set nospell | endif
  if g:myLang == 1 | setlocal spell spelllang=de_de | endif
  if g:myLang == 2 | setlocal spell spelllang=en_us | endif
  echo "language:" g:myLangList[g:myLang]
endf

map <F7> :call MySpellLang()<CR>
imap <F7> <C-o>:call MySpellLang()<CR>

Don't know what I changed, but the most important thing is that it works now. I also changed the order of the commands, because before the first hit of F7 would only display the current state and did not change anything, now it directly switches to the next language.


It would be better to set the "myLang" variable local for each buffer, because the spell option is also local. In case you use two or more buffers and activate "en_gb" for one, "g:myLang" contains 2 now if you want to activate it for another one, you have to press <F7> three times.

So change each occurrence of g:myLang to b:myLang and define it for each buffer when the function is called for the first time. For this: remove the "let g:myLang = 0" line and add the following statement to the function directly after the function declaration:

if !exists( "b:myLang" )
  let b:myLang=0
endif

Rate this article:

Share this article:

Hubs Highlights International Sites Wikia messages
Entertainment
Gaming
Cartoons & Comics
Science Fiction
Hobbies
Sports
See all...
Grand Theft Auto
Pushing Daisies
Legend of Zelda Wiki
Terminator Wiki
Everquest II Wiki
Astronomy Wiki
German
Spanish
Chinese
Japanese
More...
Wikia is hiring for several open positions


Vote for featured Wikia!

Send this article to a friend
"Toggle spellcheck with function keys"
 
 
Hi!

I thought you'd like this page from Wikia!

http://vim.wikia.com

Come check it out!
Send confirmation