It's quite simple if you install them into separate directories using the manual installation procedure
Let's assume you wish to install ESP-IDF v4 into ~/espressif/v4
and v5 into ~/espressif/v5
.
First install v4:
$ export IDF_TOOLS_PATH="$HOME/espressif/v4/bin" IDF_PATH="$HOME/espressif/v4/esp-idf"
$ mkdir -p "$HOME/espressif/v4" && cd "$HOME/espressif/v4"
$ git clone -j8 -b v4.3.2 --recursive https://github.com/espressif/esp-idf.git
$ cd esp-idf
$ ./install.sh
$ . export.sh
Then v5:
$ export IDF_TOOLS_PATH="$HOME/espressif/v5/bin" IDF_PATH="$HOME/espressif/v5/esp-idf"
$ mkdir -p "$HOME/espressif/v5" && cd "$HOME/espressif/v5"
$ git clone -j8 -b v5.0.1 --recursive https://github.com/espressif/esp-idf.git
$ cd esp-idf
$ ./install.sh
$ . export.sh
Using the command line you can subsequently activate either one of them running the usual environment setup:
$ export IDF_TOOLS_PATH="$HOME/espressif/v4/bin" IDF_PATH="$HOME/espressif/v4/esp-idf" && . "$IDF_PATH/export.sh"
or
$ export IDF_TOOLS_PATH="$HOME/espressif/v5/bin" IDF_PATH="$HOME/espressif/v5/esp-idf" && . "$IDF_PATH/export.sh"
As for VSCode, you can tell its ESP-IDF plugin which one you want to use. Here are some instructions (sorry, I'm too lazy to adapt them to the specific example above): https://github.com/DaStoned/beegram#optional-visual-studio-code-setup
If you have different VSCode workspaces with different versions of ESP-IDF, make sure you're saving this setting on Workspace level (as opposed to global level).