mass delete words based on spell

4 views
Skip to first unread message

Bee

unread,
Mar 1, 2010, 1:03:26 PM3/1/10
to vim_use
I have a long list of words, on word per line. Many of the words are
not "words", i.e. not in a dictionary.

I would like to turn on vim's spell check and delete only the words
vim knows. Then I will go thru the remaining words and add them, or
not, to my vim word list.

Is that possible?

-Bill

Christian Brabandt

unread,
Mar 1, 2010, 2:30:21 PM3/1/10
to vim_use
Hi Bee!

How about that:
:%s/\w\+/\=printf("%s",
empty(spellbadword(submatch(0))[0])?submatch(0):'')/
(one line)

regards,
Christian

Christian Brabandt

unread,
Mar 1, 2010, 2:35:04 PM3/1/10
to vim_use
Hi

On Mo, 01 M�r 2010, Christian Brabandt wrote:

> :%s/\w\+/\=printf("%s",
> empty(spellbadword(submatch(0))[0])?submatch(0):'')/
> (one line)

this should have been

:%s/\w\+/\=printf("%s", !empty(spellbadword(submatch(0))[0])?submatch(0):'')/

(still one line, though)

regards,
Christian

Bee

unread,
Mar 1, 2010, 3:45:48 PM3/1/10
to vim_use
On Mar 1, 11:35 am, Christian Brabandt <cbli...@256bit.org> wrote:
> this should have been
>
> :%s/\w\+/\=printf("%s", !empty(spellbadword(submatch(0))[0])?submatch(0):'')/
>
> (still one line, though)

That does work, but leaves empty lines, which are easy to remove with:

:sort iu

Is it possible to use spellbadword() in:

:g/{pattern}/d

Tim Chase

unread,
Mar 1, 2010, 3:47:04 PM3/1/10
to vim_use
Christian Brabandt wrote:
> this should have been
>
> :%s/\w\+/\=printf("%s", !empty(spellbadword(submatch(0))[0])?submatch(0):'')/

Lovely solution and introduction to spellbadword() (which I've
not seen/used before). However, I'm curious why you chose to use
printf("%s", ...) instead of just using the contents.

So my reworking of Christian's idea:

:%s/\w\+/\=empty(spellbadword(submatch(0))[0]))?'':submatch(0)/g

The other catch (at least in English) is that words like "can't"
aren't found whole by "\w\+", so you might have to tweak the
regexp or 'iskeyword' to include apostrophes.

-tim

Bee

unread,
Mar 1, 2010, 4:00:39 PM3/1/10
to vim_use

Yes wonderful introduction to spellbadword()

I tried your solution and got errors, counted parens and found one too
many closing, and this works.

:%s/\w\+/\=empty(spellbadword(submatch(0))[0])?'':submatch(0)/g

-Bill

Christian Brabandt

unread,
Mar 1, 2010, 4:10:50 PM3/1/10
to vim_use
Hi Tim!

On Mo, 01 M�r 2010, Tim Chase wrote:

> Christian Brabandt wrote:
>> this should have been
>>
>> :%s/\w\+/\=printf("%s", !empty(spellbadword(submatch(0))[0])?submatch(0):'')/
>
> Lovely solution and introduction to spellbadword() (which I've not
> seen/used before). However, I'm curious why you chose to use
> printf("%s", ...) instead of just using the contents.

old habbit (I often tend use use explicitly printf() even when it not
required)

>
> So my reworking of Christian's idea:
>
> :%s/\w\+/\=empty(spellbadword(submatch(0))[0]))?'':submatch(0)/g
>
> The other catch (at least in English) is that words like "can't" aren't
> found whole by "\w\+", so you might have to tweak the regexp or
> 'iskeyword' to include apostrophes.

true. Didn't think of that since in Germany we wouldn't consider that as
words.

regards,
Christian

Bee

unread,
Mar 1, 2010, 5:07:19 PM3/1/10
to vim_use

Thank you Tim and Christian

Since I had one word per line, having grabbed the words from a book a
friend is writing, sorted to keep only unique, this works great:

:%s/\w\+\n/\=empty(spellbadword(submatch(0))[0])?submatch(0):''/g

-Bill

corykendall

unread,
Mar 1, 2010, 10:59:32 PM3/1/10
to vim_use
I'm new here, but I tend to use macro based solutions. Is there a
problem with that?

For this I would use:
qa]sddkq1000@a

Tim Chase

unread,
Mar 2, 2010, 9:49:23 AM3/2/10
to vim...@googlegroups.com
corykendall wrote:
> I'm new here, but I tend to use macro based solutions. Is there a
> problem with that?
>
> For this I would use:
> qa]sddkq1000@a

[please don't top-post]

The problems I'd have with doing that are mostly "I have to think
about things" issues:

- do I have more than 1000 items and may need to re-execute the
macro? (having 'ruler' showing the number of lines in the file
might help)

- does "]s" break the repeated macro execution if there isn't a
bad-spell match, or does it continue to delete the remainder of
the 1000 things after the last bad-spelling is found?

- do I have something valuable in register "a" that I don't want
to tromp with my macro; or the flip side of "what register do I
have that's available"?

- if there's a bad-spell word as the first line, does issuing "k"
("go up from line #1", possibly an error-ish condition) after
deleting it trigger the macro-recording to stop?

- can I issue this from anywhere in the file or do I have to do
it from the top (and does my 'wrapscan' setting change its behavior)?


That's a lot more thinking than I like to do ;-)

The :g or :s versions can be used in a mapping and trusted to do
what they should without any of the above issues (except perhaps
tromping the search register).

-tim

Christian Brabandt

unread,
Mar 2, 2010, 1:49:10 PM3/2/10
to vim_use
Hi corykendall!

On Mo, 01 M�r 2010, corykendall wrote:

> I'm new here, but I tend to use macro based solutions. Is there a
> problem with that?

Certainly not. Use whatever works best for you. I personally dislike
macros, because usually I have problems decrypting them. Therefore I
prefer ex commands and functions which I find more readable.

regards,
Christian

corykendall

unread,
Mar 2, 2010, 4:40:32 PM3/2/10
to vim_use
On Mar 2, 1:49 pm, Christian Brabandt <cbli...@256bit.org> wrote:
> Hi corykendall!

>
>
> Certainly not. Use whatever works best for you. I personally dislike
> macros, because usually I have problems decrypting them. Therefore I
> prefer ex commands and functions which I find more readable.
>
> regards,
> Christian

On Mar 2, 9:49 am, Tim Chase <v...@tim.thechases.com> wrote:
>
> The problems I'd have with doing that are mostly "I have to think
> about things" issues:
>
> - do I have more than 1000 items and may need to re-execute the
> macro? (having 'ruler' showing the number of lines in the file
> might help)
>
> - does "]s" break the repeated macro execution if there isn't a
> bad-spell match, or does it continue to delete the remainder of
> the 1000 things after the last bad-spelling is found?
>
> - do I have something valuable in register "a" that I don't want
> to tromp with my macro; or the flip side of "what register do I
> have that's available"?
>
> - if there's a bad-spell word as the first line, does issuing "k"
> ("go up from line #1", possibly an error-ish condition) after
> deleting it trigger the macro-recording to stop?
>
> - can I issue this from anywhere in the file or do I have to do
> it from the top (and does my 'wrapscan' setting change its behavior)?
>
> That's a lot more thinking than I like to do ;-)
>
> The :g or :s versions can be used in a mapping and trusted to do
> what they should without any of the above issues (except perhaps
> tromping the search register).
>
> -tim

Good responses both, thanks guys.

I think I agree that if you want as repeatable solution, an ex
expression is better because it's more readable, and rememberable.

But if I need a one-off solution, I find it easier to record a
macro... Fix the first occurance, navigate to the next occurance in a
repeatable way, and then execute as many times as necessary. I also
like that I can run a macro once... twice... three times... each time
making sure it worked correctly, and then launch it on the whole file.

Perhaps once I get more comfortable with regular expressions I'll
change my tune :)

corykendall

unread,
Mar 2, 2010, 4:40:38 PM3/2/10
to vim_use
On Mar 2, 1:49 pm, Christian Brabandt <cbli...@256bit.org> wrote:
> Hi corykendall!
>
>
> Certainly not. Use whatever works best for you. I personally dislike
> macros, because usually I have problems decrypting them. Therefore I
> prefer ex commands and functions which I find more readable.
>
> regards,
> Christian

On Mar 2, 9:49 am, Tim Chase <v...@tim.thechases.com> wrote:

Reply all
Reply to author
Forward
0 new messages