Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I open Vim at a Rust program's `main` function?

Tags:

vim

rust

I'm in the terminal, I cd <projectdirectory>. How can I open Vim positioned at the fn main() of this project.

I don't need a ready solution: ideas will be helpful. Could I potentially make Vim use racer to jump to a symbol automatically on start?

like image 587
dpc.pw Avatar asked Nov 19 '25 02:11

dpc.pw


1 Answers

To open the file main.rs at the first line beginning with fn main, simply run:

$ vim main.rs +/'^fn main'

If you want every file named main.rs to automatically open to the first line beginning with fn main, you can add the following to your .vimrc file:

autocmd BufRead main.rs /^fn main

You can extend this to all files ending in .rs by changing main.rs to *.rs. However, if the file does not contain any matching lines, you will get an error message (Error detected while processing BufRead Auto commands for <filename>). The silent! command suppresses errors:

autocmd BufRead *.rs silent! /^fn main

This will search to fn main if it exists, but fail silently if it does not.

like image 100
trent Avatar answered Nov 20 '25 16:11

trent



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!