sublime - save all open/loaded files that have names? - python

In Sublime Text 2, I want to be able to save all open/loaded files that have names.
I like how Sublime can have files with filenames, and have files that were never saved, and can be closed and it remembers about the untitled files and reloads them without me having to save them.
But when a file has a filename and has some changes in the buffer not yet saved, sublime shows it as such, with the filename and circle, I close sublime, and reopen it, I sublime has remebered it as it was and so the changes are still not saved to the file. That's great.. But.. I'd like a command to save all, but not the untitled ones.
There is a save all option in the menu, but it pops up a dialog box asking regarding saving of untitled files.
What API functions would be involved to write a command that leaves the untitled ones as is, and saves the ones with filenames? (and is there any example code I can run that uses those API functions?)

AFAIK, an opened file is represented by one or more views. So try to get all views and save those with file names. I wrote a simple example. Hope it can help you.
By the way, you can check all API's via the following link.
Sublime Text 2 API Reference
import sublime, sublime_plugin
class SaveAllExistingFilesCommand(sublime_plugin.ApplicationCommand):
def run(self):
for w in sublime.windows():
self._save_files_in_window(w)
def _save_files_in_window(self, w):
for v in w.views():
self._save_exiting_file_in_view(v)
def _save_exiting_file_in_view(self, v):
if v.file_name():
if v.is_dirty():
v.run_command("save")

lhuang's answer is fantastic, and does exactly what I think you want it to do. Make sure you save the plugin as Packages/User/save_all_existing_files.py in order for it to work properly. You can reach the Packages directory via the Preferences -> Browse Packages... menu item. I do have a few additions to make your life a little easier, though - a menu item and a key combination.
You generally shouldn't edit anything in the Packages/Default directory, as all the files can be overridden/expanded upon, but in this case I recommend it for aesthetics sake. Open Packages/Default/Main.sublime-menu and add the following line right after line 128, which should be the save_all menu item:
{ "command": "save_all_existing_files", "caption": "Save All Named Files", "mnemonic": "F" },
This will add a "Save All Named Files" option to the File menu. Next, go to Preferences -> Key Bindings - User and add the following line:
{ "keys": ["ctrl+alt+shift+s"], "command": "save_all_existing_files" },
If this file doesn't have any other contents, surround the above line with square brackets [ ] and remove the trailing comma, otherwise Sublime will complain at you.
Once the keymap file is saved, you can trigger the command by hitting CtrlAltShiftS. Obviously, you can change the keys if you don't like them. If you pick a combination that gives weird behavior, check out the FindKeyConflicts plugin by #skuroda - it's invaluable for troubleshooting your setup, and especially when developing your own packages.
Good luck!

Just add the next line to the Preferences > Key Bindings - User
{ "keys": ["super+shift+s"], "command": "save_all" }
super is a Command key in OS X. Use ctrl on Windows.

Related

Unknown problem in Python. Printing does not work and files are not saved correctly

When I try to write something, such as variables, the code is renamed to the file name on the computer.
For example, if I write:
a = 20
f = 15
print(a+f)
then the code file will automatically be renamed to the first line, i.e. "a = 20"
Then, when I try to run the code, the program outputs nothing but "Python" and some incomprehensible words.
What could it be related to?
enter image description here
enter image description here
I installed the latest version of Visual Stuio Code with Python, they are new, so there should be no problems. But this time it went wrong.
After reinstalling the program, the problem remains.
First of all, if there is no special requirement, please do not use Code Runner to run the script, using the official extension Python is a better choice.
In addition, the dot on your file label means that you have not saved the file, you can add the following setting to enable automatic saving in the settings.
"files.autoSave": "afterDelay",
You may have created the file using the following method. File --> New File... --> Python File. At this time, the file has not been named, also not saved. You can see that there is no such file in the resource manager list at this time.
So the file label shows the first line of codes. This is a feature of vscode, you can refer to this link. And because the file has not been saved, there will be problems executing the script.
You can rename the script file directly (F2), or vscode will remind you to name the file when saving. Another way to create a file is to right click and choose New File..., enter filename and end with .py extension.

Setting a python script default when creating a new file

I would like to set a default custom script whenever creating a new .py file or .ipynb file in Pycharm or VS Code.
I would like the default script to load looking as such:
import time
import datetime
start = time.time()
# Code here.
end = time.time()
print(str(datetime.timedelta(seconds=end - start)))
Is it possible to set default scripts in IDEs? Many Thanks.
It is currently not possible to set a new file template. Maybe the code snippet will help you.
You can easily define your own snippets without any extension. To create or edit your own snippets, select User Snippets under File > Preferences (Code > Preferences on macOS), and then select the language (by language identifier) for which the snippets should appear, or the New Global Snippets file option if they should appear for all languages. VS Code manages the creation and refreshing of the underlying snippets file(s) for you.
Of course, we choose python here
Then change the open file to the following.
{
"time":{
"prefix": "time",
"body": [
"import time",
"import datetime",
"start = time.time()",
"$1",
"end = time.time()",
"print(str(datetime.timedelta(seconds=end - start)))"],
"description": "",
"isFileTemplate": true
}
}
Then enter time in the .py file to get the fragment.
More information about the code snippet can be found here.

How do i remove the unnecessary text in the terminal in vscode?

Hi i'm new into vscode and when i run a program there is some text that i want to get rid of.
i mainly want to remove the first two paragraphs, but also removing the path would be ideal
i tried code runner but thats not the solution i'm looking for
i also tried changing the color to black but i reckon there is a way to remove it
Adding the "-NoLogo" start parameter will remove the paragraph of text; If you open your settings file (Ctrl+Shift+P and then type "Settings" -> User Settings JSON), you can use the following bit of configuration:
// should go in the main JSON object with the other keys
"terminal.integrated.profiles.windows": {
// it might generate some more profiles automatically, but powershell is what matters
"PowerShell": {
"source": "PowerShell",
"icon": "terminal-powershell",
"args": ["-NoLogo"]
}
}
I believe by default those lines will always appear because those are the one that show up when you open cmd on Windows.
Also, by default, the path open to the current project folder, that the reason why you see the path.
You can do a cls to clear the terminal, but the path will remain unless you change the directory.
You can read more about VS Code integrated terminal here and about terminal profiles here
First go to terminal settings and then the add -nologo arg

I'm using same py file in VS Code to learn different examples. Why does it still run the first block of code after I delete and write different code?

I started with this example:
cars = ['audi', 'bmw', 'subaru', 'toyota']
for car in cars:
if car == 'bmw':
print(car.upper())
else:
print(car.title())
I deleted this and moved on to a new example, within same py file:
requested_topping = 'mushrooms'
if requested_topping != 'anchovies':
print("Hold the anchovies!")
After I click run on this second code, VS Code still prints the output for the cars example. What could be the matter?
The white dot indicates the file has not been saved.
You can add this in the settings.json file:
"files.autoSave": "afterDelay",
"files.autoSaveDelay": 1000,
to enable the autosave function in the VSCode.
The issue is unlikely to be due to the code you've written. It is likely related to how you're saving your .py file, and how it is being ran.
Are you running the file using the 'play' button / Ctrl+F5 in VSCode, or are you double clicking on the .py file from File Explorer / Finder?
If it's the latter, then perhaps you're not saving the file in VSCode first, try saving the file with Ctrl+S first.
Screenshots would likely go a long way to helping you answer this question.
Command n (New file..), convert language mode to python from plain text and paste the new block and run ?

Rename a file or directory shortcut in jupyter lab?

I'm using jupyter lab.but everytime when i touch a new file or directory it default names Untitle.I have to click it and select "rename".
Is there a shortcut to rename a file or a directory? i have search this question in google but i didn't find what i want.
Thanks!
You don't need to select the file and rename it.
You can give desired name just after a notebook it created or while it's open.Right Click on the tab where Untitled(in my case it is Untitled1) is written, in top left corner and select Rename Notebook.
It will open a popup text box.
change the name and click on RENAME.
I managed to enable the shortcut by going Settings > Keyboard Shortcuts and add descriptions as below (and hit the save button at the upper right).
"command": "docmanager:rename",
"keys": [
"Ctrl R"
],
"selector": "body"
Image
I am also having the same problem. I found the settings in using my jupyter-themer
from the Menu: Help-> Edit Keyboard Shortcuts -> rename notebook.
see image description

Categories