The opening and closing quotes (or brackets, braces etc) are highlighted in PyCharm(and other editors). So this means it can identify the pair.
Now, is there a way to delete both the quotes at once (or brackets, braces etc) when either of the opening or closing quotes are deleted, Since it identifies the pair?
For eg. I want this in one keyboard action (by both cases either deleting the opening or closing square bracket):
From this: [[a for a in l1 if a!=0]]
To this: [a for a in l1 if a!=0]
I googled and searched on SO but couldn't find it.
I know of 2 quick ways, but none of them is quick enough.
1: Using the AceJump addon, you just jump to one parens, hit delete, then jump to the other one, and hit delete... Naturally this has the disadvantage that parentheses in a situation like this: ([{(([]))}) would be harder to jump to.
2: There is a command called "Move caret to matching brace". Then, using either AceJump to jump directly to your first brace (or just navigating to it in any way), you activate the function "Move caret to matching brace 2 times". After moving the caret 2 times, you can delete the first matching parens, and then use the action to navigate back ("Back"), and then delete your second brace.
3: Solution 2 does not work for quotes. For them, instead of executing the action "Move to matching brace" you can use the incremental selection, and jump to the most convenient of the 2 quote signs... This however doesn't allow you to navigate back to the previous(or next) quote and delete that one. Therefore for quotes, I have no solution, but this "incremental selection" can work in a few situations (when one of the quotes is at the beginning or the end of a line)
Related
comment-dwim (commend-do-what-I-mean, usually M-;) works well. It:
appends spaces at the end of the line until at least some column is reached,
if the preceding line also had a comment tailing the code, aligns the comment character, and
inserts the comment character for the mode, followed by one space.
In Python one feature is missing to comply with the stringent rules:
at least two spaces must separate code from the trailing comment.
How can I configure comment-dwim to leave at least two spaces in python-mode?
Fiddling with comment-dwim may be holding the wrong end of the stick. For me, M-; automatically inserts two spaces, the '#' character, and a space, following which point is placed. If it's not doing that for you, then you may have reconfigured it somewhere else, possibly accidentally.
I suggest an alternative is to have a 'format-on-save' option enabled, which will do this for you without fussing in your .emacs or with customize.
(use-package python-black :after python
:hook (python-mode . python-black-on-save-mode)
:bind ("C-c p C-f" . 'python-black-buffer))
I use black, but any alternative formatter should do the same. I never worry about how my file is formatted, because black fixes it for me automatically.
From the docs for black:
Black does not format comment contents, but it enforces two spaces
between code and a comment on the same line, and a space before the
comment text begins.
Alright, so in VSCode, when you use an opening bracket, it automatically uses a closing bracket. Instead, I want this to be angle brackets. How would I do this? For example:
Presses: <
Output: <>
Thanks in advance!
Edit: I'm going to clarify this. I'm doing this in a Tkinter text widget. So pressing < will insert a >. Soory for the lack of clarity.
A really simple solution is to bind a function to <KeyRelease>, since that will fire after the default bindings have actually inserted the character into the widget. Or, you could bind to <KeyPress> and manage inserting both the start original character and its closing character.
Let's start by defining a dictionary which defines which characters have matching characters. In this case we'll just use {} and <> for simplicity:
matched_pairs = {"{": "}", "<": ">"}
Next, lets define a function which examines the event to see if it is for a character which in in our dictionary. If we find a matching character we do two things: we insert the matching character, then move the insertion point back one so that the insertion character is between the pairs of characters.
def maybe_insert_matching_pair(event):
matching = matched_pairs.get(event.char, None)
if matching:
event.widget.insert("insert", matching)
event.widget.mark_set("insert", "insert-1c")
Finally, let's bind this function to every keypress, though you could also bind it to only the keys that you know have matching pairs.
the_text_widget.bind('<KeyRelease>', maybe_insert_matching_pair)
I'd like to 'edit' some xml files, which may have similar sections multiple times in one file.
I need add 2 possible missing lines (i call it a pair) inside each section.. i.e. check if the 'pair' exists or not, if it does not then add them.
e.g. below is the possible missing pair lines i'd like to add in.
<arg>--possibleMissedKey</arg>
<arg>possibleMissedValue</arg>
Below file has the pair, so i do not need to add them in
but if any of the section did miss this pair, i'd like to add the pair in to the section.. Also the number of lines in each section is not predictable.
<some-tag-section-not-interesting>
some contents not interesting to me
</some-tag-section-not-interesting>
<some-tag-to-look-for>
<some stuff - a> ..... </some stuff - a>
<arg>--possibleMissedKey</arg>
<arg>possibleMissedValue</arg>
<something-else-not-interesting>blahblah</something-else-not-interesting>
</some-tag-to-look-for>
<some-tag-to-look-for>
<some stuff - b>....</some stuff - b>
<arg>--possibleMissedKey</arg>
<arg>possibleMissedValue</arg>
<something-else-not-interesting>blahblah</something-else-not-interesting>
</some-tag-to-look-for>
so i've consider a few options, but each one i've a question for it:
the first thing came to my mind is 'sed'. I am hoping to replace the ending tag
</some-tag-to-look-for>
with
<arg>--possibleMissedKey</arg>
<arg>possibleMissedValue</arg>
</some-tag-to-look-for>
i.e. essentially add it to the last part of the section.
but i am not aware of whether I can 'pattern match on multiple lines' in sed. and I have not used so called 'hold space'.
My experience with sed has been to check some string in the 'current single line'.
Another option i was hoping to check, is to introduce a inside_a_section_flag, initial value is '0'. start reading the file, the moment I find a staring <some-tag-to-look-for>, I turn that inside_a_section_flag to '1',
and once I reach the 'ending </some-tag-to-look-for>', I do some possible changes, and turn it back to '0'.
so this inside_a_section_flag, if it is 1, means I are inside of the section.... so I need to look for the 'pair',
if I found the pair, I turn the inside_a_section_flag to be 1, meaning I do not need to add the pair and can get out of the current section....
but I am not aware of whether sed can also work with a variable flag, i.e. do conditional replacement/change based on a variable value.
Should this be done by shell at all, instead should this be done by a python script instead?
This might work for you (GNU sed):
sed '/<some-tag-to-look-for>/{:a;n;/<arg>--possibleMissedKey<\/arg>/b;/<\/some-tag-to-look-for>/!{h;ba};x;s/\S.*/<arg>--possibleMissedKey<\/arg>/p;s//<arg>--possibleMissedValue<\/arg>/p;x}' file
Match on a line containing <some-tag-to-look-for>.
Loop through the following lines.
If a line containing <arg>--possibleMissedKey</arg> is encountered, bail out.
Otherwise, if the current line does not match </some-tag-to-look-for>, make a copy and repeat.
When the end tag is found, insert the required two lines using the copied line as a template (so as to retain indentation).
I can't get the auto-indentations to work properly unless I use the automatic closing of braces, et al (which I don't like), and I see no option allowing one to skip over/out.
Eclipse has a configuration option for this, and Visual Studio doesn't auto-close everything by default, but rather formats the code block after manually entering the closing brace (which I rather prefer).
Surely there's something apart from going all the way over to the "End" key?
Edit / update:
As I consider it bad form to leave a question without a marked answer, would someone with more recent experience with PyCharm (I haven't used it in quite some time) weigh in with a recommendation for the best among the below solutions? Perhaps there's a newer configuration option or simple solution not yet listed?
Press Ctrl-Shift-Enter to close the missing braces on the current line (if any), add the missing colon (if missing) and put the caret into the correctly indented position on the next line.
Shift + Enter will jump past completions and drop you onto the next line.
Ctrl + ] will jump to the end of the current element.
This seems to work in most cases to skip past auto-completions. I find it the most versatile of the options.
Ctrl + [ will jump you to the start of whatever code block you are in.
As previously mentioned Ctrl + Shift + Enter will add any extra auto-completions you might need and drop onto a new line.
A college told me a solution:
First you type this:
def test_foo(
PyCharm inserts self) as soon as you type (
Current state: ^ indicates the cursor position:
def test_foo(self^):
Just type ): and hit ENTER, and you are on the next line.
Since I type with ten fingers Ctrl-Shift-Enter is not a solution for me in such common editing operations.
Why do you dislike the auto-closing brackets? They do not disturb anyone, see:
^ specifies the cursor position
method([a, {b: c^}])
behaves as if no brackets were there if you insert new brackets!
insert a '}':
method([a, {b: c}^])
now insert a ']':
method([a, {b: c}]^)
now insert a ')':
method([a, {b: c}])^
et voila! From a users point of view, if you typed blindly, you can not tell from what you see now if the auto-closing brackets were inserted or not.
Or simply: keep the closing brackets on. IntelliJ did a great work on a lot of small things we want to have intuitively.
In Vim, it's a quick 3-character command to change what's inside the current quoted string (e.g., ci"), but is there a simple way to change what type of quotes are currently surrounding the cursor?
Sometimes I need to go from "blah" to """blah""" or "blah" to 'blah' (in Python source code) and I'd ideally like to do it quickly using default key bindings.
Try the surround.vim plugin. I find it an essential addition to any vim installation.
Surround.vim is great, but I don't think it'll handle your triple-quoted needs directly.
The way I've done stuff along these lines (when surround wasn't appropriate) was to use %, make the change, then double-backtick to go back to the starting point. E.g. if the cursor is somewhere in a single-quoted string, do f'%, make the change, then double-backtick and ..