Alternative way of creating new file in VS Code? - visual-studio-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

Related

VS Code change file extension at file save as dialog

On some files I need to change standard save as file dialog.
I want to use custom command that executed 'workbench.action.files.saveLocalFile' command with some params that can change availible file extensions.
Where I can find the params if they exist?
Another way:
Custom command with showSaveDialog (my own vscode.SaveDialogOptions)
Close unsaved editor
Open saved file in editor
At step 2 I use 'workbench.action.closeActiveEditor' command, but I can see confirm dialog (Do you want to save the changes...)
How to remove this confirm?

urxvt: Open graphical file explorer in the current folder

While the command line is very useful for most of the tasks, I sometimes resort to a graphical file explorer (like Nautilus, Thunar, PCmanFM). I would like a keyboard shortcut or a context-menu entry to open such a graphical file explorer _in the current directory. I know the converse is possible with nautilus-open-terminal. Is there currently a way to do that?
It may require writing a perl-extension, but I do not know enough of perl and urxvt to find how to capture the current PWD.
Assuming you're using Bash as your shell, here's an alias you could add to your .bashrc:
alias nh="nautilus file://$(pwd)"
I called it nh for "Nautilus here". So from the comman-line you would simply type:
nh
for a nautilus window in the shell's current directory.
I have no idea what's involved in adding something to the urxvt context menu - sorry.
Well, I quickly hacked a small extension to do this: https://github.com/raphaelfournier/urxvt-perl.
The opening of the file browser can be triggered with the right-click menu or a keyboard shortcut. The selection can be a directory, but also a filename, which will then be opened by the application associated with it in the file browser.

How to get VSCode to "open as new file" from Go To File dialog

I have recently switched to VSCode, and am loving it, except for one specific thing that drives me nuts.
My "goto" command is {Command+P}, the easy search-and-open-file bar. If I type the name of a file into this bar and it does not exist, I want to be able to hit ENTER and have it open a tab editing that file as a new file. This is the behavior I would get in old-school Windows Notepad, or in mvim :e <filename>, but I can't figure out how to do it in VSCode.
Is there a toggle or a plugin I can use to get this behavior straight out of the Go To File dialog?
Answering my own question:
No, there's no way to do this using {Command+P}. This is strictly a file finder and I've yet to see any plugin that changes the behavior.
If you're using the VsCodeVim plugin, an almost-as-good approach is just :e <file> - immediately open a new buffer editing the given file. There's no tab autocomplete this way, but you just have to live with that.

Removing a file or folder using VS 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.

Change VSCode Default Language for new files?

Is there a way in Visual Studio Code to change the default language that is used for new files?
By default if you open a new file, it's set for "Plain Text", I want this to be "HTML" instead.
I am often copy pasting HTML into VSC, editing a bit, then copying it back to a CMS I am using (the CMS editor is horrible). I don't want to save the code on my computer at all, just edit it a bit with HTML syntax highlighting, but I want that to be the default.
You can now set the default language at either the user or workspace settings level using files.defaultLanguage:
"files.defaultLanguage": "html"
This can be done as a one off by changing the language mode:
F1 to launch command palette
Type lang, enter
Type html, enter
Now you can do this change. Check out
"files.defaultLanguage" in settings.
With v1.42 you can either set
"files.defaultLanguage": "${activeEditorLanguage}"
and when you open a new untitled file with an html file as the last active file, the new file will automatically be assigned a languageMode of HTML.
Alternatively, pasting any html code into a new untitled file from a vscode editor will automatically be detected and the languageMode set to HTML. Unless you have specifically set
"files.defaultLanguage": "plaintext"
then the languageMode will not be automatically detected as of v1.43.
Also see https://stackoverflow.com/a/68596936/836330 for a preview language detection feature in vscode 1.59.
A slightly quicker way to accomplish what Daniel had answered:
Press Ctrl+K, then M
Type html, then press Enter
I had a similar issue with VS Code. I wanted to default to Python when opening new files.
Solution: Click on File > Preferences > Settings. In the search area, Type: "files.defaultLanguage". Then fill in the language of your choice.
Perhaps you could create a scratch file with the .html extension? Open that when you need to do some copy/paste editing.