Abstract
When in command line and using filename autocompletion, you may use Cntl-N (Tab) for next match, Ctrl-P (Shift-Tab) for previous, and Ctrl-W for generic remove-one-word-from-cmdline. It may happen that you browse a directory and want to browse another directory:
:e dir1/long-file-name1 :e dir1/long-file-name2 ... :e dir1/long-file-name51
now you want to have
:e dir2/
in the cmdline. You either have to Ctrl-N to cycle through the rest of the files; use Ctrl-P (in this example: 51 times) to cycle back; press backspace to delete long-file-name51; or press Ctrl-W five times to remove the words "name51", "-", "file", "-" and "long" from the cmdline. These methods are not very comfortable.
Solution
cnoremap <C-T> <C-\>e(RemoveLastPathComponent())<cr>
func! RemoveLastPathComponent() return substitute(getcmdline(), '\%(\\ \|[\\/]\@!\f\)\+[\\/]\=$\|.$', , ) endfunc
Description: Ctrl-T removes the last path component. It uses 'isfname' and stops at characters not included, except escaped spaces. At least one character is always removed.
" variant, first removes the extension: func! RemoveLastPathComponent2() let cl = getcmdline() let clr = fnamemodify(getcmdline(), ":r") if cl != clr return clr else " return fnamemodify(getcmdline(), ":h") return RemoveLastPathComponent() endif endfunc
Enjoy!