Wikia

Vim Tips Wiki

Changes: Add classname based on filename while in input mode

Edit

Back to page

(':r' only removes the suffix, not the path. ':t:r' works a lot better for me.)
(Reword and use expand().)
 
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 <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:
+
In program development, it is common to use the name of the current file as a classname, after omitting the path and extension of the file. One way to do this quickly is to use an insert-mode abbreviation. 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 your [[vimrc]] file to define the abbreviation:
   
 
<pre>
 
<pre>
" classname (the filename minus the path and extension)
+
" Insert classname (filename minus path and extension).
iab <buffer> <unique> ,f <c-r>=fnamemodify(getreg('%'), ':t:r')<CR>
+
iab <buffer> <unique> ,f <C-r>=expand('%:t:r')<CR>
 
</pre>
 
</pre>
   
<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.
+
In insert mode, typing <code><C-r>=</code> (Ctrl-R then <code>=</code>) inserts the value of the following expression. The expression is <code>expand('%:t:r')</code> (terminated by <code><CR></code> which represents Enter).
  +
  +
The <code>expand()</code> function expands <code>'%'</code> (representing the name of the current file), while modifying that name according to the given codes: <code>:t</code> is the tail of the full path name (file name and extension, after omitting any path), and <code>:r</code> is the root part of the name after omitting any extension.
  +
  +
To use this, while in insert mode, type <code>,f</code> followed by a space or the escape key to insert the name of the current file.
   
 
==See also==
 
==See also==
*{{help|filename-modifiers}} - More information on Filename Modifiers
+
*{{help|filename-modifiers}} information on filename modifiers
   
 
==Comments==
 
==Comments==
  +
Rather than vimrc, the mapping should be in a file in a filetype plugin directory. Then, using <code><buffer></code> would be useful (the abbreviation would only apply for files of that type). The tip needs some rewording for that. [[User:JohnBeckett|JohnBeckett]] ([[User talk:JohnBeckett|talk]]) 10:31, December 20, 2012 (UTC)

Latest revision as of 10:31, December 20, 2012

Tip 1048 Printable Monobook Previous Next

created 2005 · complexity basic · author Don Mitchell · version 6.0


In program development, it is common to use the name of the current file as a classname, after omitting the path and extension of the file. One way to do this quickly is to use an insert-mode abbreviation. In the following example ,f is mapped as an abbreviation for the filename without the path and extension. Add the following lines to your vimrc file to define the abbreviation:

" Insert classname (filename minus path and extension).
iab <buffer> <unique> ,f <C-r>=expand('%:t:r')<CR>

In insert mode, typing <C-r>= (Ctrl-R then =) inserts the value of the following expression. The expression is expand('%:t:r') (terminated by <CR> which represents Enter).

The expand() function expands '%' (representing the name of the current file), while modifying that name according to the given codes: :t is the tail of the full path name (file name and extension, after omitting any path), and :r is the root part of the name after omitting any extension.

To use this, while in insert mode, type ,f followed by a space or the escape key to insert the name of the current file.

See alsoEdit

CommentsEdit

Rather than vimrc, the mapping should be in a file in a filetype plugin directory. Then, using <buffer> would be useful (the abbreviation would only apply for files of that type). The tip needs some rewording for that. JohnBeckett (talk) 10:31, December 20, 2012 (UTC)

Around Wikia's network

Random Wiki