# Improved and consistent line selection with X and \<a-X\>

**URL:** <https://discuss.kakoune.com/t/improved-and-consistent-line-selection-with-x-and-a-x/2243>\
**Category:** Recipes & Guides\
**Tags:** key\
**Created:** [June 16, 2023, 3:02am UTC](https://discuss.kakoune.com/t/improved-and-consistent-line-selection-with-x-and-a-x/2243 "2023-06-16T03:02:28Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![arrufat](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/arrufat/32/1157_2.png) [@arrufat](https://discuss.kakoune.com/u/arrufat)\
**Post date:** [June 16, 2023, 3:02am UTC](https://discuss.kakoune.com/t/improved-and-consistent-line-selection-with-x-and-a-x/2243/1 "2023-06-16T03:02:29Z")

</div>

Ever since Kakoune made the `x` key more predictable, I missed the easier way it provided to select new lines. However, I still prefer the new `x` behavior.

The solution was to either `xJJJJ` or `JJJJx`. This was good because it also worked upwards, by using `K` instead of `J`.

After some tweaking, I found a couple of mappings that are so good, I think they should even be hard-coded:

```auto
map -docstring 'extend lines downwards' global normal X %{<a-:>F<ret>x}
map -docstring 'extend lines upwards' global normal <a-X> %{<a-:><a-;><a-H>Kx}

```

I think this configuration makes a ton of sense and matches the behavior of other commands in Kakoune, such as `/` to search, `?` to extend, and the `alt-` variants to do it backwards.

What do you think?

---

<div class="post-metadata">

**Author:** ![natasky](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/natasky/32/1049_2.png) [@natasky](https://discuss.kakoune.com/u/natasky)\
**Post date:** [June 19, 2023, 8:19am UTC](https://discuss.kakoune.com/t/improved-and-consistent-line-selection-with-x-and-a-x/2243/2 "2023-06-19T08:19:25Z")

</div>

I like how `<a-X>` works.

`X` seems not to extend to next line if it’s empty. Changing `T` to `F` fixes that -

```auto
map -docstring 'extend lines downwards' global normal X %{<a-:>F<ret>x}

```

Didn’t test much, don’t know if it causes other issues.

---

<div class="post-metadata">

**Author:** ![arrufat](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/arrufat/32/1157_2.png) [@arrufat](https://discuss.kakoune.com/u/arrufat)\
**Post date:** [June 20, 2023, 3:35am UTC](https://discuss.kakoune.com/t/improved-and-consistent-line-selection-with-x-and-a-x/2243/3 "2023-06-20T03:35:37Z")

</div>

Well spotted, thank you!  
Modifying the original post, so that people don’t copy it by mistake.

---

<div class="post-metadata">

**Author:** ![arrufat](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/arrufat/32/1157_2.png) [@arrufat](https://discuss.kakoune.com/u/arrufat)\
**Post date:** [June 20, 2023, 3:45am UTC](https://discuss.kakoune.com/t/improved-and-consistent-line-selection-with-x-and-a-x/2243/4 "2023-06-20T03:45:01Z")

</div>

By the way, is it normal that mappings don’t work with a count? I would expect to be able to do `3X` to extend the selection by 3 lines.

---

<div class="post-metadata">

**Author:** ![Screwtapello](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/screwtapello/32/12_2.png) [@Screwtapello](https://discuss.kakoune.com/u/Screwtapello)\
**Post date:** [June 20, 2023, 4:19am UTC](https://discuss.kakoune.com/t/improved-and-consistent-line-selection-with-x-and-a-x/2243/5 "2023-06-20T04:19:31Z")

</div>

The right-hand-side of a mapping can use the `%val{count}` expansion to apply the active count to whichever parts of the mapping it’s relevant for. Unfortunately it defaults to 0 rather than 1, but 0 is not a normal-mode command so if you’re just prefixing to a different normal-mode command it should be fine:

```auto
map -docstring 'cool j' global user j %{:exec "%val{count}j:echo cool!<lt>ret>"<ret>}

```

Test with `5<space>j`.

Note: You need the `:exec` in the mapping so that `%val{count}` is not expanded until the mapping is executed, rather than when the mapping was defined.

---

<div class="post-metadata">

**Author:** ![arrufat](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/arrufat/32/1157_2.png) [@arrufat](https://discuss.kakoune.com/u/arrufat)\
**Post date:** [June 20, 2023, 4:30am UTC](https://discuss.kakoune.com/t/improved-and-consistent-line-selection-with-x-and-a-x/2243/6 "2023-06-20T04:30:40Z")

</div>

Thank you! I’ve submitted a PR, to see if there’s interest in having this in Kakoune by default:

> <https://github.com/mawww/kakoune/pull/4929>
>
> Since we changed the \`x\` keys behaviors, the \`X\` and \`\<a-X\>\` have been free.
> 
> …I think it makes sense to make them work mostly like \`/\` and other commands, where shift extends the selection and alt changes the direction.
> 
> So, the implemented behavior is:
> \`\<X\>\` : select the current and next line (extend downwards)
> \`\<a-X\>\` : select the current and previous line (extend upwards)
> 
> Overall, I think it's a good change that's consistent with the way other keys (and modifiers) behave in Kakoune.
> 
> Note: in this implementation, pressing once \`\<X\>\` or \`\<a-X\>\` will result in two lines being selected (regardless of the current line being completely selected). This makes the keys more predictable, but it might be surprising at the beginning, if someone expects the old \`X\` behavior.

---

<div class="post-metadata">

**Author:** ![GavinSmith0123](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/gavinsmith0123/32/1112_2.png) [@GavinSmith0123](https://discuss.kakoune.com/u/GavinSmith0123)\
**Post date:** [December 27, 2024, 11:25pm UTC](https://discuss.kakoune.com/t/improved-and-consistent-line-selection-with-x-and-a-x/2243/7 "2024-12-27T23:25:53Z")

</div>

This was useful after I upgraded Kakoune and my old configuration (which used the `X` command that seems to have disappeared) broke. In fact, I map `"x"` instead of `"X"` because I use this so much.

I was completely used to typing `xx` to select two lines and would have been lost without this.

---

<div class="post-metadata">

**Author:** ![7v0lk0v](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/7v0lk0v/32/1453_2.png) [@7v0lk0v](https://discuss.kakoune.com/u/7v0lk0v)\
**Post date:** [February 10, 2025, 9:28am UTC](https://discuss.kakoune.com/t/improved-and-consistent-line-selection-with-x-and-a-x/2243/8 "2025-02-10T09:28:53Z")

</div>

I think this definition is more consistent:

`map -docstring 'extend lines downwards' global normal X %{<a-:><a-L>Jx}`

…not just in code similarity but also in behaviour: in the original version, pressing `X` selects one line but `<a-X>` immediately selects 2 - now both versions start with 2 lines selected (and if you want just one, well there’s good ol’ `x`).

Thanks for sharing.

---

<div class="post-metadata">

**Author:** ![alexherbo2](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/alexherbo2/32/1140_2.png) [@alexherbo2](https://discuss.kakoune.com/u/alexherbo2)\
**Post date:** [February 10, 2025, 5:24pm UTC](https://discuss.kakoune.com/t/improved-and-consistent-line-selection-with-x-and-a-x/2243/9 "2025-02-10T17:24:20Z")

</div>

```kak
define-command select_whole_lines_or_extend_lines_down %{
  try %{
    # At least one selection is not full, so select whole lines.
    execute-keys -draft '<a-K>\A^.*\n\z<ret>'
    execute-keys '<a-:>x'
  } catch %{
    execute-keys '<a-:>Jx'
  }
}

map -docstring 'select whole lines or extend lines down' global normal x ':select_whole_lines_or_extend_lines_down<ret>'
map -docstring 'select whole lines' global normal X x

```
