There are a few ways you can handle command-line editing in Vim mappings.
For starters, there's CTRL-\ e
which allow you to fully replace the contents of the command-line with the result of an expression. (The documentation at :help c_CTRL-\_e
has example usage.)
In general, you can use getcmdline()
to find what is currently in the command-line, in order to pick the parts that you want. There's also getcmdpos()
and setcmdpos()
to find where the cursor is or change its position.
An alternative to replacing the whole command-line is to emulate the action you want to implement by using repeated use of different keystrokes. For example, if you want a mapping to delete to the end of the current word, you could use getcmdline()
and getcmdpos()
to find how many characters are left in the word and then produce that number of "Delete" keystrokes, which you can easily do with repeat("\<Del>", n)
and is well suited for a cnoremap <expr>
kind of mapping.
If you care about command-line editing (and who among us doesn't?), rather than implementing a bunch of pinky-strain-inducing mappings for it, you might consider using the command-line window, which you can enter using q:
from Normal mode, or CTRL-F
from the command-line, where you can use Vim's other modes (Normal and Insert mode) to edit your command-line before having it executed. You also have access to the command-line history as lines in that window, which you can browse with k
and j
, or search with ?
, etc.