Hello.
In vim, while in insert mode, the parenthesis matched will be the one before the cursor.
It would be nice that kakoune behaves the same because this make it easy to see if I need to type another parenthesis or not.
Because, currently, when adding characters at the end of the line, for instance, no matching parenthesis will be shown.
By the way, is there any way to enable this by default in every buffer?
Also, is there a way to make the highlighting more apparent? Like having another background color.
I tried the following, but it does not work:
hook global BufSetOption add-highlighter show_matching
Thanks to fix this issue.
hello,
to enable the show_matching highlighter by default add the following snippet to your kakrc:
hook global WinCreate .* %{
addhl show_matching
}
To change the face of the show_matching highlighter, and let it pop out visually, you can modify the MatchingChar face. You can read up on faces in the readme or use :doc faces from within kakoune. For example you could add set-face MatchingChar red,white+b to the previous mentioned hook.
@ekie Thanks. The only thing that I miss now is the behaviour in insert mode.
The matching algorithm relies on the character that's under the anchor because it's a hint as to what would be selected using the m/M primitives. To know if you need another closing parenthesis, just exit insert mode and the anchor will be moved back to the last character you typed, highlighting the opening one.
@lenormf Thanks for the explanation.
I think I don't quite understand kakoune, currently (I'm a new user).
I like the idea of using selections to have an immediate visual feedback of what you're doing, but I feel like some tasks are slower to do with kakoune than with vim.
This issue is one example.
Another example is that we don't have (and it does not seem ideomatic to have) a <c-w> binding to delete the previous word (I know I could add this binding, but it seems not to be the kakoune way).
In both of these examples, the idiomatic way (as I understand) is to go back to normal mode.
But I feel it is slower that what I'd do with vim.
I can understand that editing the code (instead of inserting new code) is powerful in normal mode (hence why <c-w> might not be needed), but one process that I do a lot is using a command from the history (in command mode), changing the last word and running it again.
In this case, a shortcut like <c-w> is really useful.
Once again, I know that I could add such a shortcut, but I want to understand how to use kakoune in the most efficient/idiomatic way.
As a new kakoune user, I am at a loss.
Could you give me some explanations?
Thanks.
From :doc faq
Why aren鈥檛 widely known command line shortcuts such as
or available in Kakoune ?
Despite their widespread availability in multiple tools, those shortcuts do not fit the paradigm that Kakoune implements, which is based on selections first.
However, you can easily declare key mappings in your configuration file to be able to use those control-based shortcuts in insert mode (c.f. the "map" command in the "commands" documentation page).
show_matching highlighter could be improved to give a visual feedback while inserting.
That's what I'm saying: doing so doesn't make sense because the job of this highlighter is to show the matching character of the one currently in the anchor. There's no reason to make it use the previous one in insert mode, it seems broken.
I've implemented the feature requested with a kak script:
decl -hidden range-faces show_matching_range
hook global InsertChar .+ %{ eval -draft %{
set buffer show_matching_range ""
try %{
exec <esc>\;hm<a-k>..<ret>\;
set buffer show_matching_range "%val{timestamp}:%val{selection_desc}|MatchingChar"
}
} }
hook global WinCreate .* %{
addhl ranges show_matching_range
}
An implementation has been provided, can we close this now?
Here is a slightly improved version that avoids running too many WinSetOption and BufSetOption hooks by only removing the highlighter for the matching character once when it has been set.
decl -hidden range-faces show_matching_range
hook global -group kakrc InsertChar [[(<{}>)\]] %{ eval -draft %{
try %{
exec -no-hooks <esc>\;hm<a-k>..<ret>\;
set window show_matching_range "%val{timestamp}:%val{selection_desc}|MatchingChar"
}
hook window -group once-matching InsertChar [^[(<{}>)\]] %{
set window show_matching_range ""
remove-hooks window once-matching
}
} }
hook global -group kakrc InsertEnd .* %{
set buffer show_matching_range ""
}
hook global -group kakrc WinCreate .* %{
addhl ranges show_matching_range
}
Here's a version updated for kakoune as of 2018-04-06 (main differences are 'range-specs' rather than 'range-faces', and the additional window argument to add-highlighter).
I also added a catch to the try so that the matching char is correctly unhighlighted after typing a mismatched brace.
declare-option -hidden range-specs show_matching_range
hook global -group kakrc InsertChar '[[\](){}<>]' %{
eval -draft %{
try %{
exec -no-hooks <esc>\;hm<a-k>..<ret>\;
set window show_matching_range "%val{timestamp}:%val{selection_desc}|MatchingChar"
} catch %{
set window show_matching_range ""
}
hook window -group improved-matching-range-once-matching InsertChar '[^[\](){}<>]' %{
set window show_matching_range ""
remove-hooks window improved-matching-range-once-matching
}
}
}
hook global -group kakrc WinCreate .* %{
add-highlighter window ranges show_matching_range
}
Is this snippet in the wiki? It probably should be, so that we can leave this issue alone and update code in the community documentation instead.
That's a good idea! I'll try to find/make a page and update this comment with the link.
I hadn't considered making or putting this on a wiki page, just that the version here didn't work and that other people might have tried to find it here.
Here is an updated version accounting for the last breaking changes:
declare-option -hidden range-specs show_matching_range
hook global InsertChar '[[\](){}<>]' %{
eval -draft %{
try %{
exec '<esc>;hm<a-k>..<ret>;'
set window show_matching_range %val{timestamp} "%val{selection_desc}|MatchingChar"
} catch %{
set window show_matching_range 0
}
hook window -once InsertChar '[^[\](){}<>]' %{
set window show_matching_range 0
}
}
}
add-highlighter global/ ranges show_matching_range
If somebody want to add that to the wiki feel free, or I'll do it myself
As "kd" pointed out on IRC, this implementation doesn't always clear up after itself if you don't insert a character after the matching bracket: if you hit an arrow key, or <esc> to go back to normal mode, etc.
Also, it would be nice if the regex were somehow based on %opt{matching_pairs} instead of hard-coded (but not a big deal, since I imagine a lot of people don't change that option).
I think including a few more hooks should fix the clearing up issue. I'm using this, modified from @occivink's comment, and it seems to work well:
hook global -group kakrc-matching-ranges InsertChar '[[\](){}<>]' %{
eval -draft %{
try %{
exec '<esc>;hm<a-k>..<ret>;'
set window show_matching_range %val{timestamp} "%val{selection_desc}|MatchingChar"
} catch %{
set window show_matching_range 0
}
hook window -once InsertChar '[^[\](){}<>]' %{
set window show_matching_range 0
}
hook window -once ModeChange .* %{
set window show_matching_range 0
}
hook window -once InsertMove .* %{
set window show_matching_range 0
}
}
}
I've still got no idea where in the wiki this belongs though. Would we make a new page named something like "Show matching braces while typing"? Or is there anywhere more generic it could go?
The Highlighters page, I guess?
I was avoiding that one mostly because it seems to be tips about making highlighters, not a list of fully built highlighter ready to use. Though I see "Smart search highlighting" is there, so maybe it is appropriate.
I think the above code highlights a bug, because -group -once is supposed to raise an error!
Ah, sorry, a copy/edit error on my part. I had them with groups in my config but wanted to post a version without and I incompletely removed them.
Reported in #2414.
Most helpful comment
Here is a slightly improved version that avoids running too many
WinSetOptionandBufSetOptionhooks by only removing the highlighter for the matching character once when it has been set.