In Nano 6.4, you can do it like this (or a similar way you could figure out yourself if you don't like my particular method):
Open your ~/.nanorc file with Nano (not some other editor).
Type bind F7 "|reg "" """ main
Then put the cursor between the "|
that you typed. Then press Alt+v and then press Ctrl+t. It should output some characters. Then go to just before the last double quote and press Alt+v; then press the left arrow key; repeat that (with Alt+v and then the left arrow key) three more times (It should output some characters; so, it should end up looking something like bind F7 "^T|reg "" ""^[[D^[[D^[[D^[[D" main
.
Save and exit the .nanorc file.
Then make this Python script (call it reg
; you don't need to add .py
to the end, unless you want to; make it executable with such as chmod +x reg
) and put it in your path (if you're on Ubuntu, if you make a ~/bin
directory (so you can put it at ~/bin/reg
), it'll be in your path ever after once you restart your computer (otherwise, you can set your desired path in your ~/.bashrc file); anyway, here's the code for the reg
file:
#!/usr/bin/env python3
import re, sys
text=sys.stdin.read()
reg=""
if len(sys.argv)==3:
#Usage: ctrl+t; |reg.py "my regex" "my replacement" (don't forget the quotes, or some such)
reg=sys.argv[1]
rep=sys.argv[2]
result=re.sub(reg, rep, text)
elif len(sys.argv)==2:
reg=sys.argv[1]
result="\n".join(re.findall(reg, text))
else:
result=re.sub(r"", r"", text) #Edit the regular expression here.
print(result)
Close Nano if it's open.
Open Nano.
Now, every time you press F7, it'll launch the execute command menu/prompt with the default text |reg "" ""
, with your cursor in between the first set of double quotes. Put your search text in the first set of quotes, and your replace text in the second set of quotes. Press enter when you wish to do the replacement. This allows you to do the regular expressions you can do in Python 3.x, which are quite nice (and allow new lines, groups, and other stuff). It'll search and replace the whole file, unless you have something selected (then it'll search and replace the selection, and for some reason it always puts a new line after the selection after a replace).
If you just do one set of quotes with a regular expression, it'll replace your document or selection with each of the matches (you can undo it with Alt+u).
If you don't have any quotes, but just do |reg
, then it'll do the default regular expression that you have saved in the Python program named reg that you put in your path.
If you don't have Nano 6.4, and you're using Ubuntu, or such, you can compile it fairly easily. Add the source code repositories for your updater, if you haven't already. Then, do sudo apt build-dep nano
. Then download the latest source code from nano-editor.org, and follow the instructions in the source code to compile.
For information about rebinding keys in order to execute external commands, see the Nanorc documentation, particularly this quote:
bind key "string" menu
Makes the given key produce the given string in the given menu (or in
all menus where the key exists when all is used). The string can
consist of text or commands or a mix of them. (To enter a command into
the string, precede its keystroke with M−V.)