Vim Tips Wiki
Register
(Undo revision 22771 by 61.247.18.230 because it was just a huge code dump with no explanation that illustrates nothing from the tip that I could see)
(Reviewed and cleaned up.)
Line 1: Line 1:
{{review}}
 
 
{{TipImported
 
{{TipImported
 
|id=1048
 
|id=1048
Line 14: Line 13:
 
|category4=Java
 
|category4=Java
 
}}
 
}}
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 <tt>,f</tt> 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 <tt>:</tt> prompt to play with it.
+
In development it is common for a user to copy or rename a file name and then use that file name, without the full path and extension, as a classname within the file. One way to do this quickly is to use an abbreviation in insert mode. In the following example <tt>,f</tt> is mapped as an abbreviation for the filename without the path and extension. Add the following lines to the .vimrc file to use the abbreviation:
   
 
<pre>
 
<pre>
Line 21: Line 20:
 
</pre>
 
</pre>
   
<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).
+
<tt><c-r>=</tt> is Control-R followed by the equal sign. This keystroke combination is very useful in input mode. <tt>getreg()</tt> gets the contents of the <tt>%</tt> register which has the filename. <tt>:r</tt> is is a filename modifier that removes the path and the extension. To use this, while in input mode, type <tt>,f</tt> followed by a space or the escape key to expand the name.
   
  +
==See Also==
To use this, while in input mode, just type <tt>,f</tt> followed by a space or the Escape key to expand it.
 
  +
*{{help|filename-modifiers}} - More information on Filename Modifiers
   
 
==Comments==
 
==Comments==

Revision as of 21:35, 7 January 2011

Tip 1048 Printable Monobook Previous Next

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


In development it is common for a user to copy or rename a file name and then use that file name, without the full path and extension, as a classname within the file. One way to do this quickly is to use an abbreviation in insert mode. In the following example ,f is mapped as an abbreviation for the filename without the path and extension. Add the following lines to the .vimrc file to use the abbreviation:

" 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 keystroke combination is very useful in input mode. getreg() gets the contents of the % register which has the filename. :r is is a filename modifier that removes the path and the extension. To use this, while in input mode, type ,f followed by a space or the escape key to expand the name.

See Also

Comments