how do you write a song? or how do you even write anything? I mean in general, not just you personally.

I don't know how to write. I just get a tune in my head and try to match it on the fingerboard. I know the basic scales and modes. But Ive never sat down and said 'okay Mixolydian, what can we make today'. Ive never written anything like that. Sure, I could probably make up something. But like I said I just try to match the music in my head to the notes on the neck. Mostly I just mess around and if it sounds good I call it 'writing'. If it sounds bad or cheesy, I just call it 'oh, I was just ****in' around'.

But how do you write something on the fly. Like if someone said 'play me something'.
I'd have no ****in clue what to do. I guess what Im really asking is how do you improv? and how do you practice and get better at it?
I asked the same question for years, but I've discovered that (and this is just my opinion) writing music isn't something that can really be taught. And I think you've kinda answered your own question when you asked "how do you practice and get better at it?". You do exactly that. You keep messing around until you find something that sounds good. I feel the more you do that, the more you'll find things that sound good.

Though, then again I know very little theory and may be talking more through personal experience and completely out of my arse here...Just keep at it!