VIM如何在缓冲区中高亮显示唯一单词?

4
使用vim进行编程时,文件/缓冲区中的任何独特单词很可能是拼写错误(变量名、方法名或语言结构)。因此,通过突出显示文件中的任何独特单词来捕捉这些拼写错误是一种不错的方式,而无需进行任何复杂的语言分析、解析甚至不需要知道正在使用的编程语言。当然,最好在输入时立即发现拼写错误。我想我不是第一个提出这样一个想法的人,所以也许有人已经设置了这样的环境或者有任何建议?

我觉得这会对眼睛造成很大的压力。你看过 ctags 或者使用 linter 吗?(例如,pflakes 可以为 Python 代码中不存在的变量进行高亮/下划线标记)。 - David Cain
2
也许,你是第一个想到这个主意的人。 :) - kev
我认为这不会是一种负担,因为源代码中的大多数单词通常都会重复多次,至少对于较大的文件来说是这样。 - morphles
2
当然可以,但如果你的目标是发现未定义的单词,我认为你的方法不正确。有很多工具可以突出显示未定义的单词。 - David Cain
1个回答

2

创意点子。

我使用以下 Vimscript 代码片段进行了快速原型设计:

let stat = {}
for ii in range(1, line('$'))
    for word in split(getline(ii), '\(\k\@!.\)\+')
        let stat[word] = get(stat, word, 0) + 1
    endfor
endfor
echo sort(keys(filter(copy(stat), 'v:val == 1')))

运行在 $VIM/vim73/autoload/vimball.vim(一个有 737 行的 23.2 k 文件)上,我得到了以下单关键字出现情况:

12, 1502, 2004, 2009, 2010, 299, 31, 4, 702, Allow, Apr, At, Author,
AutoInstall, Constants, Copyright, Date, DechoTabOn, ENTER, Error,
Functions, GetLatestVimScripts, Input, LICENSE, Listing, Load, Modelines,
No, Normal, Once, Output, Own, Ph, Risk, Statement, Usage, Use, VIM,
Version, Vim, Windoze, Your, about, accomplished, actions, allow, already,
appear, appears, applies, apportion, assume, attempts, automatically, base,
based, bash, both, bypass, c, ch, change, construct, continue, copyright,
cp, cr, create, creates, cygwin, decompress, decompression, defined, did,
dir, distribute, does, doesn, embedded, enc, endfor, even, events,
evidence, except, existing, express, extraction, fmr, fo, force, function,
getpos, give, given, grab, ie, implied, included, index, initialize, input,
inputrestore, inputsave, insure, invoked, its, just, keep, keepcpo, list,
listing, loop, made, messages, missing, mkvimball, named, neither, next,
noacd, nofile, noma, nor, normal, noruler, noshowcmd, ok, older, on,
option, options, over, patch, pick, picked, placed, present, previous,
prologue, prompt, read, readable, redraw, removed, same, see, setpos,
setting, settings, shell, showing, skip, specified, specify, spite, split,
standard, string, strlen, sure, suspect, switch, ta, tab#, take, that,
title, true, un, undefined, under, used, v31, various, warning, warranty,
was, when, where, will, wrote, your, zsh

嗯,对我来说看起来不是很有用(即使排除评论后也没有太大改进),但也许你可以拿这个作为基础并加以改进。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接