History
Article Edit this page Discussion

Add classname based on filename while in input mode

From Vim Tips Wiki

Jump to: navigation, search

Tip 1048 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.

[edit] Comments

Rate this article:

Share this article:

Hubs Highlights International Sites Wikia messages
Entertainment
Gaming
Cartoons & Comics
Science Fiction
Hobbies
Sports
See all...
Grand Theft Auto
Pixar
Legend of Zelda Wiki
Terminator Wiki
Everquest II Wiki
Dark Shadows
German
Spanish
Chinese
Japanese
More...
Wikia is hiring for several open positions


Vote for collaboration of the month.

Send this article to a friend
"Add classname based on filename while in input mode"
 
 
Hi!

I thought you'd like this page from Wikia!

http://vim.wikia.com

Come check it out!
Send confirmation