Vim Tips Wiki
Advertisement

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 or go back to ":e dir/" or use Ctrl-P (in this example: 51 times) or press backspace to delete long-file-name51 or press Ctrl-W 5 times to remove "name51", "-", "file", "-" and "long". These methods are not very comfortable.

Solution

A script which deletes the last filname component from the cmdline after pressing Ctrl-T:

cnoremap <C-T> <C-\>e(RemoveLastPathComponent())<cr>
func! RemoveLastPathComponent()
    return substitute(getcmdline(), '\%(\\ \|[\\/]\@!\f\)\+[\\/]\=$\|.$', , )
endfunc 

Enjoy!

Advertisement