How to move cursor automatically from code editor to terminal in VS Code - python-3.x

I am using Microsoft VS Code version 1.34.0 (64 bit version) on Windows 10 64 bit.
I have "Python" and "Code Runner" extensions installed and using it for Python 3.7.3
I click on triangle in top right corner to run code or do Ctrl + Alt + N.
When I run code that needs user input, I can see the message(if provided any in code) and cursor in Terminal but the cursor is inactive (empty rectangle).
The active cursor (blinking) is still in code editor pane.
Is there any way to automatically move the active cursor from code editor to Terminal for user input?
I am trying to avoid one extra mouse click or (Ctrl + `)

Install Code Runner from vscode extensions
Then Add the following line in settings.json :
"code-runner.preserveFocus": false

Related

Python file won't run in vs code using play button

i wrote a basic python program and tried running it using the play button but nothing happens,
i look through the interpreters and the one for python isnt detected
can someone guide me
tried looking online for answers but most are confusing since i can't seem to find some of the settings they are recommending i use
Hey, my suggestion would be :
First check the installation of python on your machine, and if it
doesn't help then,
Open keyboard shortcuts in VS Code 'CTRL + K and CTRL + S' or by
clicking settings button in bottom-left corner.
Search "Run Python File in Terminal".
You will get first option with the same title.
Double click the Key Binding area in front of title.
And set a keyboard shortcut for running Python {eg: 'ALT + Q' (My shortcut)}. This would be much
convenient.

Android Studio 'Comment with line Comment' shortcut not working

Whenever I press Ctrl + / to comment out a line in Android Studio in a C++ file, it just moves the caret down 1 line without making a comment. Creating a different keybinding for this shortcut has the same effect.
I've used this feature fine before, it only started happening when I opened up android studio today. I've tried disabling all plugins and restarting Android Studio to no avail.
If the keyboard shortcut doesn't work. Then try
Alt + Shift + Insert
What It will do is activate column selection mode, making it easy to just click and drag to select multiple lines of code that you can type on. It's easier than trying to make an entirely new keyboard shortcut.
The other way to do Column Selection Mode is to just right click anywhere on the code and it will be in menu popup, click to activate/de-activate.

How to run current line in Spyder 3.5( ctrl +f10 not working)

I am very new to Python and I am used to R studio so I choose Spyder. On the Spyder layout I saw a button 'run current line (ctrl +f10)'. But it doesn't work by pressing the button or c+10. Am I missing something? I can only select the script and 'ctrl+enter ' to run current line which is not convenient at all. I am using ubuntu with Anaconda distribution.
The key to run the current line by itself is F9. The shortcut ctrl+F10 is used if you are in debugging mode.
You can see a list of shortcuts by selecting Preferences in the Tool menu, and then clicking on Keyboard shortcuts.
Coming from R studio I imagine you were hoping to have a command that runs the next command, rather than just that one row (which can break a command into several parts and cause errors).
The exact equivalent doesn't exist yet but if you get accustomed to adding #%% before and after chunks ("cells") you want to run together then you can use the following commands to run the whole chunk.
Run cell: Ctrl + Return
Run cell and advance : Shift+Return
F9 is the key that does the job for you.
To replicate the RStudio style, go to Preferences in Tools menu and go to Keyboard Shortcuts.
Since Ctrl + Enter is assigned to another function, change that first.
Then assign the F9 key value to Ctrl + Enter. Now Spyder is the same as RStudio. Atleast in a way.
Some keyboards have a different layout than others in terms of what the keys are supposed to do. For me running happens if done via Fn + F9.
Control Enter is a quick way of executing a line or block of code in both R Studio & Python.
In Spyder, make sure the line or block is highlighted before you hit 'ctrl-enter'

Clear PyCharm Run Window

Is there a way to clear the "Run" console in PyCharm?
I want a code that delete/hide all the print() made previously.
Like the "clear_all" button, but without having to press it manually.
I have read that there is a way to do it in a terminal with os.system("cls"), but in PyCharm, it only adds a small square without clearing anything.
Also, I don't want to use print("\n" *100) since I don't want to be able to scroll back and see the previous prints.
In Pycharm:
CMD + , (or Pycharm preferences);
Search: "clear all";
Double click -> Add keyboard shortcut (set it to CTRL + L or anything)
Enjoy this new hot key in your Pycharm console!
Pycharm Community Edition 2020.1.3
You can right click anywhere above the current line on the console, and choose the "Clear All" option. It'll clear the console
How to
Download this package https://github.com/asweigart/pyautogui. It allows python to send key strokes.
You may have to install some other packages first
If you are installing PyAutoGUI from PyPI using pip:
Windows has no dependencies. The Win32 extensions do not need to be
installed.
OS X needs the pyobjc-core and pyobjc module installed (in that
order).
Linux needs the python3-xlib (or python-xlib for Python 2) module
installed.
Pillow needs to be installed, and on Linux you may need to install additional libraries to make sure Pillow's PNG/JPEG works correctly. See:
Set a keyboard shortcut for clearing the run window in pycharm as explained by Taylan Aydinli
CMD + , (or Pycharm preferences);
Search: "clear all"; Double click ->
Add keyboard shortcut (set it to CTRL + L or anything)
Enjoy this new hot key in your Pycharm console!
Then if you set the keyboard shortcut for 'clear all' to Command + L use this in your python script
import pyautogui
pyautogui.hotkey('command', 'l')
Example program
This will clear the screen after the user types an input.
If you aren't focused on the tool window then your clear hot-key won't work, you can see this for yourself if you try pressing your hot-key while focused on, say, the editor, you won't clear the embedded terminals contents.
PyAutoGUI has no way of focusing on windows directly, to solve this you can try to find the coordinate where the run terminal is located and then send a left click to focus, if you don't already know the coordinates where you can click your mouse you can find it out with the following code:
import pyautogui
from time import sleep
sleep(2)
print(pyautogui.position())
An example of output:
(2799, 575)
and now the actual code:
import pyautogui
while True:
input_1 = input("?")
print(input_1)
pyautogui.click(x=2799, y=575)
pyautogui.hotkey('command', 'l')
Easy Method:
Shortcut: Control K,
Right click on terminal and clear Buffer
There's also another way of doing it using the system class from os. All you need to do is have this code:
from os import system, name
# define our clear function
def clear():
# for windows the name is 'nt'
if name == 'nt':
_ = system('cls')
# and for mac and linux, the os.name is 'posix'
else:
_ = system('clear')
# Then, whenever you want to clear the screen, just use this clear function as:
clear()
However, in order for this functionality to work in pycharm, you need to enable "Emulate terminal in output console". You can find this under edit configuration of the file where you want to use the clear function, then it's under Execution option. Here's a screenshot: pycharm screensho
You could just do a ("\n" * 100000000), so it'll be impossible to scroll back.
In PyCharm terminal you can type 'cls' just like in linux terminal.
For Python Console (where you see the output) assign a shortkey for "clear all" in File -> Settings -> Keymap -> Other -> "Clear all"
You can also click somewhere on the PythonConsole -> Right button -> clear.
Hope it helps
I just relised that instead of going to the trouble of setting up a shortcut, you could just set up a command using PyAutoGUI to click on the trash bin on the side of the window e.g
note, to install pyautogui click on the end of the import pyautogui line, then press alt+enter and click install pyautogui.
import pyautogui
# to find the coordinates of the bin...
from time import sleep
sleep(2) # hover your mouse over bin in this time
mousepos = pyautogui.position() gets current pos of mouse
x,y = mousepos # storing mouse position
print(mousepos) # prints current pos of mouse
# then to clear it;
pyautogui.click(x, y) # and just put this line of code wherever you want to clear it
(this isn't perfect thanks to the time it takes to run the code and using the mouse, but it is reasonable solution depending on what you are using it for.)
I hope this answer is helpful even though this is an old question.
Just click the trash can icon to the left of the command window and it clears the command history!
In PyCharm 2019.3.3 you can right click and select "Clear All" button.This is deleting all written data inside of the console and unfortunately this is manual.
Sorry to say this, here the main question is how to do it programmatically means while my code is running I want my code to clear previous data and at some stage and then continue running the code. It should work like reset button.
After spending some time on research I solved my problem using Mahak Khurmi's solution https://stackoverflow.com/a/67543234/16878188.
If you edit the run configuration you can enable "emulate terminal in output console" and you can use the os.system("cls") line and it will work normally.
Iconman had the easiest answer.
But simply printing "\n" * 20 (or whatever your terminal height is) will clear the screen, and the only difference is that the cursor is at the bottom.
I came here because I wanted to visually see how long each step of a complex process was taking (I'm implementing a progress bar), and the terminal is already full of scrolling logging information.
I ended up printing ("A" * 40) * 20, and then "B" and "C" etc., and then filming it. Reviewing the video made it easy to see how many seconds each step took. Yes I know I could use time-stamps, but this was fun!

Releasing the find and replace pane in visual studio 2012

In Visual Studio 2012 the find and replace window has Docking alignment to the top right side, how to release it?
The "quick find" window isn't docked in the usual sense. Press Esc to dismiss it and also make the yellow-orange search result highlighting go away.
Alternatively, click the X in the top right of the mini window.
Edit.Find = docked Find window
Edit.Replace = docked Replace window
Edit.FindinFiles = undocked Find window
Edit.ReplaceinFiles = undocked Replace window
so for undocked windows you can press Ctrl + Shift + F or Ctrl + Shift + H
or alternatively change shortcuts in Options > Environment > Keyboard to reach desired windows easier.
This is not possible unfortunately, which really drives me nuts when trying to find something in a minified file (single line) since the find window commonly blocks the found result.
Last post I could find about it:
https://visualstudio.uservoice.com/forums/121579-visual-studio-ide/suggestions/3636907-let-me-undock-the-find-and-replace
The best solution I can find is copy and past file into notepad and find there (for my needs).
In Visual Studio 2012 all docked windows can be released as follows;
Docked Window's Header right click menu (or click Window Position Icon what located the far right of the header)> Float menu
If the usual things, as already suggested, won't work, here are two other ideas I have found to work with oddities in Visual Studio.
Disable all plugins and try again (had a weird case of VS crashing on Ctrl+S and it was a faulty plugin)
Try a repair on the install, could have some faulty files.

Resources