A lot of very good answers here. Here's my take on this:

I went through a similar stage in my life. I was recommended mediation, exercise, etc., and I did religiously follow those. However, I never saw much improvement. Nothing helped and I felt that I was loosing it - until one day when I said enough is enough, I'm going to fix this no matter what.

You see, the problem in recommending meditation is that it doesn't always connect to you the same way it connected to me or someone else. That was my problem. I never had the interest and never even felt that meditation will fix my attention span. The more I tried it, the more frustrated I was. That is when I realized I had to do something different. To be clear, it’s not that meditation won’t work for me. It’s just that I had to practice meditation in a different way.

I figured that meditation is just a way of practicing awareness and mindfulness. To do this, I first listed what makes me distracted most. The first reason I found was that I wasn’t happy in my life. I listed whatever made me unhappy, and tried coming up with reasons to counter them. This is typically one of the main reasons why we loose our focus. Something else is bothering you more than what you want to do. Address that first, and rest of the tasks become easy. Tip #0: Spend some time to introspect. Identify root causes for your discomfort and come up with whatever reason that helps you feel less stressed about those.

Next, I realized that I spent too much time on YouTube, Netflix, Facebook, Instagram, etc. The first thing I did was to cut them off. Just check them less frequently (say, once a week) that you are used to before (say, once every few hours). You’ll have some withdrawal symptoms in the first one week, but it gets way better after that. Trust me, you will do just as good without those - I’m a living example. Tip # 1: Identify what distracts you most and try to cut it down.

Next, I wanted to practice mindfulness. To do this, I picked something I used to enjoy before, but not anymore. Painting, sketching, reading, watching old movies, documentaries, reading history, listening to podcasts, listening to music, etc., were all the things I used to do before, but lost interest just because I was loosing my attention span. So I forced myself to start finishing what I started. Take listening to music, for example. I’d start a song, and skip it within a minute just because I used to get restless. I started forcing myself to listen to the whole song. I started forcing myself to finish the whole article, the entire book, the entire movie. The best part of doing these is that you’ll know exactly when you’re getting derailed. I’d take a break - pause the book, pause the movie - and reflect on why I want to skip, and why I want to re-focus. That helped me tremendously. I’d take a few minutes and get back to my goal. Tip #2: Identify your hobbies and practice mindfulness so that you don’t get stressed more than you currently are.

Then I moved on to my actual work. I’d pick a topic that I “sort of know“ and focus on getting better at it. For me, it was selective topics in coding. Just because I practiced mindfulness with other tasks (i.e., hobbies), I was aware of when I was loosing track of my work. And every time I lost track, I’d pause, take a short break, and force myself to get back at it. Tip #3: Practice mindfulness not just at some tasks, but at every task.

Overall, I’d say pay attention to your actions. You’ll get better at this. Give it time, and be patient - nothing comes easy. I hope this helps!