1

I very commonly use CTRL-W to adjust a previous path in the command line, like I'll bring up the directory of the current file or perhaps a previous :e ... command from history that's close, and use CTRL-W to remove the filename and maybe a path segment or two before just typing the new suffix.

One of the annoyances of this is that removing, say, the "foo-bar.baz" filename at the end of the path requires hitting CTRL-W three times, and that particular chord is a little cumbersome.

So I was wondering if I could adjust the word boundaries to include '.' and '-' as word characters, but only in command mode.

3
  • 2
    Have you considered q:?
    – Quasímodo
    Oct 20, 2020 at 20:16
  • At least for me, it's more than 3 (more like 5). You could possibly add/remove - and . from iskeyword for the vim filetype, but that affects word-motions in vim files.
    – D. Ben Knoble
    Oct 20, 2020 at 20:41
  • @D.BenKnoble yeah, that's the thing. Those boundary characters are actually quite important to me in my code, it's just on the command-line they produce this problem. I was thinking maybe an iskeyword set on an autocmd CommandLineEnter event or whatever it is but haven't gotten that fancy before so thought I'd ask around in case anyone had worked out how to do this.
    – scanny
    Oct 20, 2020 at 22:04

1 Answer 1

1

I think I got something working (rewrote completely my original answer). It's another usecase of ++once autocmd.

function! FlashIskWith(what)
    let s:saved_isk = &iskeyword
    autocmd CmdlineChanged ++once exe "set isk=" . s:saved_isk
    exe "set iskeyword+=" . a:what
endfunction
function! C_W_WithIsk(what)
    call FlashIskWith(a:what)
    return "\<C-W>"
endfunction
cnoremap <expr> <C-W> C_W_WithIsk("-,.")

Now, pressing <C-W> in command-line mode will :

  • save current &iskeyword
  • load an autocmd to restore it whenever CmdlineChanged
  • add - and . to &iskeyword
  • "execute" <C-W> (see :h :map-expression)
  • since it deletes the previous word, CmdlineChanged is triggered and &isk is restored.
1
  • 1
    consider setlocal for iskeyword
    – D. Ben Knoble
    Oct 22, 2020 at 17:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.