Jumping to a specific line of code
One feature of code editors that I ignored for a long long time is the ability to jump to a specific line number in a current file or to open a file on a specific line of code.
The need to go to a particular line of code is quite common, for example to investigate a printed stack trace in a terminal. Let’s consider for a moment that we have a simple Python stack trace available and want to investigate the executed code:
Traceback (most recent call last):
File "/home/pstribny/playground/linenumbers/linenumbers.py", line 5, in <module>
raise_exception()
File "/home/pstribny/playground/linenumbers/linenumbers.py", line 2, in raise_exception
raise ValueError()
ValueErrorYou can see that we get information about both file locations and line numbers.
In Visual Studio Code, we can open files with fuzzy search using CTRL + P. It is possible to also add a line number to the search by separating the file name and the line number by a colon. The final syntax would be “file:linenumber”, e.g. “file.py:2” to go to the second line of file.py.
In case the file in question is open already, we can specify just a line number to go to. To do that, just leave out the file part and use a colon with a number like :54. There is also a direct shortcut CTRL + G. VSCode even supports going to a specific character on the line by appending another colon with a number, if you need that.
While going to a specific line is useful on its own, Visual Studio Code makes it even simpler to jump to file locations from the integrated terminal. When getting terminal output with a stack trace, it is possible to just CTRL + click on the printed locations (see the underline):
This is not true only for various stack traces (check if your language is supported), but for file locations in general. That means we can print file locations in a console ourselves and get the ability to jump to code files that we want!
Let’s say we print something like this to the console:
print("/home/pstribny/playground/linenumbers/linenumbers.py:8:3")
VSCode will recognize the syntax and allows us to jump to linenumbers.py, on line 8, and character number 3.
I am not the same person I was 10 years ago, now I often jump to specific lines of code. And you?
Petr



