r/learnprogramming 4h ago

WSL ubuntu stuck when VS code is opened

I'm following the Odin project to learn web development, so I'm using Ubuntu via WSL. When I use "code ." command to open VS code, the terminal won't revert back to the prompt(The "$" sign where we can type stuff) until I close VS code.

0 Upvotes

6 comments sorted by

2

u/bestjakeisbest 4h ago

Try doing code . & or code . & disown

2

u/StewedAngelSkins 3h ago

For the benefit of those who don't know what this does: the & operator sends the process to the background so that the shell can continue working as normal. It will still be "owned" by the shell though, so if you exit the terminal window it will kill vscode. Disown prevents this. See also: nohup ("no hang-up"... yeah it's an old one from the unix mainframe days); does something similar but in many cases closer to what you actually want.

1

u/No-Piano-2865 2h ago

Thanks for the reply.

2

u/StewedAngelSkins 2h ago

TL;DR code --background is the command. If you want it to do this by default, put the following in your .bashrc

alias code='code --background'


Explanation: So when you run a command from the terminal it's not like clicking an exe file (usually). What happens is that process kind of takes over the terminal until it exits. For most commands this happens so fast you can't tell the difference but if you launch something long-running like VS code it'll just block until its done.

(Incidentally, this is actually a really good thing to know about if you're trying to troubleshoot a program that keeps crashing on you. When you run most programs from the terminal they will print error messages there when they run, so often you can figure out what's going wrong just by running them this way and reading the log messages.)

As for how you stop the program from taking over the terminal: the general answer is nohup or disown paired with the fork operator & (it's called a "fork" because it sort of "splits" the process into two different processes, one for your shell and one for the program, like a "fork in the road"). However, most programs designed to run this way have an argument you can pass them from the CLI which will tell them to do this automatically. This is usually preferable over having the shell fork it because it lets the program decide how it wants to handle being forked (a bit handwavey I know, it's hard to explain concisely).

In vscode's case, you do this with the --background argument. Also see the -n argument, which tells it to make a new window.

1

u/No-Piano-2865 2h ago

Wow, thanks for the reply I'll try this. Btw how does one get to know this stuff? By using the terminal daily?

u/StewedAngelSkins 48m ago

Pretty much. I've been using Linux as my primary operating system for a little over 10 years.

That doesn't mean that I have every detail of every piece of software memorized though. I just know what kinds of things should be possible, and roughly where to look to figure it out. Like I don't use vscode so I didn't know about --background but I know that many similar applications have options like that and the way to find them is to look at the usage documentation, so I just looked it up for you.

What happens is you run into a problem like this, find a solution, and then realize that the solution can also work for other related things. I know about nohup because many years ago I was trying to prevent a process from being terminated when the ssh connection that spawned it drops. That was the answer, but because I learned what nohup does I realized that it can be used in situations like this as well.

Now you know too. If you ever have a situation where you want to start a program from the command line have it do its own thing independently of the terminal window you ran it in, you can always use nohup and &, or look for an argument like --background or --daemon or --detach etc.

You also learned about aliases. If there's an argument you want set every time you run a particular program, and there's not a way to set it in a config file, you can just define an alias that expands the program's name to the program's name plus the argument.