使用 vim 缩写时防止尾随空格 [英] preventing trailing whitespace when using vim abbreviations

查看:19
本文介绍了使用 vim 缩写时防止尾随空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 vim 的新用户(windows 中的 gvim),我发现缩写是一个很好的节省时间的方法 - 但是如果我有时可以停止尾随空格,它们会更好.

I am a new user of vim (gvim in windows), and have found abbreviations a nice time saver - however they would be even better if i could stop the trailing whitespace at times.

我有一些经常使用的目录,所以我在 _vimrc 中添加了一些缩写/路径对:

I have some directories that i use a lot, and so i added some abbreviation/path pairs to my _vimrc:

:ab diR1 C:/dirA/dira/dir1/
:ab diR2 C:/dirA/dirb/dir2/ 

等等……

现在当我输入 diR1 我得到 C:/dirA/dira/dir1/[]| 其中空格由 [] 并且光标是 | 字符.我想去掉 [] == 空格.

Now when i type diR1 <space> i get C:/dirA/dira/dir1/[]| where the whitespace is represented by [] and the cursor is the | character. I would like to get rid of the [] == whitespace.

这是一个小问题:但是您似乎可以在 Vim 中自定义其他所有内容,所以我想我会问——在 vim 中使用缩写时是否可以避免尾随空格?

This is a minor complaint: however you seem to be able to customise everthing else in Vim so i figured i'd ask -- is it possible to avoid the trailing whitespace when one uses abbreviations in vim?

在 Vim 中使用的替代工具是一个很好的答案 - 我的目标是保存重新输入常用的目录结构,但为了让光标方便,因为我几乎总是在末尾添加一些内容,例如 myFile.txt.

An alternate tool used within Vim is a good answer - my objective is to save re-typing frequently used directory structures, but to have the cursor handy as i would almost always add something to the end, such as myFile.txt.

在将 myFile.txt 添加到末尾之前,我退格的尾随空格(毫无疑问,因为 space 触发了缩写)比一遍又一遍地输入整个内容,但如果我能避免这样做,那将是理想的......

The trailing white space (doubtless due to the fact that the space triggered the abbreviation) which i backspace over before adding myFile.txt to the end is less annoying than typing the whole thing over and over, but it would be ideal if i could avoid doing so ...

推荐答案

pb2q 答案正是您在当前方案中想要的,但并未完全回答标题中提出的问题.这个确切的问题在 vim 帮助文件中得到解决.参见 :helpgrep Eatchar.它给出的例子是这样的:

pb2q answer is exactly what you want in your current scenario, but does not fully answer the question presented in the title. This exact problem is addressed in the vim help file. See :helpgrep Eatchar. The example it gives is this:

You can even do more complicated things.  For example, to consume the space
typed after an abbreviation: >
   func Eatchar(pat)
      let c = nr2char(getchar(0))
      return (c =~ a:pat) ? '' : c
   endfunc
   iabbr <silent> if if ()<Left><C-R>=Eatchar('\s')<CR>

你可以把 Eatchar 函数放在你的 ~/.vimrc 文件中,然后像这样在你的缩写中使用:

You would put the Eatchar function in your ~/.vimrc file and then use like so in your abbreviations:

iabbr <silent> diR1 C:/dirA/dira/dir1/<c-r>=Eatchar('\m\s\<bar>/')<cr>

这会吃掉"任何尾随的空白字符或斜线.请注意,我使用了 iabbr 而不是 abbr,因为实际上很少需要缩写在命令行模式下扩展.您必须在命令行模式下小心使用缩写,因为它们会在意想不到的地方扩展,例如搜索和 input() 命令.

This would "eat" any trailing white space character or a slash. Note that I used iabbr instead of just abbr, because it is rare to actually want abbreviations to expand in command line mode. You must be careful with abbreviations in command line mode as they will expand in unexpected places such as searches and input() commands.

有关更多信息,请参阅:

For more information see:

:h abbreviations
:helpgrep Eatchar
:h :helpgrep

这篇关于使用 vim 缩写时防止尾随空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆