How do I insert the current filename into the contents in Vim?












36














After some searching, I got to know :echo @% displays the current filename in the bottom line of vim-screen.



I would like to dump the filename (with and without full path) into the contents of the file without leaving vim.



Is there any way to do this?










share|improve this question





























    36














    After some searching, I got to know :echo @% displays the current filename in the bottom line of vim-screen.



    I would like to dump the filename (with and without full path) into the contents of the file without leaving vim.



    Is there any way to do this?










    share|improve this question



























      36












      36








      36


      10





      After some searching, I got to know :echo @% displays the current filename in the bottom line of vim-screen.



      I would like to dump the filename (with and without full path) into the contents of the file without leaving vim.



      Is there any way to do this?










      share|improve this question















      After some searching, I got to know :echo @% displays the current filename in the bottom line of vim-screen.



      I would like to dump the filename (with and without full path) into the contents of the file without leaving vim.



      Is there any way to do this?







      vim






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Sep 29 '15 at 23:16









      muru

      1




      1










      asked Dec 4 '12 at 14:31









      mtk

      8,0432760103




      8,0432760103






















          6 Answers
          6






          active

          oldest

          votes


















          25














          I'm sure there are other ways to do this... but the main command you want is :r[ead] which inserts the output of a command into the buffer.



          So, to insert just the file name:



          :r! echo %


          And, to include the full path:



          :r! echo %:p


          For more info:



          :help read
          :help filename-modifiers





          share|improve this answer































            36














            The current filename is in the "% register, so you can insert it (while in insert mode) with <c-r>%; the full path can be inserted with <c-r>=expand("%:p"). You could make a macro of it if you use it often. For more, similar tricks, see :h expand and :h "=.






            share|improve this answer























            • Maybe I'm misreading that, but isn't <c-r> mapped as the redo command?
              – jonyamo
              Dec 4 '12 at 15:38








            • 10




              In insert mode, <c-r> specifies that the next character is a register.
              – Kevin
              Dec 4 '12 at 15:43










            • Righto, thanks. Totally forgot to switch to insert mode...
              – jonyamo
              Dec 4 '12 at 15:49












            • You can also do "%p in normal or visual modes.
              – naught101
              Aug 25 '17 at 2:36



















            13














            As can be seen in :h registers, the "% register contains the current file name. The :pu[t] command inserts the content of a register into the text.



            So, to insert the actual filename you can type either of these, in command mode:



            :put %


            or



            "%p


            To insert the filename with the full path, type



            :put=expand('%:p')


            in command mode.





            More info:



            :h pu[t]


            By typing "rp you can paste the contents of register "r.






            share|improve this answer



















            • 2




              The :put "%:p" doesn't seem to work for me. It seems to work only with expand(), like in: :put =expand('%:p'), which makes it not much less cumbersome than <c-r>=expand('%:p') unfortunately.
              – akavel
              Sep 16 '15 at 11:21










            • @akavel You're correct, I'll update the answer. Thank you! ;) And, indeed, both are equally verbose.
              – braunmagrin
              Sep 29 '15 at 23:00





















            4














            A simple way is to run:



            !!echo %



            • !! is replacing the current line with the result of the command following it.


            • % is replaced by the name of the edited file in the command so this will effectively insert that name in the edited file.



            The filename is the one you passed to the vi(m) command and might contain a relative or absolute path. Should you want to strip it and only retain the file name, run



            !!basename %





            share|improve this answer































              1














              If you need to do this frequently, it might be useful to bind a key sequence to what you want, as per http://vim.wikia.com/wiki/Insert_current_filename




              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>



              The above all work directly in vim for the session, or you can put it in the .vimrc (where the leading colon on the line is optional).






              share|improve this answer































                0














                I was googling a simplest way to insert current file name (without path or extension) to current position (no new line, no replace current line). So combining previous answers and this link, this is what I was looking for.



                In insert mode:



                <C-R>=expand("%:t:r")


                Then next time (with the command already in history):



                <C-R>=<UP>





                share|improve this answer





















                  Your Answer








                  StackExchange.ready(function() {
                  var channelOptions = {
                  tags: "".split(" "),
                  id: "106"
                  };
                  initTagRenderer("".split(" "), "".split(" "), channelOptions);

                  StackExchange.using("externalEditor", function() {
                  // Have to fire editor after snippets, if snippets enabled
                  if (StackExchange.settings.snippets.snippetsEnabled) {
                  StackExchange.using("snippets", function() {
                  createEditor();
                  });
                  }
                  else {
                  createEditor();
                  }
                  });

                  function createEditor() {
                  StackExchange.prepareEditor({
                  heartbeatType: 'answer',
                  autoActivateHeartbeat: false,
                  convertImagesToLinks: false,
                  noModals: true,
                  showLowRepImageUploadWarning: true,
                  reputationToPostImages: null,
                  bindNavPrevention: true,
                  postfix: "",
                  imageUploader: {
                  brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                  contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                  allowUrls: true
                  },
                  onDemand: true,
                  discardSelector: ".discard-answer"
                  ,immediatelyShowMarkdownHelp:true
                  });


                  }
                  });














                  draft saved

                  draft discarded


















                  StackExchange.ready(
                  function () {
                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f57555%2fhow-do-i-insert-the-current-filename-into-the-contents-in-vim%23new-answer', 'question_page');
                  }
                  );

                  Post as a guest















                  Required, but never shown

























                  6 Answers
                  6






                  active

                  oldest

                  votes








                  6 Answers
                  6






                  active

                  oldest

                  votes









                  active

                  oldest

                  votes






                  active

                  oldest

                  votes









                  25














                  I'm sure there are other ways to do this... but the main command you want is :r[ead] which inserts the output of a command into the buffer.



                  So, to insert just the file name:



                  :r! echo %


                  And, to include the full path:



                  :r! echo %:p


                  For more info:



                  :help read
                  :help filename-modifiers





                  share|improve this answer




























                    25














                    I'm sure there are other ways to do this... but the main command you want is :r[ead] which inserts the output of a command into the buffer.



                    So, to insert just the file name:



                    :r! echo %


                    And, to include the full path:



                    :r! echo %:p


                    For more info:



                    :help read
                    :help filename-modifiers





                    share|improve this answer


























                      25












                      25








                      25






                      I'm sure there are other ways to do this... but the main command you want is :r[ead] which inserts the output of a command into the buffer.



                      So, to insert just the file name:



                      :r! echo %


                      And, to include the full path:



                      :r! echo %:p


                      For more info:



                      :help read
                      :help filename-modifiers





                      share|improve this answer














                      I'm sure there are other ways to do this... but the main command you want is :r[ead] which inserts the output of a command into the buffer.



                      So, to insert just the file name:



                      :r! echo %


                      And, to include the full path:



                      :r! echo %:p


                      For more info:



                      :help read
                      :help filename-modifiers






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Dec 4 '12 at 16:06

























                      answered Dec 4 '12 at 14:41









                      jonyamo

                      1,6731217




                      1,6731217

























                          36














                          The current filename is in the "% register, so you can insert it (while in insert mode) with <c-r>%; the full path can be inserted with <c-r>=expand("%:p"). You could make a macro of it if you use it often. For more, similar tricks, see :h expand and :h "=.






                          share|improve this answer























                          • Maybe I'm misreading that, but isn't <c-r> mapped as the redo command?
                            – jonyamo
                            Dec 4 '12 at 15:38








                          • 10




                            In insert mode, <c-r> specifies that the next character is a register.
                            – Kevin
                            Dec 4 '12 at 15:43










                          • Righto, thanks. Totally forgot to switch to insert mode...
                            – jonyamo
                            Dec 4 '12 at 15:49












                          • You can also do "%p in normal or visual modes.
                            – naught101
                            Aug 25 '17 at 2:36
















                          36














                          The current filename is in the "% register, so you can insert it (while in insert mode) with <c-r>%; the full path can be inserted with <c-r>=expand("%:p"). You could make a macro of it if you use it often. For more, similar tricks, see :h expand and :h "=.






                          share|improve this answer























                          • Maybe I'm misreading that, but isn't <c-r> mapped as the redo command?
                            – jonyamo
                            Dec 4 '12 at 15:38








                          • 10




                            In insert mode, <c-r> specifies that the next character is a register.
                            – Kevin
                            Dec 4 '12 at 15:43










                          • Righto, thanks. Totally forgot to switch to insert mode...
                            – jonyamo
                            Dec 4 '12 at 15:49












                          • You can also do "%p in normal or visual modes.
                            – naught101
                            Aug 25 '17 at 2:36














                          36












                          36








                          36






                          The current filename is in the "% register, so you can insert it (while in insert mode) with <c-r>%; the full path can be inserted with <c-r>=expand("%:p"). You could make a macro of it if you use it often. For more, similar tricks, see :h expand and :h "=.






                          share|improve this answer














                          The current filename is in the "% register, so you can insert it (while in insert mode) with <c-r>%; the full path can be inserted with <c-r>=expand("%:p"). You could make a macro of it if you use it often. For more, similar tricks, see :h expand and :h "=.







                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Dec 4 '12 at 15:43

























                          answered Dec 4 '12 at 14:55









                          Kevin

                          27k106199




                          27k106199












                          • Maybe I'm misreading that, but isn't <c-r> mapped as the redo command?
                            – jonyamo
                            Dec 4 '12 at 15:38








                          • 10




                            In insert mode, <c-r> specifies that the next character is a register.
                            – Kevin
                            Dec 4 '12 at 15:43










                          • Righto, thanks. Totally forgot to switch to insert mode...
                            – jonyamo
                            Dec 4 '12 at 15:49












                          • You can also do "%p in normal or visual modes.
                            – naught101
                            Aug 25 '17 at 2:36


















                          • Maybe I'm misreading that, but isn't <c-r> mapped as the redo command?
                            – jonyamo
                            Dec 4 '12 at 15:38








                          • 10




                            In insert mode, <c-r> specifies that the next character is a register.
                            – Kevin
                            Dec 4 '12 at 15:43










                          • Righto, thanks. Totally forgot to switch to insert mode...
                            – jonyamo
                            Dec 4 '12 at 15:49












                          • You can also do "%p in normal or visual modes.
                            – naught101
                            Aug 25 '17 at 2:36
















                          Maybe I'm misreading that, but isn't <c-r> mapped as the redo command?
                          – jonyamo
                          Dec 4 '12 at 15:38






                          Maybe I'm misreading that, but isn't <c-r> mapped as the redo command?
                          – jonyamo
                          Dec 4 '12 at 15:38






                          10




                          10




                          In insert mode, <c-r> specifies that the next character is a register.
                          – Kevin
                          Dec 4 '12 at 15:43




                          In insert mode, <c-r> specifies that the next character is a register.
                          – Kevin
                          Dec 4 '12 at 15:43












                          Righto, thanks. Totally forgot to switch to insert mode...
                          – jonyamo
                          Dec 4 '12 at 15:49






                          Righto, thanks. Totally forgot to switch to insert mode...
                          – jonyamo
                          Dec 4 '12 at 15:49














                          You can also do "%p in normal or visual modes.
                          – naught101
                          Aug 25 '17 at 2:36




                          You can also do "%p in normal or visual modes.
                          – naught101
                          Aug 25 '17 at 2:36











                          13














                          As can be seen in :h registers, the "% register contains the current file name. The :pu[t] command inserts the content of a register into the text.



                          So, to insert the actual filename you can type either of these, in command mode:



                          :put %


                          or



                          "%p


                          To insert the filename with the full path, type



                          :put=expand('%:p')


                          in command mode.





                          More info:



                          :h pu[t]


                          By typing "rp you can paste the contents of register "r.






                          share|improve this answer



















                          • 2




                            The :put "%:p" doesn't seem to work for me. It seems to work only with expand(), like in: :put =expand('%:p'), which makes it not much less cumbersome than <c-r>=expand('%:p') unfortunately.
                            – akavel
                            Sep 16 '15 at 11:21










                          • @akavel You're correct, I'll update the answer. Thank you! ;) And, indeed, both are equally verbose.
                            – braunmagrin
                            Sep 29 '15 at 23:00


















                          13














                          As can be seen in :h registers, the "% register contains the current file name. The :pu[t] command inserts the content of a register into the text.



                          So, to insert the actual filename you can type either of these, in command mode:



                          :put %


                          or



                          "%p


                          To insert the filename with the full path, type



                          :put=expand('%:p')


                          in command mode.





                          More info:



                          :h pu[t]


                          By typing "rp you can paste the contents of register "r.






                          share|improve this answer



















                          • 2




                            The :put "%:p" doesn't seem to work for me. It seems to work only with expand(), like in: :put =expand('%:p'), which makes it not much less cumbersome than <c-r>=expand('%:p') unfortunately.
                            – akavel
                            Sep 16 '15 at 11:21










                          • @akavel You're correct, I'll update the answer. Thank you! ;) And, indeed, both are equally verbose.
                            – braunmagrin
                            Sep 29 '15 at 23:00
















                          13












                          13








                          13






                          As can be seen in :h registers, the "% register contains the current file name. The :pu[t] command inserts the content of a register into the text.



                          So, to insert the actual filename you can type either of these, in command mode:



                          :put %


                          or



                          "%p


                          To insert the filename with the full path, type



                          :put=expand('%:p')


                          in command mode.





                          More info:



                          :h pu[t]


                          By typing "rp you can paste the contents of register "r.






                          share|improve this answer














                          As can be seen in :h registers, the "% register contains the current file name. The :pu[t] command inserts the content of a register into the text.



                          So, to insert the actual filename you can type either of these, in command mode:



                          :put %


                          or



                          "%p


                          To insert the filename with the full path, type



                          :put=expand('%:p')


                          in command mode.





                          More info:



                          :h pu[t]


                          By typing "rp you can paste the contents of register "r.







                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Oct 18 '15 at 0:22









                          G-Man

                          12.9k93364




                          12.9k93364










                          answered Dec 4 '12 at 21:55









                          braunmagrin

                          23115




                          23115








                          • 2




                            The :put "%:p" doesn't seem to work for me. It seems to work only with expand(), like in: :put =expand('%:p'), which makes it not much less cumbersome than <c-r>=expand('%:p') unfortunately.
                            – akavel
                            Sep 16 '15 at 11:21










                          • @akavel You're correct, I'll update the answer. Thank you! ;) And, indeed, both are equally verbose.
                            – braunmagrin
                            Sep 29 '15 at 23:00
















                          • 2




                            The :put "%:p" doesn't seem to work for me. It seems to work only with expand(), like in: :put =expand('%:p'), which makes it not much less cumbersome than <c-r>=expand('%:p') unfortunately.
                            – akavel
                            Sep 16 '15 at 11:21










                          • @akavel You're correct, I'll update the answer. Thank you! ;) And, indeed, both are equally verbose.
                            – braunmagrin
                            Sep 29 '15 at 23:00










                          2




                          2




                          The :put "%:p" doesn't seem to work for me. It seems to work only with expand(), like in: :put =expand('%:p'), which makes it not much less cumbersome than <c-r>=expand('%:p') unfortunately.
                          – akavel
                          Sep 16 '15 at 11:21




                          The :put "%:p" doesn't seem to work for me. It seems to work only with expand(), like in: :put =expand('%:p'), which makes it not much less cumbersome than <c-r>=expand('%:p') unfortunately.
                          – akavel
                          Sep 16 '15 at 11:21












                          @akavel You're correct, I'll update the answer. Thank you! ;) And, indeed, both are equally verbose.
                          – braunmagrin
                          Sep 29 '15 at 23:00






                          @akavel You're correct, I'll update the answer. Thank you! ;) And, indeed, both are equally verbose.
                          – braunmagrin
                          Sep 29 '15 at 23:00













                          4














                          A simple way is to run:



                          !!echo %



                          • !! is replacing the current line with the result of the command following it.


                          • % is replaced by the name of the edited file in the command so this will effectively insert that name in the edited file.



                          The filename is the one you passed to the vi(m) command and might contain a relative or absolute path. Should you want to strip it and only retain the file name, run



                          !!basename %





                          share|improve this answer




























                            4














                            A simple way is to run:



                            !!echo %



                            • !! is replacing the current line with the result of the command following it.


                            • % is replaced by the name of the edited file in the command so this will effectively insert that name in the edited file.



                            The filename is the one you passed to the vi(m) command and might contain a relative or absolute path. Should you want to strip it and only retain the file name, run



                            !!basename %





                            share|improve this answer


























                              4












                              4








                              4






                              A simple way is to run:



                              !!echo %



                              • !! is replacing the current line with the result of the command following it.


                              • % is replaced by the name of the edited file in the command so this will effectively insert that name in the edited file.



                              The filename is the one you passed to the vi(m) command and might contain a relative or absolute path. Should you want to strip it and only retain the file name, run



                              !!basename %





                              share|improve this answer














                              A simple way is to run:



                              !!echo %



                              • !! is replacing the current line with the result of the command following it.


                              • % is replaced by the name of the edited file in the command so this will effectively insert that name in the edited file.



                              The filename is the one you passed to the vi(m) command and might contain a relative or absolute path. Should you want to strip it and only retain the file name, run



                              !!basename %






                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited Mar 30 '16 at 0:59









                              Community

                              1




                              1










                              answered Dec 4 '12 at 14:40









                              jlliagre

                              46.5k783132




                              46.5k783132























                                  1














                                  If you need to do this frequently, it might be useful to bind a key sequence to what you want, as per http://vim.wikia.com/wiki/Insert_current_filename




                                  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>



                                  The above all work directly in vim for the session, or you can put it in the .vimrc (where the leading colon on the line is optional).






                                  share|improve this answer




























                                    1














                                    If you need to do this frequently, it might be useful to bind a key sequence to what you want, as per http://vim.wikia.com/wiki/Insert_current_filename




                                    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>



                                    The above all work directly in vim for the session, or you can put it in the .vimrc (where the leading colon on the line is optional).






                                    share|improve this answer


























                                      1












                                      1








                                      1






                                      If you need to do this frequently, it might be useful to bind a key sequence to what you want, as per http://vim.wikia.com/wiki/Insert_current_filename




                                      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>



                                      The above all work directly in vim for the session, or you can put it in the .vimrc (where the leading colon on the line is optional).






                                      share|improve this answer














                                      If you need to do this frequently, it might be useful to bind a key sequence to what you want, as per http://vim.wikia.com/wiki/Insert_current_filename




                                      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>



                                      The above all work directly in vim for the session, or you can put it in the .vimrc (where the leading colon on the line is optional).







                                      share|improve this answer














                                      share|improve this answer



                                      share|improve this answer








                                      edited Oct 27 '17 at 21:33

























                                      answered Oct 20 '17 at 18:36









                                      Randall

                                      23919




                                      23919























                                          0














                                          I was googling a simplest way to insert current file name (without path or extension) to current position (no new line, no replace current line). So combining previous answers and this link, this is what I was looking for.



                                          In insert mode:



                                          <C-R>=expand("%:t:r")


                                          Then next time (with the command already in history):



                                          <C-R>=<UP>





                                          share|improve this answer


























                                            0














                                            I was googling a simplest way to insert current file name (without path or extension) to current position (no new line, no replace current line). So combining previous answers and this link, this is what I was looking for.



                                            In insert mode:



                                            <C-R>=expand("%:t:r")


                                            Then next time (with the command already in history):



                                            <C-R>=<UP>





                                            share|improve this answer
























                                              0












                                              0








                                              0






                                              I was googling a simplest way to insert current file name (without path or extension) to current position (no new line, no replace current line). So combining previous answers and this link, this is what I was looking for.



                                              In insert mode:



                                              <C-R>=expand("%:t:r")


                                              Then next time (with the command already in history):



                                              <C-R>=<UP>





                                              share|improve this answer












                                              I was googling a simplest way to insert current file name (without path or extension) to current position (no new line, no replace current line). So combining previous answers and this link, this is what I was looking for.



                                              In insert mode:



                                              <C-R>=expand("%:t:r")


                                              Then next time (with the command already in history):



                                              <C-R>=<UP>






                                              share|improve this answer












                                              share|improve this answer



                                              share|improve this answer










                                              answered Dec 19 '18 at 12:04









                                              kub1x

                                              1012




                                              1012






























                                                  draft saved

                                                  draft discarded




















































                                                  Thanks for contributing an answer to Unix & Linux Stack Exchange!


                                                  • Please be sure to answer the question. Provide details and share your research!

                                                  But avoid



                                                  • Asking for help, clarification, or responding to other answers.

                                                  • Making statements based on opinion; back them up with references or personal experience.


                                                  To learn more, see our tips on writing great answers.





                                                  Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                                  Please pay close attention to the following guidance:


                                                  • Please be sure to answer the question. Provide details and share your research!

                                                  But avoid



                                                  • Asking for help, clarification, or responding to other answers.

                                                  • Making statements based on opinion; back them up with references or personal experience.


                                                  To learn more, see our tips on writing great answers.




                                                  draft saved


                                                  draft discarded














                                                  StackExchange.ready(
                                                  function () {
                                                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f57555%2fhow-do-i-insert-the-current-filename-into-the-contents-in-vim%23new-answer', 'question_page');
                                                  }
                                                  );

                                                  Post as a guest















                                                  Required, but never shown





















































                                                  Required, but never shown














                                                  Required, but never shown












                                                  Required, but never shown







                                                  Required, but never shown

































                                                  Required, but never shown














                                                  Required, but never shown












                                                  Required, but never shown







                                                  Required, but never shown







                                                  Popular posts from this blog

                                                  Morgemoulin

                                                  Scott Moir

                                                  Souastre