Is it possible to reverse a single line in Notepad++?


Hello world

Would change to

dlrow olleH

Should I install a plugin for such functionality?

UPDATE: I'd like to keep the clipboard in its original state. So a [copy]-[external transform]-[paste] is not suitable.

In addition, I'd like to stay in a disconnected desktop environment, so any online service solution is not suitable.


You can do an iterated search and replace to reverse the string, after you have marked the beginning of the string with a special character (I use the percent sign % in the example)

search: (%)(.*)(.)

replace: \3\1\2

enable "regular expressions" in search and replace.

repeat the operation until the string is reversed.

| improve this answer | |
  • 2
    This might be exhausting for a long line, but that's quite a nice solution since it's still "inside" NPP's environment. – Ron Klein Sep 4 '10 at 11:12

UPDATE: I have added a Python-3 "reverse the clipboard text" script.
This Python-3 script caters for Unicode text; ie. all text...

# original # Ĥĕłłō ŵōŗłđ in Unicode  
# reversed # edocinU ni đłŗōŵ ōłłĕĤ  

Because no integrated solution has been presented (yet), I'll mention a Unix-tools work-around.

Download the utilities mentioned in the source (below), and put them into a PATH'd folder.
Pop the code (below) into a .cmd file and call it anything you like. (it to, must of course be in a PATH'd folder).
Create a Windows shortcut to the .cmd (put it somewhere in your StartMenu, and set it to run minimized)... and assighen a Shortcut-Hotkey to it.

You can then just copy the text of your choice to the clipboard and press your Hotkey... Done! .. the reversed text replaces the selected text.

If you prefer you can set up a NotePad++ macro to select and copy an entire line.. (take note of the new-line char(s) at the end of the text.

Also, as mentioned in the source notes. This workd for single-bytes character sets..

sed.exe may be able to handle Unicode, but I haven't quite worked that one out yet... If you happen to know how to do this, please post the info, either here or perhaps in my posting of a specific qustion abut this issue: can-gnu-sed-for-windows-handle-unicode

This is the sed.exe version:

@echo off
  :: FUNCTION: Reverse the text in the clipboard. ::
  ::    8-bit characters only (ANSI).             ::
  :: It removes all \r and \n characters, because ::
  :: because sed.exe adds a trailing line-feed    ::
  :: Four `NIX utilities are used.                ::
  ::    2 gclip.exe  (GNU)                        ::
  ::    1 pclip.exe  (GNU)                        ::
  ::    3 gsed.exe   (GNU)                        ::
  ::    4 tr.exe (is `NIX, but maybe not GNU)     ::
  >   "%temp%\%n0.sed" echo /\n/!G
  >>  "%temp%\%n0.sed" echo s/\(.\)\(.*\n\)/^&\2\1/
  >>  "%temp%\%n0.sed" echo //D
  >>  "%temp%\%n0.sed" echo s/.//
  pclip.exe | sed.exe -f "%temp%\%n0.sed" | tr.exe -d "\r\n" | gclip.exe  
  del "%temp%\%n0.sed"
goto :eof  

Here is the Python-3 version:

## FUNCTION: Reverse the text in the clipboard. ##
##           The text is handled as unicode.    ##
## Using Python-3.1.2                           ##
##  with Python-Win32-extensions for Python-3.1 ##
import win32clipboard as w
| improve this answer | |
  • Works great with python 2.5 on windows as well. Sweet! – Dennis Aug 5 '10 at 16:03
  • This changes the state of the clipboard. I'd like to avoid this. However, if it works, I might accept that. – Ron Klein Sep 4 '10 at 11:09

If you don't need to do it that often you could copy the line into this online tool to do the reversal.

| improve this answer | |
  • Nice idea, but the point is to keep working inside the Notepad++ environment, and not to go to any external resource (a browser). – Ron Klein Sep 4 '10 at 11:10
  • Tool is offline unfortunately – Antonio Sep 19 '18 at 21:28

I realise this uses another tool, but you can reverse text in Excel and then copy it back. Create a spreadsheet and add this macro:

Sub ReverseText()
    Cells(Selection.Row, Selection.Column + 1) = StrReverse(Selection.Value)
End Sub

If you intend to use this feature regularly, assign the macro to a button on the toolbar for easy access. Copy some text into any cell. Ensure the cell is selected and run the macro. Copy the generated text back to Notepad++.

Surprising that this simple feature is not available in Notepad++ or through a plugin like TextFX.

| improve this answer | |

The BinHex plugin version 2.0 of Notepad++ has a String reverse function implemented.

You can mark the string that should be reversed and hit Ctrl+R.

| improve this answer | |

An extension of gor's idea if your string is less then ten characters:
Make a macro of this.
Highlight the desired area in N++





replace in selection: checked

N++ only supports ten groups, so that's all you get, but at least it does ten letters at once.

Off topic, IE's regex engine is different than the accepted in regards to these sort of regex's, so this might be doable in IE without the iterations.

| improve this answer | |
  1. Select the text to reverse.
  2. Break it into multiple lines by using the Replace window with In selection and Regular expression selected to Replace All (.) with \n\1
  3. Select the text again (for some reason, the last character is left out of the selection after step 2).
  4. Use the answer to Reverse line order in Notepad++ to reverse the lines.
  5. Recombine the lines by using the Replace window with In selection and Extended selected to Replace All \n with an empty string.
  6. You may need to remove extraneous newlines.
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.