开发者

Vim: moving the cursor to a string in a source file without error messages if it doesn't exist

When I use vi to open a file *.c, I would like the cursor to move to the string "main" automatically. If there is no "main", I want the cursor to go to "void" without an error prompt.

In my .vimrc I have set

:autocmd BufRead *.c 1;/main 
开发者_运维问答

but this cannot implement all my requirements. Specifically, if there exists no "main" in some opened C source file, vi prompts "Error, cannot find main ...." which is the behaviour I want to remove.

I have also tried adding <silent> or :silent to that autocmd line, but it doesn't do what I want. Can anyone help me? Thanks.


Just use :silent!; it runs a given command blocking not only the normal messages but also the errors.

I would recommend to use the BufReadPost event instead of BufRead to run your command after the buffer is loaded, and change the search pattern to look for main as a separate word:

:autocmd BufReadPost *.c :silent! 1;/\<main\>


Try /main\|^, but if cursor in file not on first line - it's not that you want.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜