37

In the visual studio code, it has commands to Copy Path and Copy Relative Path (Ctrl+Shift+p->File: Copy Path of Active File). Is there a way I could copy the active file name instead of the whole path?

If there is no way copy the active file name. Is there a way I could write a script or something which creating a new command based on existing Copy Path and showing the new command in the Command Palette?

r0n9
  • 2,505
  • 1
  • 29
  • 43

10 Answers10

29

The extension Copy file name works like a charm! CTRL+ALT+F gives you filename and CTRL+ALT+E gives the filename.extension.

Melebius
  • 6,183
  • 4
  • 39
  • 52
Wael Chorfan
  • 686
  • 7
  • 9
  • This gives an error when I tried to copy the file name of a .wav file, (cannot read properties of undefined (reading 'document')) – Zac Wrangler Jul 15 '22 at 22:30
  • Use command + shift + X to access the extensions pane (or click on it from the vertical bar on the leftmost part of the screen). – stevec Feb 28 '23 at 11:09
  • On macOS, I think it's [command + option + f](https://stackoverflow.com/a/73093267/5783745), which unfortunately overrides the shortcut for find within selection (see [here](https://stackoverflow.com/a/73093267/5783745)). – stevec Mar 07 '23 at 10:20
8

open your file on vscode> select file as active file> Ctrl+Shift+P > Alt+Shift+C now you can use Ctrl+V to paste this path any where you want.

AEM
  • 1,354
  • 8
  • 20
  • 30
3
  • step1. first install "copy file name" extension from extensions(ctrl+shift+X) in your vscode.
  • step2. then use ctrl+alt+e for copying the full file_name with file_extension(like .cpp or .py or .java etc..).
    or ctrl+alt+f for for copying the file_name without file_extension.
Ajay jangid
  • 711
  • 9
  • 9
2

It is been a while but I found if you want to write a script using the currently opened file, create a new task in task.json and calling some scripts in the task might be a good option.

Basically, ${file}, ${fileBasename} and ${fileDirname} are the variables that can be used in your script file.

Useful reference: https://code.visualstudio.com/docs/editor/variables-reference

r0n9
  • 2,505
  • 1
  • 29
  • 43
2

As an alternative, if you see the active file in the "Explorer" view, you can type F2 once you select it.

With VSCode 1.71 (Aug. 2022), you can type F2 and:

  • 1st press => prefix
  • 2nd press => all
  • 3rd press => suffix
  • 4th press => cycles back around to prefix

https://user-images.githubusercontent.com/4544166/184671196-01adb871-a666-4065-9ad4-57b752f1fecc.gif

This has been release to VSCode insider today, for you to test/play with it.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
1

Without using any extensions, here's a combination of key shortcuts you can use to copy the path of the current file. It's a bit cumbersome but with some practice can be faster than doing the same using the mouse.

  1. Ctrl+Shift+E to open and focus the Explorer pane.
    The current file will be selected automatically by default.
  2. F2 to start renaming the file.
    This will select the file name without file extension.
  3. (optional) Ctrl+A to include the file extension in the selection.
  4. Ctrl+C to copy.
  5. Escape to abort renaming (or just click anywhere).
kapex
  • 28,903
  • 6
  • 107
  • 121
0

If you arrived here wondering how to copy the full file name path, it's:

  1. Press command + k then,
  2. Press command + option + c

That will copy the file path to the clipboard.

Another way to copy the full (or relative) path is by right clicking on the relevant file tab (top of screen):

enter image description here

stevec
  • 41,291
  • 27
  • 223
  • 311
0

The extension File Utils also provides that capability along with others (duplicate, move, rename, delete for files and folders) through the context menu in the Explorer pane or a file tab, but not the keyboard shortcut.

GeorgesZ
  • 161
  • 1
  • 3
0

Ctrl+K, then Ctrl+Alt+C, since at least version 1.78.2

enter image description here

lineage
  • 792
  • 1
  • 8
  • 20
-3

no need any extensions, Ctrl+K --> Ctrl+Shift+C, then you will get relative path

  • This opens me a terminal – Pithikos Jan 13 '23 at 08:56
  • my env is windows, pressing Ctrl+Shift+C directly will open a terminal. you should press Ctrl+K firstly , then press Ctrl+Shift+C, then could get a full path, you could paste it anywhere. as for relative path, I configure it as shortcut Shift + Alt + C, actually you could configure/check shortcuts from setting > Keyboard Shortcuts, and grep "path", to see all related shortcut. – Fugui.li Feb 02 '23 at 09:59
  • Question was for the file name, not the path nor the relative path – Andrew Apr 19 '23 at 21:06