Vim Tips Wiki
Advertisement
Tip 1048 Printable Monobook Previous Next

created November 15, 2005 · complexity basic · author Don Mitchell · version 6.0


I often copy or rename Java files, and then have to change the name of the class to match the filename (minus the path to the file and the .java extension). To do this I added an abbreviation. I chose ,f as the abbreviation, but you can use anything. To try this out put the following lines in your vimrc or just run it at the : prompt to play with it.

" classname (the filename minus the path and extension)
iab <buffer> <unique> ,f <c-r>=fnamemodify(getreg('%'), ':r')<CR>

<c-r>= is Control-R followed by the equal sign. This is very useful in input mode. getreg() gets the contents of the % register which has the filename (which may or may not have the full path depending on how you loaded the file). :r is is a filename modifier that removes the path and the extension (:help filename-modifiers for more info).

To use this, while in input mode, just type ,f followed by a space or the Escape key to expand it.

Comments

Advertisement