Vim Tips Wiki
(Remove html character entities)
(Reword and use expand().)
 
(5 intermediate revisions by 5 users not shown)
Line 1: Line 1:
{{review}}
 
 
{{TipImported
 
{{TipImported
 
|id=1048
 
|id=1048
 
|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 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 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('%'), ':r')<CR>
+
iab <buffer> <unique> ,f <C-r>=expand('%:t:r')<CR>
 
</pre>
 
</pre>
   
  +
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).
<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).
 
   
  +
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 input mode, just type <tt>,f</tt> followed by a space or the Escape key to expand it.
 
  +
 
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==
  +
*{{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, 20 December 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 also[]

Comments[]

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)