preventing trailing whitespace when using vim abbreviations

前端 未结 3 1620
情歌与酒
情歌与酒 2021-01-04 07:29

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

3条回答
  •  天涯浪人
    2021-01-04 08:11

    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  if if ()=Eatchar('\s')
    

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

    iabbr  diR1 C:/dirA/dira/dir1/=Eatchar('\m\s\/')
    

    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
    

提交回复
热议问题