whenever I press the letter/button 'U' during coding in vs code, it automatically creates a new file.
the button 'u' is working as a shortcut key to create a new file in vs code.
lastly, if I copy/paste the letter 'u' in my code then it works fine.
I think some of your keybinds are messed up, You could try to check here, see if this works.
https://code.visualstudio.com/docs/getstarted/keybindings#:~:text=All%20keyboard%20shortcuts%20in%20VS,This%20will%20open%20your%20keybindings.
This should be due to some extensions you installed, or some misoperation that led to the modification of shortcut keys.
Find the keyboard shortcut and search for "creat: new file"
Related
This is my first post on StackOverflow. I'm also a beginner in Python. So I was just tinkering with the open() function, I was making a simple program to replace text in other .txt files. Here is my code:
f = open("file.txt", "r+")
f.truncate(0)
f.write("This text has been replaced.")
f.close()
print("Text replaced")
So, after running this program, the text in "file.txt" is getting changed. However, when I do ctrl + z, it's showing Undo Reload from Disk?, and when you click OK, the text gets back to normal.
How to prevent this? I am using Python 3.9, Pycharm code editor.
Thank you
I think your question might be more about the PyCharm UI editor.
If I understand correctly:
you have "file.txt" open in the PyCharm editor
you run the code above
At this point, the filesystem has the "file.txt" with the updated contents. The PyCharm editor (buffer) still has the old contents.
When you ctrl+z, PyCharm notices the filesystem has been updated and prompts to see how you want to proceed. When you click "OK", PyCharm writes its buffer to the file, which is the original contents.
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 ?
How can I move a line (or set of selected lines) down or up in jupyter notebook. Are there any shortcuts?
After moving line 3 one line up:
For example ALT+UP or ALT+DOWN is for up/down displacement that is used in Eclipse IDE.
This question discusses cell displacement.
TLDR:
By following this tutorial, I was able to enable all the sublime-text shortcuts (which includes the moving lines with Ctrl+Up/Ctrl/Down).
I then was able to furthermore, customize the shortcuts by editing the sublime.js
Complete answer:
Find your jupyter path with jupyter --config-dir
Add the following code to <JUPYTER-PATH>/custom/custom.js:
require(["codemirror/keymap/sublime", "notebook/js/cell", "base/js/namespace"],
function(sublime_keymap, cell, IPython) {
// setTimeout(function(){ // uncomment line to fake race-condition
cell.Cell.options_default.cm_config.keyMap = 'sublime';
var cells = IPython.notebook.get_cells();
for(var cl=0; cl< cells.length ; cl++){
cells[cl].code_mirror.setOption('keyMap', 'sublime');
}
// }, 1000)// uncomment line to fake race condition
}
);
You need to find this codemirror/keymap/sublime.js (that is required in the code from point 2), for me, it was in <MY-PYTHON-ENVIRONMENT>/lib/python3.8/site-packages/notebook/static/components/codemirror/keymap/sublime.js
At the end of this file, you can edit the shortcuts, in the keyMap.pcSublime section of this file (if you have a PC).
For OP, the lines to edit would be: swapLineUp and swapLineDown
I did this with Ubuntu & sublime installed.
You can select the line with mouse in jupyter notebook. Drag it
up or down before the line where you want to move it. Press >
button on keyboard while your text is still selected.
This will bring the cursor to end of selected line,
but before the text of line being replaced. Press Enter.
Since jupyter works with code blocks you can use that shortcut between code blocks as
Ctrl + Shift + -
But this will only works between code cells not in the same code block. I don't think there is a built-in shortcut for that. For more shortcuts you can look here: https://www.dataquest.io/blog/jupyter-notebook-tips-tricks-shortcuts/
There's no such a shortcut in Jupyter, however, you can manage your own shorcuts Go to Help > Edit keyboard Shortcuts and start setting your commands.
There's some good info on this other post which will lead you to manage your profile's notebook.json file and so on.
Hope this was useful ;)
Edit: typo
You can create your custom shortcuts in Jupyter notebook
Jupyter Shortcuts
Is there an easy way of removing blank lines from IPython notebook?
I have picked up a habit of blank lines from web development and my fingers tend to hit enter automatically. This makes IPython notebooks less(by taking too much of my 14" screen), not more readable in most cases.
I was wondering if there is a way of automatically remove blank lines from the notebooks.
In notebooks, I think preferred way of splitting the code is by placing each separate method to a different cell.
Is commonly accepted style guide for notebooks or does PEP 8 apply as it is?
EDIT: I think question 2, Is answered by IPython docs. https://github.com/ipython/ipython/wiki/Dev:-Coding-style
If you are talking about deleting the empty line from jupyter notebook which opens in web browser then press Esc and D(keyboard key) 2 times.
You can also see all the keyboard shortcuts in the jupyter notebook file
by clicking on the help and then from dropdown on keyboard shortcuts.
Pressing Esc and D (2 times) will delete only blank lines starting from the beginning. It won't delete any lines which have some text or code.
Pressing Esc and D (2 times) deletes the complete cell.
I guess the Question is about deleting the blank lines, unless the author interpretation of blank lines are blank cells.
If you came here looking for "How to delete a single line (whether code line or blank line) in a jupyter notebook cell"
Ans: ctrl-d (in Edit mode)
Using a Mac:
to forward delete a line from the curser use Fn + command + delete
to backward delete a line from the curser use command + delete
to delete a whole cell use Esc + (d twice)
These, and other shortcuts, can be found under the Help tab.
Go to the required line and press: ESC + D (together twice)
Use Ctrl+Backspace or Ctrl+Delete both will delete blank lines.
Ctrl+Backspace can be used to delete whole text of line before cursor position.
Similarly Ctrl+Delete to delete text after cursor.
If you came here looking for "How to delete a single line (whether code line or blank line) in a jupyter notebook cell"
ctrl-d (in Edit mode)
I think the question is about deleting an empty new line of a cell.My solution is to write something in the line you want to delete, then go to the end of the line above it and press delete till the junk written in the line to be deleted is cleared.This deletes the empty new line as well.
Select the row you want to delete and click the scissors icon highlighted in the below screenshot -
I'm trying to add a context menu option for New -> Python Script on Windows 7. However, everything I've tried has failed.
The way that I thought it should work is below:
Add the following registry key:
[HKEY_CLASSES_ROOT\.py\ShellNew]
"FileName"="Template.py"
Null File version:
[HKEY_CLASSES_ROOT\.py\ShellNew]
"NullFile"=""
Optional registry key
[HKEY_CLASSES_ROOT\.py]
"PerceivedType"="text/plain"
#="Python Script"
Add the file to the Windows, ShellNew folder...
This does nothing, although I've done this before, and it worked with other file types. I'm unable to find anything for this anywhere, because they do everything I try for other file types.
What am I doing wrong?
EDIT: Python 2.7.8 or later has this option added during setup/installation.
Reference: MSDN Extending Shortcut Menus
I have found that the easiest way to do this is with the following method, which I have tested on my Windows 10 PC.
Open Regedit
Navigate to Computer\HKEY_CLASSES_ROOT.py
Right click on the .py key > New > Key
Name the new key "ShellNew"
Inside the ShellNew key, add a new string value
Name the string value "NullFile"
Change the NullFile's value to 1
That's it!
(note: this should work with all plain-text file formats)
HKEY_CLASSES_ROOT\.py\PerceivedType="text"
together with
HKEY_CLASSES_ROOT\.py\ShellNew\NullFile=""
works for me on Windows 7.
I have also set HKEY_CLASSES_ROOT\Python default value to "Python Script"
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\.py]
#="Python"
"Content Type"="text/x-python"
"Python"="Python"
"PerceivedType"="text"
[HKEY_CLASSES_ROOT\.py\ShellNew]
"NullFile"=""
[HKEY_CLASSES_ROOT\Python]
#="Python Script"
Use open++. It's easy to configure, and maybe it can do what you want.