Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ex commands: blockwise, charwise range #22297

Open
beyarkay opened this issue Feb 17, 2023 · 8 comments
Open

Ex commands: blockwise, charwise range #22297

beyarkay opened this issue Feb 17, 2023 · 8 comments
Labels
enhancement feature request
Milestone

Comments

@beyarkay
Copy link

Problem

(Originally asked about solutions here)

In vim/neovim, ranges are only line-wise. I'd like to be able to perform commands on column- and line-wise ranges. There are more examples in the Stack overflow post, but I find it odd that the vim help pages talk about line-based ranges as though column-based ranges were planned in the future:

							*:visual_example*
Currently the ":" command works on whole lines only.  When you select part of
a line, doing something like ":!date" will replace the whole line.  If you
want only part of the line to be replaced you will have to make a mapping for
it.  In a future release ":" may work on partial lines.

Is there any tracking issue for the comment "In a future release ":" may work on partial lines"? If not, I felt there should be one. It would be a big help.

There is a plugin that partially solves this: vis, although it's not as ergonomic as it could be.

Expected behavior

If a block of text were selected using ctrl-v (or even just a part of a line of text), then pressing : should auto-fill the partial-line range selector: :`<,`> instead of it's current behaviour, which auto-fills the line-based range selector: :'<,'>. I'm not sure if it's feasible (or possible) to migrate all line-based commands to the new selector.

@beyarkay beyarkay added the enhancement feature request label Feb 17, 2023
@justinmk
Copy link
Member

justinmk commented Feb 17, 2023

auto-fill the partial-line range selector: :`<,`>

This is in vim's todo.txt I believe. vim/vim#3292

It is relevant for all commands, not just :.

Closest thing I could find is: https://github.com/vim/vim/blob/1d87e11a1ef201b26ed87585fba70182ad0c468a/runtime/doc/todo.txt#L5743-L5745

7   Substitute in a block of text.  Use {line}.{column} notation in an Ex
    range, e.g.: ":1.3,$.5s" means to substitute from line 1 column 3 to the
    last line column 5.

But this could be for all commands, not just :s. And I agree that backtick mark notation should work charwise:

:`<,`>foo
:`a,`bfoo

Note that charwise is different than blockwise. Would need some way to disambiguate that.

@justinmk justinmk changed the title Use ctrl-v to visually mark some block, and then : to execute a command over that block Ex commands: blockwise/charwise range Feb 17, 2023
@justinmk justinmk added this to the backlog milestone Feb 17, 2023
@justinmk justinmk changed the title Ex commands: blockwise/charwise range Ex commands: blockwise, charwise range Feb 17, 2023
@justinmk
Copy link
Member

otoh, it's pretty easy to make a plugin that :

  1. copies the selected block,
  2. pastes it into a new scratch buffer
  3. sets 'filetype' to match the original buffer
  4. executes the command

Not perfect but could be pretty useful.

@ad-chaos
Copy link

ad-chaos commented Feb 17, 2023

You can use the \%V atom to only match in the visual area (thanks to chrisbra)

@justinmk
Copy link
Member

justinmk commented Feb 18, 2023

use the \%V atom

That is a special case for search and :substitute, but doesn't answer the general idea of being able to pass a charwise or blockwise range to commands.

@justinmk justinmk modified the milestones: backlog, unplanned Feb 18, 2023
@beyarkay
Copy link
Author

Good catch about blockwise selection being different from charwise selection. Maybe something like :`a,`bfoo for charwise and :1.3,$.5foo for blockwise?

I'm not fully up to date on nvims goals, does something like this belong in the main program or is it more suitable for a plugin? I don't have experience with plugins/nvim internals but would be keen to get my hands dirty

@justinmk
Copy link
Member

This would probably need to be a core change

@beyarkay
Copy link
Author

Okay, who would I speak to in order to get an idea about what the definition of done is? Maybe this would need to be broken into a charwise and a blockwise change, although I'm not sure what your status quo is.

@justinmk
Copy link
Member

what the definition of done is? Maybe this would need to be broken into a charwise and a blockwise change

Yeah, let's start with charwise first. I'd say "done" for charwise is:

Proposal

  • Support backtick ranges pass column info to the command
    :`<,`>foo
    :`a,`bfoo
    
    • Note: it's vaguely possible that parsing a backtick range might conflict with :help backtick-expansion. Probably isn't an issue because :help backtick-expansion does not happen in the "range" part (the start) of a command.
  • Column info is available as <col1> , <col2> (like :help <line1>)
    • For user-defined command, :command -addr=lines
  • Support "x.y" ranges pass column info to the command
    :1.2,2.2 foo
    
  • Update nvim_parse_cmd() , nvim_cmd()
  • Add charwise support to a few common Ex commands:
    • :read
    • :write
    • :substitute
    • :global
    • ...
    • Document the rest.
  • Update docs: :help :range,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement feature request
Projects
None yet
Development

No branches or pull requests

3 participants