I’ve been unable to use the <a-k> shortcut to filter a range of selected text.
For example, if we open kak to the scratch buffer:
*** this is a scratch buffer which won’t be automatically saved ***
*** use it for notes or open a file buffer with the :edit command ***
And use % to select the entire buffer, then <a-k>use it for notes<ret> to filter the selection. The entire buffer is still highlighted blue on screen, the contents of %val{selection} is still the entire buffer, and curiously the contents of %reg{/} is use it for notes.
This process works if I use s to match selections, but I see a lot of scripts using <a-h> or <a-x> to select a line then <a-k> to filter the selection and this method doesn’t seem to be working for me.
I’ve tried the master branch on commit ace499e and the v2019.1.20 tag with and without the -n flag.
I think you are confused about the semantics of the <a-k> command.
<a-k> is designed to reduce the amount of selections, so for example, If you would create a selection on both of the lines (edit: this means 1 per line) you mentioned and then execute <a-k>use it for notes<ret>, you should be left with only the selection of the second line.
The behaviour that I think you expected is indeed what the s command is for.
Thanks for the reply. I was asking about <a-k> because I’m trying to get the previous typed word as a selection in a hook available via %val{selection} or $kak_selection. I’m looking at what some of the completion tools scripts do in an InsertIdle hook, but everyone of these execute-keys commands results in an 'execute-keys' no selections remaining error.
% creates one selection covering the whole document
s selects regex matches within the current selection(s)
the regex foo . bar matches each line (not including the newline at the end)
<ret> executes the s command
Now we have four selections:
foo A bar
foo B bar
foo 3 bar
foo D bar
If we now type <a-k>[A-Z]<ret> we will keep the selections that match [A-Z] (i.e. which contain a capital letter) and discard the rest, resulting in three selections:
foo A bar
foo B bar
foo D bar
On the other hand, if we start from the initial document and instead type %<a-k>[A-Z]<ret>:
% creates one selection covering the whole document
<a-k> keeps selections that match the given regex and discard the rest
the regex [A-Z] matches somewhere within our document-wide selection (actually 3 times, but one is enough)
<ret> executes the <a-k> command
Now we have one selection, still covering the entire document.
All the examples you quote are looking for something more specific than just an arbitrary word. Jedi wants a word with a . in it, Racer wants a word followed by . or ::, Clang wants a word followed by ., -> or ::, and ctags wants a bare word… alone on a line? Or a word that contains only alphanumeric characters? I’m not sure if ^ and$ match the beginning and end of a selection as well as a line.
Anyway, you probably want something simpler than that, maybe something like:
execute-keys -draft <space>b_
<space> removes all secondary cursors, leaving only the primary (but it’s in -draft mode, so it doesn’t mess up the user’s cursors for real)
b goes back a word
_ trims any leading or trailing whitespace from the selection, which is important if bhad to go past multiple whitespace characters to get to the previous word
<a-k> keeps selections that match the given regex and discard the rest
interesting. Then again, coincides with I’ve read so far, assuming of course that the above is followed by the carriage return key.
I’ll have to check on another system
so to recapitulate if I have
*** this is a *scratch* buffer which won't be automatically saved ***
*** use it for notes or open a file buffer with the :edit command ***
and if I were to select the whole buffer with % then <a-k> and finally type notes and <ret> it’s supposed to discard the line everything that doesn’t match notes, correct?
and if I were to select the whole buffer with % then <a-k> and finally type notes and <ret> it’s supposed to discard the line everything that doesn’t match notes , correct?
<a-k> keeps selections. If you select whole buffer, you have single selection. If you press <a-k> and type anything that is inside this selection it will be kept. If you have several selections, you discard selections that doesn’t have the regex, and keep those who match.
For example:
string buffer_name;
int buffer_index;
string window_name;
int window_index;
You want to make all strings near each other, and all ints near other ints. % - select whole buffer:
[string buffer_name;
int buffer_index;
string window_name;
int window_index;]
If you select everything with % or with xJ you then have 1 selection containing the whole buffer. Then if you do <a-k> with a regex, you obtain either:
No selection in case the regex does not match within the selection
The full selection in case the regex does match
However, if you select the first line and do C or <a-C> you obtain 2 selections, one on line 1 and one on line 2. Then if you execute <a-k> with a regex you can obtain:
No selections in case neither of the selections contain a match with the regex
1 selection in case one of the selections match
2 selections, in case both match.
As pointed out by andreyorst:
Again, if you need to select something within selection you need s key. Alt+k keeps selections that contain matches, not matches itself.
Sometimes people use “selections” to mean “things that have been selected”. For example, if I select five apples from the fruit market, I have five selected apples or five selections, and if I select the text “hello” I have five selected characters or five selections.
That is not how Kakoune counts selections. In Kakoune, all the text from one end (the cursor) to the other (the anchor) is a single selection. If you have multiple cursors (by ctrl-clicking with the mouse, by pressing C, by using s, S, <a-s>, or <a-S>) then you have multiple selections, but only as many as you have cursors.
So when we say <a-k> keeps selections matching a regex, we don’t mean “it keeps the individual characters that match the regex selected”, we mean if there are any matches within a single, large selection, then the entire single, large selection is kept.
For example, if I select five apples from the fruit market, I have five selected apples or five selections, and if I select the text “hello” I have five selected characters or five selections.
still, there would be only other items sans a group by the time you select them. And god forbid if you select them all at once.
but you can still delete the whole scratch buffer by % and hitting the d key, right? I don’t see any documentation that I have to select those two lines separately on the scratch buffer with C or Alt-Ceither, and then delete the lines. d doesn’t know any better. Or c for that matter.
What I’d like to see is the same behavior that you would normally get by having those d or c keys.
That is not how Kakoune counts selections. In Kakoune, all the text from one end (the cursor) to the other (the anchor) is a single selection.
but you’re referring to Kakoune now as a third-person object. Kakoune doesn’t know any better.
You do. Not Kakoune.
as a matter of fact, even C or a-C don’t do anything when there are newlines or whitespaces either. There goes the functionality of the purpose of what Alt-k is supposed to do.
So now I’m fighting with and trying to figure out where should I begin with a-C and where to finish.