Wikia

Vim Tips Wiki

Changes: Add classname based on filename while in input mode

Edit

Back to page

(Reviewed and cleaned up.)
(Change <tt> to <code>, perhaps also minor tweak.)
Line 3: Line 3:
 
|previous=1046
 
|previous=1046
 
|next=1050
 
|next=1050
|created=November 15, 2005
+
|created=2005
 
|complexity=basic
 
|complexity=basic
 
|author=Don Mitchell
 
|author=Don Mitchell
Line 13: Line 13:
 
|category4=Java
 
|category4=Java
 
}}
 
}}
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:
+
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 <code>,f</code> 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 20: Line 20:
 
</pre>
 
</pre>
   
<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.
+
<code><c-r>=</code> is Control-R followed by the equal sign. This keystroke combination is very useful in input mode. <code>getreg()</code> gets the contents of the <code>%</code> register which has the filename. <code>:r</code> is is a filename modifier that removes the path and the extension. To use this, while in input mode, type <code>,f</code> followed by a space or the escape key to expand the name.
   
==See Also==
+
==See also==
 
*{{help|filename-modifiers}} - More information on Filename Modifiers
 
*{{help|filename-modifiers}} - More information on Filename Modifiers
   

Revision as of 06:05, July 13, 2012

Tip 1048 Printable Monobook Previous Next

created 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

Around Wikia's network

Random Wiki