TLDR; To be confident, be humble, help others, appreciate failure, and know you'll never be an expert.

Long form:

The one (or last?) time I felt this way was when I had just stepped down as CTO of a well funded company I co-founded.

I can say today that the reasons for stepping down had little to do with my competence, though I didn't feel that way at the time. In truth, it was 2007, and my co-founder and I realized that we were in for a very long stretch of no income, and a short runway, and that the best thing to do was to essentially go on standby.

Still, my confidence took a hit.

I took about a month off, which didn't really help change that feeling. I enjoyed the time off, and it was good for me in general, but it didn't change how I felt about my abilities.

Eventually I decided to do whatever I could to reduce any doubts I had about my competence. I started teaching people how to write code, which meant I really had to make sure that I knew what I was talking about. Teaching helped fill in a lot of gaps I didn't know I had, but also helped me see that I knew what I was doing, at least enough to teach others.

I didn't stop at teaching how to write code though. I started consulting, and managed to find gigs that let me help engineering teams communicate better, and taught those same teams how to have more robust practices around code changes, releases, failures, debugging, code reviews, etc. All the other non-coding stuff every coder needs to be good at.

It took years, but eventually it got really difficult to feel like I didn't know what I was doing. It was clear I knew enough to be effective.

I learned another pretty important thing during that time:

I'm not an expert. No one is. The people who say they are don't yet realize what they don't know.

Admitting I'm not an expert does a few things: it allows me to fail and learn, and to be grateful for what I gain when I do. I don't pretend like I'm a beginner that can't function without stackoverflow. I just leave room for the possibility that there are often better ways to do something. I've learned that shutting the doors to those possibilities is a sure fire way of feel incompetent.

Being humble and ok failing makes all the difference. If someone doesn't like my approach, that's fine. Rather than defend my approach at all costs, I explain the goal, define constraints, and reduce any friction towards hearing how to improve the idea. If after talking it out my idea was still the right way to go I make sure to express gratitude for the feedback.

While trying to "get back", a friend of mine who recently lost his fight with cancer, who's leadership abilities and incredible software development skills were something I still hold up as my "end goal", often said: "It's always good to be humble, and almost always better to be humbled."

I try to optimize what I do around that idea. Having a baseline of "there's always more to learn" just makes things better and easier.

It also helps me get stuff done. It helps me have good relationships with people I work with. If someone doubts a decision I've made, my default mode is to assume they're correct. They often are correct, and I grow. Sometimes they're wrong, and it gives me an opportunity to be humble and help them grow with me. Either way, no energy is spent defending an idea because my ego says I need to. Energy is only ever spent on improving my skills and the skills of teams I lead or work with.