Community
    • Login

    Start new file in language>python with shortcut while standardlanguage remains NONE, TXT

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    8 Posts 4 Posters 252 Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Robert JablkoR
      Robert Jablko
      last edited by Robert Jablko

      Hi everybody. I need help starting a new file in language>python with shortcut while standard language remains NONE, TXT.

      Why
      I use NPP as my standard Editor for Text *.TXT and my standard Editor for Phython *.PY, both on a daily basis. The standard is TXT. So when I start a new file Ctrl+N, a TXT-file is created. That is nice, but when I want to code in Python, I have to start a new file and change the language, that amounts to Ctrl+N, Alt+P, P, UP, Enter. I want a more efficient way.

      I don’t think I am the first person to have that wish, but I haven’t found a solution yet.

      What have I tried

      • I have tried to replicate the steps with a macro, but that does not record the change of language.
      • I have tried finding the answer both with google and here in the community and came up short looking for opening new file, language, python, npp in variations.

      Results:

      • In the community I found a lot of things that deals with JSON Files. I didn’t understand.
      • In Google the closest thing I have found was this link, but this remains a txt-file:
        • https://superuser.com/questions/620950/notepad-new-file-names

      Can anybody please help.

      Cheers, Robert

      Alan KilbornA PeterJonesP 2 Replies Last reply Reply Quote 0
      • Alan KilbornA
        Alan Kilborn @Robert Jablko
        last edited by

        @Robert-Jablko

        The absolute best way is to simply save your new tab as a file in the file system as something ending in .py.

        1 Reply Last reply Reply Quote 0
        • PeterJonesP
          PeterJones @Robert Jablko
          last edited by PeterJones

          @Robert-Jablko said in Start new file in language>python with shortcut while standardlanguage remains NONE, TXT:

          I have tried to replicate the steps with a macro, but that does not record the change of language.

          The Language commands aren’t macro-recordable, but they do turn out to be macro-playable, so you cannot record a macro that will do it, but after you record a macro, you can edit the %AppData%\Notepad++\shortcuts.xml and restart Notepad++ and from then on, the macro will include that command.

          I use the following macro to create a new file and change its language to Perl:

                  <Macro name="NewPerl" Ctrl="no" Alt="no" Shift="no" Key="0">
                      <Action type="2" message="0" wParam="41001" lParam="0" sParam="" />
                      <Action type="2" message="0" wParam="46013" lParam="0" sParam="" />
                  </Macro>
          

          For Python, the second line would use 46012 instead of 46013, based on the math inside menuCmdID.h. You can either manually set the keyboard shortcut when you edit the XML, or after your Notepad++ restart, you can just use Shortcut Mapper to change the shortcut for that macro.

          (More on the meaning of the XML in Macro commands can be found here in the User Manual)


          Last year, I gave this answer, which includes a SaveAs after setting the type. I would highly recommend you add the SaveAs to the end of your Macro, as well.

          Robert JablkoR 1 Reply Last reply Reply Quote 3
          • Robert JablkoR
            Robert Jablko @PeterJones
            last edited by Robert Jablko

            @PeterJones @Alan-Kilborn

            Thank you for the quick answers. The macro works like a charm. I love it!!! And learned so much, yet again.

            I will have two macros.

            1. The first macro opens a new file with python language. I actually don’t always need to save it. A lot of times I just quickly want to doodle around, insert code I have found on the web, change a part in my project, where there is too much going on for me to focus and I need a clean environment. Last, but not least I want to be able to use my extensive QuickText library that is not available in TXT.

               <Macro name="Open New Python" Ctrl="no" Alt="no" Shift="no" Key="0"> <!-- My first Macro by hand -->
                   <Action type="2" message="0" wParam="41001" lParam="0" sParam="" /> <!-- Start New File -->
                   <Action type="2" message="0" wParam="46012" lParam="0" sParam="" /> <!-- Change Language to Python -->
               </Macro>
              
            2. The second macro opens a new file with python language and opens the window to save as.

               <Macro name="Open new Python and save" Ctrl="no" Alt="no" Shift="no" Key="0"> 
                   <Action type="0" message="2172" wParam="0" lParam="0" sParam="COMMENT: 41001 = File|New" />
                   <Action type="2" message="0" wParam="41001" lParam="0" sParam="" /> 
                   <Action type="0" message="2172" wParam="0" lParam="0" sParam="COMMENT: 46012 = Language|Python" />
                   <Action type="2" message="0" wParam="46012" lParam="0" sParam="" /> 
                   <Action type="0" message="2172" wParam="0" lParam="0" sParam="COMMENT: 41008 = Save|As" />
                   <Action type="2" message="0" wParam="41008" lParam="0" sParam="" />
               </Macro>
              

            The following explanation is obviously NOT for you two, but for future me and maybe other newbies reading this post and trying to recreate it. The first question I had, was how to edit a macro by hand. There is no obvious way in npp that I have found. Why I did is to find the shortcuts.xml in my npp-folder, open it in npp and then insert the macro-snippts between <Macros> </Macros>. Then I had to restart npp. In my case it looks like this:

            </InternalCommands>
            <Macros>
                <Macro name="Trim Trailing Space and Save" Ctrl="no" Alt="yes" Shift="yes" Key="83">
                    <Action type="2" message="0" wParam="42024" lParam="0" sParam="" />
                    <Action type="2" message="0" wParam="41006" lParam="0" sParam="" />
                </Macro>
                <Macro name="Open new Python" Ctrl="no" Alt="no" Shift="no" Key="0"> <!-- My first Macro by hand -->
                    <Action type="2" message="0" wParam="41001" lParam="0" sParam="" /> <!-- Start New File -->
                    <Action type="2" message="0" wParam="46012" lParam="0" sParam="" /> <!-- Change Language to Python -->
                </Macro>
                <Macro name="Open new Python and save" Ctrl="no" Alt="no" Shift="no" Key="0"> 
            	<Action type="0" message="2172" wParam="0" lParam="0" sParam="COMMENT: 41001 = File|New" />
                    <Action type="2" message="0" wParam="41001" lParam="0" sParam="" /> 
                    <Action type="0" message="2172" wParam="0" lParam="0" sParam="COMMENT: 46012 = Language|Python" />
                    <Action type="2" message="0" wParam="46012" lParam="0" sParam="" /> 
                    <Action type="0" message="2172" wParam="0" lParam="0" sParam="COMMENT: 41008 = Save|As" />
                    <Action type="2" message="0" wParam="41008" lParam="0" sParam="" />
                </Macro>
            </Macros>
            <UserDefinedCommands>
            

            That is my first time using XML by the way, but that is why I love npp.

            Alan KilbornA PeterJonesP 2 Replies Last reply Reply Quote 3
            • Alan KilbornA
              Alan Kilborn @Robert Jablko
              last edited by

              @Robert-Jablko said in Start new file in language>python with shortcut while standardlanguage remains NONE, TXT:

              The first question I had, was how to edit a macro by hand. There is no obvious way in npp that I have found. Why I did is to find the shortcuts.xml in my npp-folder, open it in npp and then insert the macro-snippts between <Macros> </Macros>. Then I had to restart npp.

              See HERE.

              1 Reply Last reply Reply Quote 2
              • PeterJonesP
                PeterJones @Robert Jablko
                last edited by

                @Robert-Jablko said in Start new file in language>python with shortcut while standardlanguage remains NONE, TXT:

                <!-- My first Macro by hand -->

                Just so you know, the next time you record a macro or change a keyboard shortcut, all the <!-- ... --> comments will disappear (Notepad++ unfortuntely doesn’t preserve XML comments in the macros or other shortcuts.xml )

                Robert JablkoR 1 Reply Last reply Reply Quote 2
                • Robert JablkoR
                  Robert Jablko @PeterJones
                  last edited by Robert Jablko

                  @PeterJones

                  Thank you.

                  Actually, that happened today. I created a python-script that closes all open python idle windows, which I hate to do by hand, there are just too many and all of same at different places. Now I don’t have to do that again. I start the script via NppExec and use a shortcut. After that my comments were gone. I use your format now to store comments.

                  mkupperM 1 Reply Last reply Reply Quote 2
                  • mkupperM
                    mkupper @Robert Jablko
                    last edited by

                    @Robert-Jablko, A while back @PeterJones posted this which shows a way to have comments in a macro.

                    1 Reply Last reply Reply Quote 1
                    • First post
                      Last post
                    The Community of users of the Notepad++ text editor.
                    Powered by NodeBB | Contributors