FANDOM


m (Duplicated)
m (Tagged as duplicated with Get the name of the current file with a copy of the template)
Line 1: Line 1:
 
{{Duplicate|1322}}
 
{{Duplicate|1322}}
Also very similar to [[Get the name of the current file]].
+
<div id="News" style="background-color: #ccffcc; margin: 0 1em 0 1em; padding: 0 10px; border: 1px solid #000;">
  +
<big>'''Duplicate tip'''</big>
  +
  +
This tip is very similar to the following:
  +
*[[Get the name of the current file]]
  +
These tips need to be merged &ndash; see the [[Vim_Tips_Wiki:Merge_guidelines|merge guidelines]].
  +
</div>
 
{{review}}
 
{{review}}
 
{{TipImported
 
{{TipImported

Revision as of 10:44, January 28, 2011

Duplicate tip

This tip is very similar to the following:

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

Duplicate tip

This tip is very similar to the following:

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

Tip 193 Printable Monobook Previous Next

created January 7, 2002 · complexity basic · author Steve Downing · version 5.7


I found this useful when I was learning Java: it simply inserts the current filename without the extension at the cursor position, when you are in insert mode.

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

To keep the extension use:

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

To insert the absolute path of the directory the file is in use:

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

To insert the relative path of the directory the file is in use:

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


References

Comments

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


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.


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.)

Use single quotes instead of double quotes to avoid need for extra backslashes:
'^.*[/\\]src[/\\]'
BTW, matchstr(expand("%:p:h"), '^.*[/\\]src[/\\]\zs.*') is simpler.

Community content is available under CC-BY-SA unless otherwise noted.