Skip to content

Instantly share code, notes, and snippets.

@ianloic
Created February 16, 2021 19:49
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ianloic/ddd8105da589d10760638af6f5306ae7 to your computer and use it in GitHub Desktop.
Save ianloic/ddd8105da589d10760638af6f5306ae7 to your computer and use it in GitHub Desktop.
Zsh function to invoke vscode remote
code () {
local script=$(echo ~/.vscode-server/bin/*/bin/code(*ocNY1))
if [[ -z ${script} ]]
then
echo "VSCode remote script not found"
exit 1
fi
local socket=$(echo /run/user/$UID/vscode-ipc-*.sock(=ocNY1))
if [[ -z ${socket} ]]
then
echo "VSCode IPC socket not found"
exit 1
fi
export VSCODE_IPC_HOOK_CLI=${socket}
${script} $*
}
@LazyRen
Copy link

LazyRen commented Jun 10, 2021

Thank you for great post & gist. This will help me a lot.

However, I found that current script results in error like below with zsh version zsh 5.0.5 (x86_64-suse-linux-gnu)

zsh: unknown file attribute: Y

I had to change ocNY1 to oc[1]N.

@badrinath89
Copy link

what is oc[1]N and what is the equivalent of it in bash

@LazyRen
Copy link

LazyRen commented Aug 22, 2022

what is oc[1]N and what is the equivalent of it in bash

oc sorts the item with respect to inode change time. And only get the first item by [1]. N sets the NULL_GLOB option for the current pattern.

So the pattern basically tries to get the most recently created/modified file.

I have no idea about the equivalent of it in bash since I'm no expert on shell scripts. Sorry for that.

@ianloic
Copy link
Author

ianloic commented Aug 22, 2022

Instead of using an echo with a glob you could try using ls and head. Something like:

local script=$(ls -1t ~/.vscode-server/bin/*/bin/code 2> /dev/null | head -1)

I haven't tested it but that should list list all of the matching files in time order with the most recent at the top, suppress any errors (for example if the glob fails) and then take just the first. It won't do the other things that the zsh glob is doing like checking that the file is executable, but that is just a nice sanity check. Bash doesn't seem to do anything clever with filename expansion, sadly: https://www.gnu.org/software/bash/manual/html_node/Filename-Expansion.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment