How to create a file in VS Code with Vim extension? - visual-studio-code

Using the VIM extension for VSCode, is there a way to create a file inside the folder you are currently selecting on the explorer tab?
Similar to how it works with NERDTree on NVIM itself where you can press "a" and it lets you write a filename.
Brief example where the cursor would be placed on the "pages" folder.

You can use :e <filename> to create a file and simply use :w to save it.
It creates a file in the directory in which your current file is open. So in the screenshot you provided, if you would be editing _app.tsx, the new file will be created in this directory unless differently stated in your filename.

Related

Spacemacs cannot create a new file when a file with a similar name exists in the same directory

In spacemacs, a user can normally create a file using 'Ctrl-x Ctrl-f', and then entering a filename in the minibuffer.
However, if a file with a similar name exists, then spacemacs does not create a new file. It opens the existing file instead.
One workaround is to jump to a terminal and create the new file, then return to spacemacs and open that file.
But this interferes with the workflow.
This isn't how an editor should work, and I can't see how it isn't a bug.
This behaviour does not exist in normal emacs.
Is there a way to disable this behaviour?
It's not a bug... it's a feature of ivy completion. It happens when the file name is a prefix of an existing name, for example if you try to open a new file foo in a directory containing an existing file foobar. You can choose the current input (foo) instead of the current candidate (foobar) with C-M-j, see: https://elpa.gnu.org/devel/doc/ivy.html#Key-bindings-for-single-selection-action-then-exit-minibuffer

Why does vscode open code.js file instead of current folder?

Subsequent to When typing code in command line to launch vscode how to avoid this wsh error?
I can now launch vscode by typing
code .
But if folder contains code.js file it will open that file instead of current folder.
Why?
I can't see any explanation in doc https://code.visualstudio.com/docs/editor/command-line

Visual studio code, how to copy the file name

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?
The extension Copy file name works like a charm!
CTRL+ALT+F gives you filename and CTRL+ALT+E gives the filename.extension.
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.
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.
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
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
This has been release to VSCode insider today, for you to test/play with it.
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.
Ctrl+Shift+E to open and focus the Explorer pane.
The current file will be selected automatically by default.
F2 to start renaming the file.
This will select the file name without file extension.
(optional) Ctrl+A to include the file extension in the selection.
Ctrl+C to copy.
Escape to abort renaming (or just click anywhere).
no need any extensions, Ctrl+K --> Ctrl+Shift+C, then you will get relative path

Can VSCode always open a default folder (for Ctrl + P searching) even if opening a file in different folder?

Is it possible to have VSCode always have a particular folder ("Directory A") open, so the files inside can be searched using Ctrl + P?
It seems the standard behaviour is that my current "added folder" (i.e. "Directory A") get removed whenever I open a file from a different location ("Directory B").
Closing VSCode and re-opening it always returns me to the last used file (i.e. opening "Directory B" and NOT "Directory A").
How can I force VSCode to always have a certain folder open please?
NB, I've looked into "workspaces" but this doesn't help as whenever opening a file not in the workspace, it seems to close the workspace.
Add Directory A to your workspace using File > Add Folder to Workspace...
Then rather than opening Directory B when you launch VSCode, open the .vscode-workspace file for the workspace that contains both directories.
If there are multiple files in your workspace which match the filename you're searching for using CTRL+P, all the matching files will show up.
The solution, at least on linux, is to create a script with the following content (let's call the script code-standard-path ):
#!/bin/bash
code /path/to/standardDir-or-standardworkspace "$1"
Then from caja right click on a file : open with -> other application.
Then select the command code-standard-path and check Remember this application for "..." files.
Now everytime you double-click on the specific file from whatever location, vscode will open in that predefined directory or workspace.
You can move a bit further and pass the standard path as first argument to the script (e.g. use code "$1" "$2"). So on the open with menu you provide each time the script like this: code-standard-path /path/to/standardDir-or-standardworkspace.
This gives you the ability to open a differrent standard path depending on the file you open (e.g. for .c, .java, .html)
Had the same issue. But there is an easy fix:
On the menu bar go to file File > Preferences > Settings > Window and under Restore Windows select the option preserve.
This will ALWAYS reopen the last session, no matter if you start VS from shell, desktop shortcut or by opening a file.
Open settings File > Preferences > Settings Or by clicking (ctrl + comma)
Type "include" in search bar
Find "Search > Quick Open: Include History"
Uncheck the option

VSCode: Open file in project

I'm using Mac OS X with Visual Studio Code 0.10.10.
How do I use the keyboard to open a file, using its partial name, in the currently open project? I can't find a command in the Command Palette that would enable me to do this. There is a command that does this for Working Files (see screenshot), but not for the currently open project.
> prefix is for running commands in VSCode.
To open a file in the current project press Cmd ⌘ + P and type the full / partial name of the wanted file. VSCode uses fuzzy search, so you can even get away with typos in your input.
There is not a proper command for this but if your file has any file extension you can just open the command palette and replace the > with a .
It will have a section of the open files at the top and "file and symbol results" at the bottom containing every file name containing a . (= all files with a file extension)
If you know the name of the file you can simply remove the > in the command palette and type the name and it will show the file and symbols results again for any file containing what you have typed.
To open known file on a current project if you aware the file name is Ctrl+P Windows.
Open file opening pallet with ctrl+p (on Windows) and cmd+p on Mac OSX.
In that pallet you can either type the file name or a file relative path like shown in the below image
The important point for relative path is that, it should not start with a dot . but should start with a fwd slash /
Absolute path can be used as well.
Ctrl+O opens the command palette right to the file search mode. The command in the shortcuts is "workbench.action.files.openLocalFile".