Wikia

Vim Tips Wiki

Changes: Add/change file extension in current buffer

Edit

Back to page

(Move categories to tip template)
(Remove html character entities)
 
Line 23: Line 23:
   
 
<pre>
 
<pre>
command! -nargs=1 AddExt execute "saveas ".expand("%:p").&lt;q-args&gt;
+
command! -nargs=1 AddExt execute "saveas ".expand("%:p").&lt;q-args>
command! -nargs=1 ChgExt execute "saveas ".expand("%:p:r").&lt;q-args&gt;
+
command! -nargs=1 ChgExt execute "saveas ".expand("%:p:r").&lt;q-args>
 
</pre>
 
</pre>
   
Line 51: Line 51:
   
 
----
 
----
Speaking up for mnemonics, some people, newbies especially, have trouble remembering !/*%^, much less, what it may or may not mean. I, for one, find ChgExt easier to remember, not only for use, but for meaning, than w %:p:r.&lt;ext&gt;. When I see the command in my vimrc 2 years from now, I won't have any trouble using it or remembering what it's for. Nor will I have to say, 'Let's see, I did that once 2 years ago... gosh, I'll just look it up again in :h expand' (if I remember that I need to look in :h expand). Let's not loose track of one of the major strongpoints of commands: they make tasks easier. It's the same reason things like aliases and functions exist in shells.
+
Speaking up for mnemonics, some people, newbies especially, have trouble remembering !/*%^, much less, what it may or may not mean. I, for one, find ChgExt easier to remember, not only for use, but for meaning, than w %:p:r.<ext>. When I see the command in my vimrc 2 years from now, I won't have any trouble using it or remembering what it's for. Nor will I have to say, 'Let's see, I did that once 2 years ago... gosh, I'll just look it up again in :h expand' (if I remember that I need to look in :h expand). Let's not loose track of one of the major strongpoints of commands: they make tasks easier. It's the same reason things like aliases and functions exist in shells.
   
 
Now if you want to change the command to say something like
 
Now if you want to change the command to say something like
   
 
<pre>
 
<pre>
command! -nargs=1 ChgExt :w %:p:r.&lt;args&gt;
+
command! -nargs=1 ChgExt :w %:p:r.<args>
 
</pre>
 
</pre>
   
Line 64: Line 64:
   
 
<pre>
 
<pre>
command! -nargs=1 ChgExt :saveas %:p:r.&lt;args&gt;
+
command! -nargs=1 ChgExt :saveas %:p:r.<args>
 
</pre>
 
</pre>
   
Line 73: Line 73:
   
 
<pre>
 
<pre>
:w %&lt;.txt
+
:w %<.txt
 
</pre>
 
</pre>
   
 
will write to ~/thud/foo/bar.txt
 
will write to ~/thud/foo/bar.txt
   
think of a '&lt;' as 'some backspaces' {{help|id=<>|label=&lt;&gt;}}
+
think of a '<' as 'some backspaces' {{help|id=<>|label=&lt;&gt;}}
   
 
*{{help|extension-removal}}
 
*{{help|extension-removal}}

Latest revision as of 09:22, September 29, 2008

Tip 768 Printable Monobook Previous Next

created August 13, 2004 · complexity basic · author Dave Silvia · version 5.7


Say I have a file that was loaded with:

:view ~/thud/foo/bar.c

I now want to save it as, say ~/thud/foo/bar.c.txt

This can be done using these commands.

command! -nargs=1 AddExt execute "saveas ".expand("%:p").<q-args>
command! -nargs=1 ChgExt execute "saveas ".expand("%:p:r").<q-args>

The following saves the current buffer's file as the current buffer's filename with '.c' appended.

:AddExt .c

The following saves the current buffer's file as the current buffer's filename with the extension changed to '.txt'.

:ChgExt .txt

ReferencesEdit

CommentsEdit

:w %:p.txt will save the file with .txt added after the existing extension.
:w %:p:r.txt will replace the extension with txt.

Speaking up for mnemonics, some people, newbies especially, have trouble remembering !/*%^, much less, what it may or may not mean. I, for one, find ChgExt easier to remember, not only for use, but for meaning, than w %:p:r.<ext>. When I see the command in my vimrc 2 years from now, I won't have any trouble using it or remembering what it's for. Nor will I have to say, 'Let's see, I did that once 2 years ago... gosh, I'll just look it up again in :h expand' (if I remember that I need to look in :h expand). Let's not loose track of one of the major strongpoints of commands: they make tasks easier. It's the same reason things like aliases and functions exist in shells.

Now if you want to change the command to say something like

command! -nargs=1 ChgExt :w %:p:r.<args>

that's another matter. But to say 'use this anti-mnemonic formula instead of the command' is not very helpful.

Also, the original question was how to ':saveas' not ':w'. One changes the buffer to the new file, the other does not. So, to be correct, the command should be

command! -nargs=1 ChgExt :saveas %:p:r.<args>

If you have a mind for anti-mnemonics and the like, great! But don't forget many people don't.


One alternative:

:w %<.txt

will write to ~/thud/foo/bar.txt

think of a '<' as 'some backspaces' :help <>


Around Wikia's network

Random Wiki