Vim检查第二行是否是'To:'并且是空的 [英] Vim check if second line is 'To:' and empty

查看:163
本文介绍了Vim检查第二行是否是'To:'并且是空的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一些东西放入启动时运行的vimrc文件中。我需要检查打开的文件的第二行:

 收件人:

这就是空格,基本上就是这些单词,然后什么都没有。如果它发现一个空 To:它会在末尾第2行将我带到该行。如果到:后面有文字,然后它将我带到第9行。



我使用Vim编辑Mutt中的电子邮件,并且试图让它决定我是否会' m回复电子邮件(电子邮件地址已存在)或创建新电子邮件(我需要输入地址)并带我到相应的行。

不是很确定从哪里开始,我不知道我是否可以在vimrc启动时运行函数,以及如何告诉它去到X行的末尾。我知道我可以去看看:

  2gg 

但行结束和正则表达式对我来说很难。

解决方案

()


 函数MuttEnter()
let l2 = getline(2)
if如果len(l2)== 3
2
else其他
9
endif
startinsert![l2 [:3]是#'To:'

endif
endfunction
augroup vimrcMutt
autocmd!
autocmd VimEnter *调用MuttEnter()
augroup END

。注意:更好地检测你是否正在编辑一个mutt消息(目前假设你是在第二行有 To:(带空格) )。



顺便说一下,你在这里完全不需要正则表达式,Vim不如perl优化它们。


I'm looking for something to put into the vimrc file that runs on startup. I need to check the 2nd line of the file that it opens for:

To:

That's to and a space, basically those word's and then nothing after it. If it finds an "empty" To: it takes me to that line at the end, line 2. If To: has text after it then it takes me to line 9.

I use Vim for editing emails in Mutt, and I'm trying to get it to decide if I'm replying to an email (the email address is already there) or creating a new email (I need to enter an address) and take me to the appropriate line.

Not quite sure where to even start with this, I don't know if I can run function's on startup in the vimrc and how to tell it to go to the end of X line. I know I can go to a line with:

2gg

but the end of the line and the regex is the hard part for me.

解决方案

Try

function MuttEnter()
    let l2=getline(2)
    if l2[:3] is# 'To: '
        if len(l2)==3
            2
        else
            9
        endif
        startinsert!
    endif
endfunction
augroup vimrcMutt
    autocmd!
    autocmd VimEnter * call MuttEnter()
augroup END

. Note: it makes sense to have better detection of whether you are editing a mutt message (currently it assumes you do if you have To: (with a space) on the second line).

By the way, you need exactly no regular expressions here, Vim is not as good at optimizing them as perl.

这篇关于Vim检查第二行是否是'To:'并且是空的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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