Removing a file or folder using VS Code - visual-studio-code

When I bring up VS Code I see options for adding a file or folder but I do not see options to delete a file or folder or rename such. Any hints?

Deleting file is strait forward. To delete a folder created using MS Code explorer,
press Ctrl + Shift + P then it will show a command prompt like
section at top.
Search 'remove' in that. A command will be shown -"Workspaces: Remove folder from workspace...". Click on that.
Intended folder name will be shown for removing.
After clicking the same, it will be removed from explorer, without affecting actual
files on the operating file system.

Thank you I figured it out. It doesn't seem immediately obvious to me but on a MAC using VSCode selecting a file and entering the return key lets you rename a file, selecting a file and then using command-del deletes a file.

The Rename and Delete actions are there, but you have to select the file in the bottom part of Explorer, under the project directory. When you select it under Open Editors or right click on the tab bar, there is no Delete and that is confusing. In this case you can choose Reveal in Side bar in the tab context menu to get to that entry. But that won't help (will select the tab under Open Editors) if the file is out of the project tree, in this case the most convenient action would probably be to Open Containing Folder and delete it manually in the file manager.

Using extension:
Install File Utils extension
https://marketplace.visualstudio.com/items?itemName=sleistner.vscode-fileutils
Press Ctr + Shift + P
Type File Utils > Delete

In VSCode, make sure you are in Explorer. Also make sure you are right-clicking the file shown in the directory/folder. Here either hitting Del for windows and command+del for Mac will delete the file.
A common issue is trying to delete a file when in source control. You must be in explorer.

VSCode 1.50 (Sept 2020) still has the feature for file deletion, and improves it with microsoft/vscode issue 71315
##Should maintain row focus after deleting a file
Use case:
Use the keyboard to focus a file in the explorer and delete it
Now the whole tree has focus
Press the down arrow key
The first item is selected
I want to get back to where I was looking before.
The search tree focuses the next item in the list when one is removed.
I don't know whether the explorer did this previously with the old tree. But it would be nice for the explorer to do the same thing.

Just right click on the file or folder you will get the options to delete the file as well as of renaming the file.

For directory delete type in VScode terminal:
rm -r directoryName
For file delete type in VScode terminal:
rm fileName.tx

No way to delete a folder by a right-click. However, you may remove it from the IDE: File > Close Folder.

Related

Remove subfolder from Visual Studio Code Workspace

My current workspace shows this directory. I would like to remove it so it doesn't appear in my workspace.
According to the documentation that I've read the way to do this is to go to File->Preferenceces -> Workspace Tab and the Add a pattern in the excludes.
However, no pattern that I've tried has worked.
I've tried
**/IVAPI/BKP_OLD_CODE/*
**/BKP_OLD_CODE/*
**/*BKP_OLD_CODE*
**/BKP_OLD_CODE
Then I click the refresh button on the worksapce explorer but the directory stills shows up. How can I add a patter so that BKP_OLD_CODE does not show up anymore?
You were very close, the fact is that, since its a folder, it does not have an extension or prefix! So just use the name of the folder (BKP_OLD_CODE in this case) and you are all done!

VSCode: Can I open individual files instead of workspaces?

Opening whole directories (each one requiring a new window) doesn’t play well with editing lots of little scripts/config files scattered in different folders.
Yes, you can! Here are three ways:
Either by running code {yourfilename here} in a terminal.
Dragging the file from a file explorer onto the open VSCode window.
Use the menu item "File/Open file... (Ctrl + O)", see image.
You don't have to open a folder in VSCode!
In VSCode create a New Window (instance) and open all the files you're interested in from anywhere on your disk.
If you want to keep this selection for later, you can Save Workspace As .. for later usage.

Alternative way of creating new file in VS Code?

When creating new file in VS Code, it just opens a blank empty file right away.
Is there a way to create a specific type of new file, like in Notepad++ you can choose the language for the file. In VS Code you get to choose the file extension only when saving the file for the first time.
It'd be a bit faster to create a new, for example, JavaScript file instead of creating empty file and saving it as .js file and then writing to it.
So far I've created files with basic Notepad (with the "show file extensions" option enabled from File Explorer) and when naming the new file, writing the extension at the end instead of .txt
You could just open the integrated terminal CTRL+`` and touch your file.
The keyboard shortcuts mentioned by #CountingStuff in this comment worked.
Here are the step-by-step instructions:
Create a new file:
Ctrl+N
Change the syntax language:
Ctrl+K M
In the drop down that appears, auto-complete or manually choose a language option.
The vscode documentation for this can be found here:
https://code.visualstudio.com/docs/languages/overview#_language-id
I've remapped my keyboard shortcuts so that CTRL+N triggers the "File > New File" menu item. Now, when I press CTRL+N it adds a new entry in the file explorer and I can add a new file from there. I can also utilise VSCode's file explorer creation mechanism to create files in subfolders by pre-fixing the file with the directory structure, so typing src\index.js will create a new file named index.js in the src directory.
Reference: https://www.youtube.com/watch?v=2QBYlfSQA6s

Can't edit files in Vscode

I've just started using Vscode and have a created a WebAPI project. On trying to open any of the default code files (Startup.cs and Program.cs) whatever I type starts to appear in the blue bar as shown below instead of where I have placed the cursor. I have no idea what's going on or what that blue bar is.
I am able to add my own code files and edit these as I would expect.
I'm running Vscode 1.29.1 on Win 10 on a Dell Latitude 12 7275. The vscode extension
Enabled extensions :
That's caused by Vim extension. Uninstall or disable it, otherwise use insert key on your keyboard to put it in insert mode.
I'l leave this here for future readers
Basically, if I split files in tabs in VS Code, I could work with the files on the left hand side but could "read-only" all the files on the right hand side.
When I went to the extensions tabs I noticed that whatever I typed was being typed in the field in the extensions search bar. After deleting that, and clicking back on the "Files" tab, it went back to normal and can basically edit files on the both sides of my split screen.
If cannot type
Check where whatever you type is going (e.g. Extensions > Search bar)
Delete wherever it's being typed, and click back on the "Files tab"
Check if it works
I had to change the parent folders permission...
Try changing the entire folder to:
Read & Write: Allows a user to open the item and change it.
https://support.apple.com/guide/mac-help/change-permissions-for-files-folders-or-disks-mchlp1203/mac

VSCode :: How can I duplicate directories and files from within the file explorer?

Ideally, I would right-click on the folder/directory or file I want to duplicate, choose "Duplicate" and, hopefully, would be asked in the same step to give the new duplicated file or directory a new name.
Looking at the current options, I couldn't find a way to do this on latest VSCode 1.19.1 (macOS):
Am I missing something? Is there other way to do this?
The more I use VSCode, the more I get used to it and, lately, I found myself barely needing to use the terminal provided in the Panel, or switching to a terminal app, for simple stuff like this.
Marketplace extension File Utils:
To duplicate a file within a folder.
On the explorer pane:
right click on the file,
press copy
now right click on the folder, and
press paste
the file will be duplicated into the same folder with "...-1" or " copy" appended to the file name
The "Duplicate action" extension will allow you to right click and choose duplicate as you described.
A somewhat old issue now but as I try to avoid installing too many extensions this may be helpful to others wanting to avoid the same. Also, as there's a strong chance this is a context in which you're using a mouse/trackpad (as opposed to keyboard), the normally fantastic 'do everything' quick menu thing (or whatever Shift + CMD + P is called) is probably not a great solution...
Hold down Alt. (Note: You can actually do this during step 2 if preferred).
Drag the file onto the folder it is already in (during drag the cursor will have a green plus icon).
Release the file where you want it to be (doesn't actually have to be the same folder).
The file will be duplicated with the filename {original-filename} copy.{original-extension}.
Click on the file again.
Hit enter/return to edit the filename.
All the above sounds much more onerous than it really is. I find it quicker than right-clicking and momentarily finding/navigating to a menu option, especially if that menu option is not a native one.
How about the good old CTRL+C and CTRL+V?
I just tried. If you click on a file then press CTRL+C, and then CTRL+V then you get a duplicate of the file named [original name] copy.[ext]
It's a mystery, though, why the duplicate file option is still not on the right-click menu after so many years.
A little warning: doing this will remove the content of the clipboard. So if you are in the middle of a copy-paste cycle, then make sure to save the clipboard content somewhere. Yes, it's inconvenient, but until MS decides to put in this basic action in the context menu (right-click) we need to deal with it.
Atleast in Linux, holding down Ctrl and dragging the file into the required directory should do the trick. No Ctrl+C Ctrl+V required.
CTRL+C
CTRL+V
F2
type new name
ENTER
opens in editor
Similar to above but with an extra couple of things
Another solution, since VS Code still does not allow this in 2022:
Install the multi-command package.
Then, in your keybindings.json add the following:
{
"key": "cmd+d",
"command": "extension.multiCommand.execute",
"args": {
"sequence": [
"filesExplorer.copy",
"filesExplorer.paste"
]
},
"when": "explorerViewletVisible && filesExplorerFocus && !explorerResourceIsRoot && !inputFocus"
}
Afterwards, you can press CMD + D to duplicate a file or folder.
You may want to add "renameFile" into the sequence to directly rename the duplicate. Of course, this works just as well with ctrl instead of cmd.