Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Create a new file with Ctrl+Alt+N keyboard shortcut in Windows Explorer with AutoHotKey
; This is part of my AutoHotKey [1] script. When you are in Windows Explorer it
; allows you to press Ctrl+Alt+N and type a filename, and that file is created
; in the current directory and opened in the appropriate editor (usually
; [gVim](http://www.vim.org/) in my case, but it will use whatever program is
; associated with the file in Windows Explorer).
; This is much easier than the alternative that I have been using until now:
; Right click > New > Text file, delete default filename and extension (which
; isn't highlighted in Windows 7), type the filename, press enter twice.
; (Particularly for creating dot files like ".htaccess".)
; Credit goes to aubricus [2] who wrote most of this - I just added the
; 'IfWinActive' check and 'Run %UserInput%' at the end.
; [1]: http://www.autohotkey.com/
; [2]: https://gist.github.com/1148174
; Only run when Windows Explorer or Desktop is active
; Ctrl+Alt+N
#IfWinActive ahk_class CabinetWClass
^!n::
#IfWinActive ahk_class ExploreWClass
^!n::
#IfWinActive ahk_class Progman
^!n::
#IfWinActive ahk_class WorkerW
^!n::
; Get full path from open Explorer window
WinGetText, FullPath, A
; Clean up result
StringReplace, FullPath, FullPath, `r, , all
FullPath := RegExReplace(FullPath, "^.*`nAddress: ([^`n]+)`n.*$", "$1")
; Change working directory
SetWorkingDir, %FullPath%
; An error occurred with the SetWorkingDir directive
If ErrorLevel
Return
; Display input box for filename
InputBox, UserInput, New File (example: foo.txt), , , 400, 100
; User pressed cancel
If ErrorLevel
Return
; Create file
FileAppend, , %UserInput%
; Open the file in the appropriate editor
Run %UserInput%
Return
#IfWinActive
@poweredbyorganic

This comment has been minimized.

Copy link

poweredbyorganic commented Mar 10, 2012

Nice work. CabinetWClass didn't work on my version of ahk. If you have the same problem, try:

#IfWinActive ahk_class ExploreWClass
@syon

This comment has been minimized.

Copy link

syon commented Mar 27, 2014

Amazing! I did it.
In Japanese version of Windows, the (^Address: ) doesn't work. Then I change it (^.+?: ).

Thanks.

@Bastianowicz

This comment has been minimized.

Copy link

Bastianowicz commented Dec 18, 2014

I used this script quite a while but it hat its downsides and stopped working with win 8. I thought about another way of accomplishing that behaviour. Try this script:

#IfWinActive, ahk_class CabinetWClass ; only in Explorer-Windows
    ^!n::
        KeyWait Alt ; Wait for alt to be released, since it stops AppsKey from working
        Send {AppsKey}
        Send n
        Send t
        return
#IfWinActive
@romanitalian

This comment has been minimized.

Copy link

romanitalian commented Feb 3, 2015

On local computer to any folder:
save this script as file: create-new-file.ahk

@ultimate-real

This comment has been minimized.

Copy link

ultimate-real commented Mar 9, 2015

To create a folder if no extension is given, add

IfNotInString, UserInput, .
{
  FileCreateDir, %UserInput%
  Return
}
@davejamesmiller

This comment has been minimized.

Copy link
Owner Author

davejamesmiller commented Mar 25, 2015

@ultimate-real Thanks! Though you can already use Ctrl+Shift+N to create a new folder in Windows 7 (and 8 I think).

@richardsun29

This comment has been minimized.

Copy link

richardsun29 commented Apr 7, 2015

I edited this script to account for the ahk_class variation, as well as making it work when the Desktop is active.
https://gist.github.com/richardsun29/a9a5a402c85df7f50283

@clever-techie

This comment has been minimized.

Copy link

clever-techie commented Sep 10, 2015

Doesn't work under Windows 10

@gregoryderner

This comment has been minimized.

Copy link

gregoryderner commented Oct 21, 2015

Win 8.1 Change this
; Get first item
FullPath = %PathArray1% <--

for
; Get first item
FullPath = %PathArray11%

@davejamesmiller

This comment has been minimized.

Copy link
Owner Author

davejamesmiller commented Oct 21, 2015

@richardsun29: Thanks, I've updated the script above.

@gregoryderner: For me it was %PathArray9% that worked, which makes me think it depends on the current Explorer settings. So I've changed it to use this regular expression instead:

FullPath := RegExReplace(FullPath, "^.*`nAddress: ([^`n]+)`n.*$", "$1")
@vlkvr

This comment has been minimized.

Copy link

vlkvr commented Apr 4, 2016

Doesn't work under Windows 10.
I changed this:
FullPath = %PathArray1%
for this:
FullPath = %PathArray9%

After that, the input field for the file name is displayed, but the file is not created.

@TheBeastOfCaerbannog

This comment has been minimized.

Copy link

TheBeastOfCaerbannog commented Dec 18, 2016

I updated script to find correct path in PathArray to make it work on Windows 7-10. I also changed hotkey to Alt+N but this could be easily reverted.
https://gist.github.com/TheBeastOfCaerbannog/4bc8c8f4965a9f53318aca9b9d26fe53

@cherepashkinVI

This comment has been minimized.

Copy link

cherepashkinVI commented Dec 5, 2018

Thank you for the script. Do you know, how to avoid writing each time file extension ".txt"? I just want to create only txt files with this shortcut and I don't want to point an extension. Also because I need to switch my keyboard sometimes, and it is difficult to write ".txt"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.