Skip to content

Visual Studio Code

Gabor Szarnyas edited this page Oct 5, 2023 · 36 revisions

Open Dialog issues

On Fedora 35 and GNOME 41.4, VS Code's "Open File" Dialog (Ctrl + O) does not get focus, i.e. it falls behind the window (one has to Alt + Tab to get it). Interestingly, it works for the first time but from the second time on, it is out of focus.

The usual tricks (changing from Wayland to Xorg, reinstalling VS Code, settings its configuration to default, rebooting, tinkering window focus/auto-raise settings with the GNOME Tweak Tool and the dconf Editor, etc.) have no effect.

Alternative solutions include removing the VS Code installed from the official Fedora repository (sudo dnf remove code). Then:

  • Installing VS Code from an RPM gives the same result as installing it from the Fedora repository.
  • Installing VS Code from Snap with sudo snap install code --classic solves the problem. However, file type associations have to be adjusted. On Fedora, this means editing ~/.config/mimeapps.list and performing s/code.desktop/code_code.desktop/g (make sure you don't end up with code_code_code.desktop entries!)
  • Installing VS Code from Flatpak solves the problem, however, it uses heavy sandboxing, so getting some extensions such as the LaTeX Workshop is tricky.

LaTeX Workshop

LaTeX Workshop is an extension to handle LaTeX documents.

Installation

See the documentation of LaTeX Workshop.

If you want, you can put a LaTeX docker image on your local machine and configure LaTeX Workshop to use that, eliminating the need of installing LaTeX. Tested on Windows+WSL2 (using remote wsl and remote container plugins), see documentation.

NOTE: If syntax highlighting does not work, change the color scheme.

Spell Check

If working in English use the Code Spell Checker vs code extension. This extension does not support Hungarian, an alternative extension: Spell Right. This uses the dictionaries of your operating system.

Recommended workspace settings for Spell Right

{
    "spellright.language": [
        "en",
        "hu"
    ],
    "spellright.documentTypes": [
        "plaintext",
        "markdown",
        "latex"
    ]
}

Navigation between TeX code and PDF:

Open the internal PDF preview by pressing Ctrl + Alt + V and selecting VSCode tab. Then, you can navigate between the code and the PDF with the following hotkeys:

  • code to PDF preview: Ctrl + Alt + J
  • PDF preview to code: Ctrl + click

Auto-formatting tables

Tables can be formatted using the latexindent Perl scripts. It's recommended to test it in the command line and fix missing dependencies. On Ubuntu 18.04, these were sufficient:

sudo cpan Unicode::GCString 
sudo cpan App::cpanminus
sudo cpan YAML::Tiny
sudo perl -MCPAN -e 'install "File::HomeDir"'
sudo cpan -fi Log::Log4perl #-fi might not be required?
sudo cpan Log::Dispatch
sudo cpan -fi File::HomeDir #-fi might not be required?
sudo cpan Log::Dispatch::File #?

On Fedora 32, this feature worked out-of-the-box with texlive-scheme-full installed.

On macOS:

sudo cpan Unicode::GCString
sudo cpan App::cpanminus
sudo cpan YAML::Tiny
sudo perl -MCPAN -e 'install "File::HomeDir"'

See this issue for installation on other platforms.

Magic comment (e.g. pdflatex) overrides recipe

To use the recipe instead set the following in settings.json:

"latex-workshop.latex.build.forceRecipeUsage": true

The latexmk recipe fails to build the bibliography

  • Problem: When using the latexmk recipe, the build fails with the following message in the log (but there are no explicit errors in the Problems panel):

    bibtex: Not writing to /my/document.blg (openout_any = p).
    I couldn't open file name `/my/document.blg'

    Meanwhile, building the document from the command line with latexmk -pdf documentx.tex works.

  • Solution: Edit the TeX Live configuration file as suggested:

    sudo vim /usr/share/texlive/texmf-dist/web2c/texmf.cnf

    Change

    openout_any = p

    to

    openout_any = a

Use XeLaTeX as the default build engine

To use XeLaTeX as the default build engine, set the following configuration in the settings.json file:

"latex-workshop.latex.recipe.default": "latexmk (xelatex)"

Troubleshooting

Cannot run Python files

Problem:

  • Running the file using the play button stalls
  • Using the "Run python file in terminal" command results in error "The active file is not a Python source file"

Solution: Go to Settings (Ctrl+,), and check whether the terminal is configured correctly. For me, the problem was caused by the "terminal.integrated.inheritEnv": false setting. Interestingly, this setting if offered by the Python extension.

According to Stack Overflow, the terminal.integrated.shell.* properties are also worth checking out.

Set terminal font

  • Problem: Changing tabs does not work with Ctrl + Page Up or Ctrl + Page Down.
  • Solution: The problem was that I have left the Drop Down Terminal open in another window. Seriously.

Settings

Set terminal font

Go to Settings (Ctrl+,), and set the Terminal | Integrated: Font Family key to e.g. Inconsolata-dz for Powerline. Alternatively, add the following entry to the settings.json file:

"terminal.integrated.fontFamily": "Inconsolata-dz for Powerline"

The text moves left and right while typing

When word wrap (default: Alt + Z) is activated and typing, the horizontal scrolling moves a bit left and right. To fix this, the relevant Stack Overflow page has two answers:

  • one: the problem might be caused by GitLens, set the gitlens.currentLine.scrollable setting to false.
  • two: set the editor.scrollBeyondLastColumn value to 0 (the default value is 5).

Smaller plugins

Privacy

Persistent tmux sessions for remote SSH connections

You can configure VS Code such that it (re)attaches to a tmux session when joining a remote server via SSH. This is useful for working on unstable connections.

"terminal.integrated.profiles.linux": {
    "bash": null,
    "tmux": {
        "path": "bash",
        "args": ["-c", "tmux new -ADs ${PWD##*/}"],
        "icon": "terminal-tmux",
    },
},
"terminal.integrated.defaultProfile.linux": "tmux",

Source: https://george.honeywood.org.uk/blog/vs-code-and-tmux/

Do not seed search string from the text around the cursor

Problem: When pressing Cmd + F (Ctrl + F), the text appears in the search box. This is often not desired. For example, when performing a long manual search-and-replace sequence, the user often wants to keep the search string the same during the entire operation.

Solution: In settings.json, add the following configuration option.

"editor.find.seedSearchStringFromSelection": "never",

For the GUI setting, see the comment on the VS Code issue tracker.

Send current line or selection to the terminal running in VS Code

From the command palette (ctrl/cmd + shift + P), select Preferences: Open Keyboard Shortcuts (JSON) and add the following entity to the JSON file:

{
    "key": "cmd+enter",
    "command": "workbench.action.terminal.runSelectedText"
}
Clone this wiki locally