Practical Vim< /a> vit Practical Vim< /a> Note that the styling for a Visual selection is the... development I thought I knew Vim, but Practical" name="description"/>
Tải bản đầy đủ - 0 (trang)
Tip 121. Fix Spelling Errors from Insert Mode

Tip 121. Fix Spelling Errors from Insert Mode

Tải bản đầy đủ - 0trang

Chapter 20. Find and Fix Typos with Vim’s Spell Checker

• 292

the correction, we could then switch back to Insert mode with the A command,

continuing where we left off.

The Fast Way: Use Spelling Autocompletion

Alternatively, we could fix the error from Insert mode using the s command, which triggers a special form of autocompletion (see :h compl-spelling ).

We could just as well use , which is slightly easier to type. In this

figure, we see screenshots taken before and after triggering the s command. Note that we’re in Insert mode throughout:

The autocomplete word list contains the same suggestions as we saw in Tip

118, on page 287, when we used the z= command.

When we trigger an autocomplete command, Vim usually offers suggestions

on how to complete the word at the current cursor position. But in the case

of s , Vim scans backward from the cursor position, stopping when it

finds a misspelled word. It then builds a word list from suggested corrections

and presents them in an autocomplete pop-up menu. We can choose a result

using any of the techniques described in Tip 112, on page 277.

The s command really comes into its own when a line contains more

than one misspelled word. Using the same example as in the previous figure,

if we were to run :set spelllang=en_us, then the word “moustache” would also be

marked as misspelled. Starting off in Insert mode with our cursor at the end

of the line, we could fix both mistakes just by typing s twice. Try it

yourself. It’s neat!

report erratum • discuss


Now What?

Congratulations—you’ve reached the end of Practical Vim! Now what?

21.1 Keep Practicing!

Keep working at it. With practice, things that once seemed tricky will become

second nature. Make it your goal to operate Vim without having to think

about what you’re doing. When you reach that level, you’ll be able to edit text

at the speed of thought.

Practical Vim doesn’t intend to provide a linear reading experience, so you

won’t have taken everything in on your first pass. Some of the tips are basic,

while others speak to the advanced user. I expect that you’ll be able to pick

up this book again and learn something new.

21.2 Make Vim Your Own

We’ve been working with stock Vim pretty much as it runs out of the box.

The factory settings have served as a useful baseline, giving us a common

feature set to work with, but I don’t mean to suggest that you stick with them.

Vim has some unfortunate default settings. If you want to know why something

behaves a certain way, the answer often seems to be, “Because that’s how vi

did it!”

But you don’t need to put up with those defaults. You can customize Vim to

make it behave the way you want. If you save your preferences in a vimrc file,

you can make it so that Vim is always configured in a way that suits your

workflow. Appendix 1, Customize Vim to Suit Your Preferences, on page 295,

provides a basic primer to get you started.

report erratum • discuss

Chapter 21. Now What?

• 294

21.3 Know the Saw, Then Sharpen It

In Bram Moolenaar’s classic essay “Seven Habits of Effective Text Editing,”

he advises that you invest time sharpening the saw.1 Building your vimrc file

is one way to do that, but it’s vital that you understand Vim’s baseline functionality before you build on top of it. First learn to use the saw. Then

sharpen it.

I’ve seen people customizing Vim in ways that blunt the saw. I’ve even seen

people sharpening the wrong edge! Don’t worry; because you’ve read Practical

Vim, you’re not going to make those same mistakes. You already know Vim’s

core functionality.

Build your vimrc from scratch. As a starting point, you could use the essential.vim

file that’s distributed with this book’s source code. Many Vim users publish

their vimrc files on the Internet, and they can be a great source for inspiration.

Copy the parts that solve your problems, but leave behind what you don’t

need. You should own your vimrc. That means understanding what’s in it.

I’ve resisted the temptation to share the best bits from my vimrc (had I done

so, this book would be double the length), but I’ve dropped hints in the

occasional sidebar. You can find my vimrc file on github, along with many of

my dotfiles.2 I’ve also documented some of my customizations at,

where I also recommend some of my favorite plugins.3

Vim has an unusual license: it’s distributed as charityware (:h license ). That

means that you can use it freely, but you are encouraged to make a donation

to the ICCF Holland foundation,4 which helps needy children in Uganda.

Please show your thanks to Vim’s authors by pledging something toward this

worthy cause.






report erratum • discuss


Customize Vim to Suit Your Preferences

The focus of this book is on mastering the core functionality of Vim, but some

default settings may not be to your taste. Vim is highly configurable; we can

make tweaks to suit our preferences.

A1.1 Change Vim’s Settings on the Fly

Vim has hundreds of options that let us customize its behavior (see

:h option-list

for the full list). We can use the :set command to change them.

Let’s take the ‘ignorecase’ setting as an example (as discussed in Tip 72, on

page 183). This is a boolean option: it can either be on or off. We can enable

it by running this:

:set ignorecase

To turn this feature off, we prefix the name of the setting with the word “no”:

:set noignorecase

If we append a trailing bang symbol after a boolean option, we can toggle the


:set ignorecase!

If we append a trailing question mark, we can find out what the option is

currently set to:

:set ignorecase?


We can also append a trailing & symbol to reset any option to its default value:

:set ignorecase&

:set ignorecase?


report erratum • discuss

Appendix 1. Customize Vim to Suit Your Preferences

• 296

Some of Vim’s settings expect a value either as a string or as a number. For

example, the ‘tabstop’ setting specifies the number of columns that a tab

character should represent (:h 'tabstop' ). We can set the value like this:

:set tabstop=2

We can make multiple assignments with a single set statement:

:set ts=2 sts=2 sw=2 et

The ‘softtabstop’, ‘shiftwidth’, and ‘expandtab’ settings also influence Vim’s treatment

of indentation. To find out more, watch the Vimcasts episode about tabs and


Most of Vim’s options also have a shorthand version. The ‘ignorecase’ setting

can be abbreviated to ic, so we could toggle this feature by running :se ic! or

disable it with :se noic. I tend to use shorthand option names for convenience

when customizing Vim on the fly, but I prefer to use the longhand names in

my vimrc for the sake of readability.

Vim’s settings usually apply globally, but some options are scoped to a window

or buffer. For example, when we run :setlocal tabstop=4, it applies to the active

buffer only. That means we can open several different files and customize the

‘tabstop’ setting for each one individually. If we wanted to apply the same value

to all existing buffers, we could run the following:

:bufdo setlocal tabstop=4

The ‘number’ option can be configured on a per window basis. When we run

:setlocal number, it enables line numbering for the active window. If we wanted

to enable line numbering for every window, we could run this:

:windo setlocal number

The :setlocal command scopes the change to the current window or buffer

(unless the option can only be set globally). If we were to run :set number, it

would enable line numbering for the current window as well as set a new

global default. Existing windows would retain their local settings, but new

windows would adopt the new global setting.

A1.2 Save Your Configuration in a vimrc File

Changing Vim’s settings on the fly is all very well, but if you have customizations that you are particularly fond of, wouldn’t it be handy if they persisted

between editing sessions?


report erratum • discuss

Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tip 121. Fix Spelling Errors from Insert Mode

Tải bản đầy đủ ngay(0 tr)