Vim Tips Wiki
(Added to Automated_Text_Insertion Category + help links + my comment)
(Duplicate of 596, 1322)
Line 1: Line 1:
  +
{{Duplicate|596|1322}}
 
{{review}}
 
{{review}}
 
{{Tip
 
{{Tip

Revision as of 19:47, 14 August 2007

Duplicate tip

This tip is very similar to the following:

These tips need to be merged – see the merge guidelines.

Previous TipNext Tip

Tip: #193 - Insert current filename

Created: January 7, 2002 16:00 Complexity: basic Author: Steve Downing <sweb--AT--REMOVE_THIS_TO_REPLY.helmsdeep.net Version: 5.7 Karma: 37/23 Imported from: Tip#193

I found this one good for when I was starting to learn Java, it simply inserts the current filename, at the cursor position, when you are in insert mode. Honestly, its a mish-mash of some other tips I found here, but I thought it might be useful.

:inoremap \fn <C-R>=expand("%:t:r")<CR> 

Enjoy!

References

Comments

I often use <C-R>% when in insert mode... It does the same thing, except you also get your file extension.

bindu--AT--wavell.net , January 22, 2002 17:38


Cool tip. I find myself needing to do this whenever I create a new Java file.

isaac.sparrow--AT--engineer.com , March 1, 2002 14:44


The % variable for file name is also helpful from the command prompt (: at the bottom). I use it for checking the syntax of source code before using it. For example, if I am editing a perl module, this is helpful:

:w|!perl -c % 

This saves the file (w), and executes perl -c on the current file which runs a syntax check.

ryangerry--AT--yahoo.com , September 28, 2005 8:21


Yet another useful % application: suppose your source-files reside in a '.../src' (or '...\src') directory. Now this line will insert the package-structure of you current Java-file at the cursor position:

:inoremap \jip <C-R>=substitute(substitute(expand("%:p:h"), "^.*[/\\\\]src[/\\\\]", "", ""), "[/\\\\]", ".", "g")<CR>

(Four backslashes because you need to escape them twice.)

vim.org--AT--guzsvany.de , July 4, 2007 14:05

Then use single quotes instead of double quotes: '^.*[/\\]src[/\\]'
BTW, matchstr(expand("%:p:h"), '^.*[/\\]src[/\\]\zs.*') is simplier
Luc Hermitte 15:25, 6 July 2007 (UTC)