diff options
author | Andreas Maunz <andreas@maunz.de> | 2012-07-21 10:45:37 +0200 |
---|---|---|
committer | Andreas Maunz <andreas@maunz.de> | 2012-07-21 10:46:32 +0200 |
commit | 81d9c4780b07960613a1dd71c351a4201d875726 (patch) | |
tree | b5a35468ee862f3d2135f7e16bb61cb900032dde /_posts/2012-05-18-using-vim.md | |
parent | f3ac06c59f8cbb153388d1488893f97b7948023e (diff) |
Spell Checking for Vim
Diffstat (limited to '_posts/2012-05-18-using-vim.md')
-rw-r--r-- | _posts/2012-05-18-using-vim.md | 229 |
1 files changed, 138 insertions, 91 deletions
diff --git a/_posts/2012-05-18-using-vim.md b/_posts/2012-05-18-using-vim.md index f4bdfbc..0309a7c 100644 --- a/_posts/2012-05-18-using-vim.md +++ b/_posts/2012-05-18-using-vim.md @@ -30,14 +30,8 @@ and do Now **Vim** will be invoked as the default text editor and you can start it by entering `vim` at the command line. -<p></p> ---- -<p></p> - - - Basic setup -=========== +----------- Put this in your `~/.vimrc`: @@ -102,18 +96,63 @@ Graphical Cheat Sheet Learn **Vim** with the [graphical cheat sheet](http://www.viemu.com/a_vi_vim_graphical_cheat_sheet_tutorial.html). -Using Vim as a pager --------------------- -The standard tool to display (not edit) text files is **Less**. However, you also may want to use **Vim** to display source files. Add - alias vless='vim -u /usr/share/vim/vimcurrent/macros/less.vim' +Spell Checking +-------------- -to your `~/.bash_aliases`, and, after reading in the file, e.g. by a new login, you have the `vless` command. When viewing a file with `vless`, press `v` to switch to the full **Vim**. +[Download](http://ftp.vim.org/vim/runtime/spell/) spell files. Example: for german, download `de.utf-8.spl`, `de.utf-8.sug`. As root, copy the downloaded spell files to `/usr/share/vim/vim7x/spell/`. + + +Put the following in your `~/.vimrc`: + + " Function to toggle spelling for language 'lang' + func! ToggleSpl( lang ) + if &l:spl =~ '\v(^|,)\V'.escape(a:lang,'\').'\v(,|$)' + "Alternatively, since 'spl' may not contain a comma this also + "works: + "if index(split(&l:spl,','),a:lang) != -1 + exec 'setl spl-='.a:lang + else + exec 'setl spl+='.a:lang + endif + :setl spell spl + endfun + +This function toggles spell checking for a given language on/off. For the language files downloaded, you can now map keys to toggle spell checking for each of them. Example for german: + + " Switch on german spelling (in addition to what is active already!) + map <F3> :call ToggleSpl('de')<cr> + +Now, `F3` will toggle spell checking for the german language. Go ahead and create keybindings for any needed language in the same way, using different keys for each language. + +![spell checking](/images/spell.png "spell checking") + +Note: Toggling a specific language on/off will not affect the status of other languages. Arbitrary combinations of languages can be enabled at a time. + +To use spell correction, a few commands are needed: + +- `]s` next misspelled word +- `[s` previous misspelled word +- `z=` view spelling suggestions for a mispelled word +- `zg` add a word to the dictionary +- `zug` undo the addition of a word to the dictionary + +Note: for the `z*` commands, the cursor must be positioned on the corresponding word. + +Text wrapping +------------- +You can do two things: -Toggle auto-indenting for code paste ------------------------------------- +- Let **Vim** format (i.e.change) your text to have shorter lines, by inserting linebreaks. Type `v` to go to visual mode, select text, and press `gq`. +- Leave lines as they are, but display them wrapped. This can be toggled by `:set wrap`, `:set nowrap` + +Both are independent. + + +Auto-Indenting on Paste +----------------------- In a console or terminal version of **Vim**, there is no standard procedure to paste text from another application. Instead, the terminal @@ -145,11 +184,96 @@ To paste from another application: Then the existing indentation of the pasted text will be retained. +Using Vim as a pager +-------------------- + +The standard tool to display (not edit) text files is **Less**. However, you also may want to use **Vim** to display source files. Add + + alias vless='vim -u /usr/share/vim/vimcurrent/macros/less.vim' + +to your `~/.bash_aliases`, and, after reading in the file, e.g. by a new login, you have the `vless` command. When viewing a file with `vless`, press `v` to switch to the full **Vim**. + + +<p></p> +--- +<p></p> + + +Using **Vim** with Git +================== + + +What happens when you type `git diff`? You will receive a simple diff, but there is also a way to do a `vimdiff` with a side-by-side view of a diff in nice colors! See [this tutorial](http://goo.gl/7SH1I) for Vimdiff usage. + +Step 1: add this to your `.gitconfig` + + + [diff] + external = git_diff_wrapper + [pager] + diff = + +Step 2: create a file named `git_diff_wrapper` (and put it somewhere in your `$PATH`): + + + #!/bin/sh + vimdiff "$2" "$5" + + +Step 3: there is still access to the default `git diff` behavior with the `--no-ext-diff` flag. Here’s a function I put in my bash configuration files: + + function git_diff() { + git diff --no-ext-diff -w "$@" | vim -R - + } + +where the argument's meanings are: + +- `--no-ext-diff`: to prevent using vimdiff +- `-w` : to ignore whitespace +- `-R` : to start vim in read-only mode +- `-` : to make vim act as a pager + +It turns out to be very useful to be able to alternate between the two versions of diff. + + +<p></p> +--- +<p></p> + + + +Using **Vim** with Ruby +================== + +**Vim** and ruby are like beer and pizza -- a perfect couple. Following the above will already give you syntax highlighting on ruby files. Here is how you add code completion, i.e. type a command prefix and use the TAB key and be presented with a list of possible completions. + +First install the [SuperTab](http://www.vim.org/scripts/script.php?script_id=1643) module for **Vim**. Then put this in your `.vimrc` + + " ruby autocompletion + autocmd FileType ruby,eruby set omnifunc=rubycomplete#Complete + autocmd FileType ruby,eruby let g:rubycomplete_buffer_loading = 1 + autocmd FileType ruby,eruby let g:rubycomplete_rails = 1 + autocmd FileType ruby,eruby let g:rubycomplete_classes_in_global = 1 + let g:SuperTabDefaultCompletionType = "<C-X><C-O>" + + "improve autocomplete menu color + highlight Pmenu ctermbg=238 gui=bold + +Now, for example, if you want to implement a `collect` call on some container (such as a hash), you can type in **Vim** a prefix of `collect` and press TAB: + + { ... }.c<TAB> + +and **Vim** will present you with a list of applicable commands, from which you select `collect`. + +![picture](/images/vim-cc.png "Vim code completion") + + <p></p> --- <p></p> + Using **Vim** with Latex ==================== @@ -257,83 +381,6 @@ For things to work you must, in order: 3. reverse-search using Ctrl + left-mouse-click. -<p></p> ---- -<p></p> - - - -Using **Vim** with Git -================== - - -What happens when you type `git diff`? You will receive a simple diff, but there is also a way to do a `vimdiff`. -Here is an example: - ![git diff](/images/git-diff.png "git diff") -Vimdiff, which gives you a side-by-side view of a diff with nice colors! -See [this tutorial](http://amjith.blogspot.com/2008/08/quick-and-dirty-vimdiff-tutorial.html) for Vimdiff usage. - -Step 1: add this to your `.gitconfig` - - - [diff] - external = git_diff_wrapper - [pager] - diff = - -Step 2: create a file named `git_diff_wrapper` (and put it somewhere in your `$PATH`): - - - #!/bin/sh - vimdiff "$2" "$5" - - -Step 3: there is still access to the default `git diff` behavior with the `--no-ext-diff` flag. Here’s a function I put in my bash configuration files: - - function git_diff() { - git diff --no-ext-diff -w "$@" | vim -R - - } - -where the argument's meanings are: - -- `--no-ext-diff`: to prevent using vimdiff -- `-w` : to ignore whitespace -- `-R` : to start vim in read-only mode -- `-` : to make vim act as a pager - -It turns out to be very useful to be able to alternate between the two versions of diff. - - -<p></p> ---- -<p></p> - - - -Using **Vim** with Ruby -================== - -**Vim** and ruby are like beer and pizza -- a perfect couple. Following the above will already give you syntax highlighting on ruby files. Here is how you add code completion, i.e. type a command prefix and use the TAB key and be presented with a list of possible completions. - -First install the [SuperTab](http://www.vim.org/scripts/script.php?script_id=1643) module for **Vim**. Then put this in your `.vimrc` - - " ruby autocompletion - autocmd FileType ruby,eruby set omnifunc=rubycomplete#Complete - autocmd FileType ruby,eruby let g:rubycomplete_buffer_loading = 1 - autocmd FileType ruby,eruby let g:rubycomplete_rails = 1 - autocmd FileType ruby,eruby let g:rubycomplete_classes_in_global = 1 - let g:SuperTabDefaultCompletionType = "<C-X><C-O>" - - "improve autocomplete menu color - highlight Pmenu ctermbg=238 gui=bold - -Now, for example, if you want to implement a `collect` call on some container (such as a hash), you can type in **Vim** a prefix of `collect` and press TAB: - - { ... }.c<TAB> - -and **Vim** will present you with a list of applicable commands, from which you select `collect`. - -![picture](/images/vim-cc.png "Vim code completion") |