how to avoid creating a new line when hitting return in auto complete mode - emacs

auto-complete in my emacs works great. But there is one annoyance. If I have finished a word correctly like "abc", but in the mean time there is another matching word called "abcd". The menu showed two options with the first one "abc" selected as default. At this time, if I hit RET, it will select "abc" as well as creating a new line. This is annoying. I want it to be just complete the selection when I hit RET. And create a new line only if I hit RET again. Is there a way to set up auto-complete to behave like that

Related

Finish code folding in one line (and keep folding sign visible all the time)

Code folding in VScode doesn't seem as good as IntelliJ's.
eg. code block
def g(a):
print(a)
print(a)
print(a)
print(a)
print(a)
Or, for (...),{...},[...] (indentation isn't actually the real concern.)
eg code sample from dart(flutter):
OutlinedButton(
onPressed: () {
Navigator.pushNamed(
context,
SecondPage.id,
);
},
child: Text("Page 2"),
)
So, now if I fold this code and try to duplicate the folded line, it doesn't copy the entire fold as it does in IntelliJ. Is there an extension or settings, with which I can achieve the same as IntelliJ? I've found a comment which says I need to copy till the beginning of the next line which is not what I want. https://github.com/microsoft/vscode/issues/51232#issuecomment-395190470
If I press Enter after folding, it creates a new line in the folding section, I actually want it to create the line after the folding section.
VScode only shows the folding sign when I hover the mouse over it, I want it visible all the time.
I'm assuming that the code language is Python.
So, now if I fold this code and try to duplicate the folded line, it doesn't copy the entire fold as it does in IntelliJ
You need to select the whole region. To do that, simply do a Shift+Down, then you can copy/paste the region.
If I press Enter after folding, it creates a new line in the folding section, I actually want it to create the line after the folding section.
Python's folding regions are based on the indentation. So when pressing Enter after a folding, the new line is tabbed so it is inside the region. You need to delete the tab so the line isn't in the region.
Finish code folding in one line
To fold on a single line, you need to use the extension Explicit Folding and the following configuration:
"[python]": {
"explicitFolding.rules": [
{
"beginRegex": "\"\"\"",
"endRegex": "\"\"\"",
},
{
"indentation": true,
"offSide": true
}
]
}
Three points. Two addressed by others already but I'll include them here for completeness, with the third being my own contribution.
Fundamentally, Code's philosophy seems to be that the end of the line containing all the folded code seems to be considered the end of the first line of the folded code, not the end of all the folded code. The folded code exists between the start of the first line of the code in question, and the start of the next visible line (the first line after the folded section). Therefore:
As per daiyam's answer, press Shift+Down at the start of the folded code to select everything between it and the next visible line. This then includes all the folded code, which you can then copy/paste.
My own contribution here: Similarly, to add a new line after the folded code, either press Down from the start of the folded code line, or press Right from the end of that line, to get to the start of the next line. Then press Enter to add a new line there. Then Up to get to the start of the new line.^^
As per mark's comment set the Editor: Show Folding Controls to always.
^^ Some potentially overkill analysis 😉 ...
Yes, that seems like a lot of steps for something you're used to doing in one step in IntelliJ, except it's only two extra (arrow) key presses. If you're switching away from IntelliJ, to Code, then your muscle memory will likely adapt pretty quick. That might only be a problem if you're switching back and forth between them frequently.
Also, whatever keys or mouse click you've done to get to the start or end of that first folding line in the first place could be replaced by a different click or key presses to get you to the start of that next line instead. So the only extra really is the Up key after the Enter, plus the (presumably pretty tiny) learning curve to remember to go to that next line start instead of previous line end.
I suppose if that's a big deal, you could use any of the Macro applications to combine those extra arrow keystrokes and the Enter to one keystroke (say Shift+Enter or something). I find myself doing that sort of thing a lot to bring consistency to my use of different IDE's etc.
Just a couple of ideas in case they help.

How to enable Arrow-Up (and Arrow-Down) key to show previous (or next) entered line in Emacs?

How to enable Arrow-Up (and Arrow-Down) key to show previous (or next) entered line in Emacs?
In Emacs, particular in Scheme mode, I want to use the ArrowUp and ArrowDown keys to navigate in the history or entered commmands, like I use to do in Bash.
However, pressing these keys in the Scheme interpreter (running inside Emacs) doesn't do anything except to go up or down one line in the buffer.
I guess the way to enable this history navigation applies to all modes?
I want to go the expression above i.e. '(1 2 3).
How do I enable it?
You need to get into the command buffer to get history of commands. Press M-x and then try Up /Down. When you are in a regular buffer, these are indeed navigation keys.
Also. have a look at Helm.
Edit (to sync the answer with the updated question, pulling the updated answer from the comments)
If the intention is to use history in the Lisp interpreter buffer, then C-up and C-down are the usual key bindings for that.

Change default EMACS mouse highlight behaviour

In EMACS the default seems to be to 'copy' any text highlighted with the mouse. I'm attempting to replicate the functionality of modern text editors, where I can highlight a section of text and press 'paste' to replace it. I have so far added
(delete-selection-mode 1)
to my init.el
The problem is, if I copy something, then highlight to paste in its place, I end up pasting what I had just highlighted, changing nothing.
What do I need to change to fix that behaviour?
The most powerful element of emacs is its introspection features, lets have a look at how we can use them to try and solve this problem. We must use the power of the source.
One of the most important tools for introspection in emacs is the describe-key command which is bound to C-h k. It brings up the documentation of whatever keystroke is called after it. So in our case if we press C-h k and then click and drag we will see the documentation for <down-mouse-1> and more importantly for <drag-mouse-1>. The documentation states that "<drag-mouse-1> at that spot runs the command mouse-set-region". Below it then gives some documentation for this command. It says
Set the region to the text dragged over, and copy to kill ring.
This should be bound to a mouse drag event.
See the ‘mouse-drag-copy-region’ variable to control whether this
command alters the kill ring or not.
Now we know that somehow mouse-drag-copy-region controls whether or not the highlighted text is copied.
If we follow the link of that variable it tells us the default value and some documentation:
If non-nil, copy to kill-ring upon mouse adjustments of the region.
Now all we have to do is set the variable to be nil to get the effect that you want. Place the following code at the end of your init file and you should be all set
(setq mouse-drag-copy-region nil)
I hope that this helps you with this problem and that more importantly it helps you with further issues.
By default, selecting a region with the mouse does not copy the text to the kill ring. If your Emacs does this, you probably have set the variable mouse-drag-copy-region.
In a fresh Emacs (24.5 started using -Q), you can do the following:
Start delete-selection-mode.
Mark a region using the mouse. Copy it using M-w.
Mark a second region. Replace it with the first using C-y.
I see two alternatives, neither of which does exactly what you request. (For both, yes, turn on delete-selection-mode.)
Use the secondary selection for the text to copy, and use the primary selection (the region) for the text to be replaced.
You copy text into the secondary selection using the Meta key plus the mouse - for example, press and hold Meta (the Alt key, usually) while dragging or double-clicking mouse-1.
You paste the secondary selection using Meta plus mouse-2.
Select text with the mouse, then copy it to the kill-ring using M-w. Then select the text to replace with the mouse and use C-y to paste the copied text to replace it.

How to force emacs's pasting to behave like in other apps

I am new emacs user and one of the things that irritates me is that when I want to replace current selected text with the one from clipboard I need to delete it first. Every other application that I know replaces pasted text with the current selection by default.
Here's a little bit more detailed description:
Select some block of text
Paste text from clipboard
Emacs just pastes text where the cursor was and previously selected text it is still there. I want that selected text was deleted first.
As artscan wrote in a comment, you can get this functionaly for the normal yank (paste) operations by adding:
(delete-selection-mode 1)
to your configuration.
If you want yank by mouse to also delete the current selection, you can add:
(put 'mouse-yank-primary 'delete-selection 'yank)
in your configuration as well.

Tab in Emacs-helm (anything) does not autocomplete with current best match

While trying to autocomplete a file (e.g. to open a file with C-x C-f) Emacs-helm shows a list of possible candidates.
If I then press Tab (which I would hope it would help me choose the first/closest match), I get the following in the minibuffer:
It looks like the minibuffer gets confused with escape characters, and it does not choose the file that I actually want to open (the top choice).
Helm requires this conceptual jump from the default Emacs completion, which is not so obvious:
You don't need to press a key to complete. The completion buffer refreshes
with new results after every input.
This is called "incremental" completion.
The normal tab functionality is not needed in "incremental"
completion. So tab was rebound to helm-select-action, which allows you to
choose an alternative action with the selection. But there is only one action
in read-file-name.
The error you're seeing could be clearer though, I've filed this issue on github.
Additionally, note Helm treats each space separated term as a filtering
regular expression. So, pressing space foo will filter
the current list down to those that contain foo in the name.