17

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

Example:

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.

7 Answers 7

9

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.

1
  • 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, 2010 at 11:12
3

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

3
  • 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, 2010 at 11:10
  • Tool is offline unfortunately
    – Antonio
    Sep 19, 2018 at 21:28
  • Tool is back online at time of writing. Works nicely.
    – AlainD
    Feb 9 at 8:54
3

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
w.OpenClipboard() 
## CF_UNICODETEXT == 13
s=w.GetClipboardData(13)
w.SetClipboardData(13,s[::-1]) 
w.CloseClipboard() 
2
  • Works great with python 2.5 on windows as well. Sweet!
    – Dennis
    Aug 5, 2010 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, 2010 at 11:09
2

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.

1

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.

0

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++

search:

(.?)(.?)(.?)(.?)(.?)(.?)(.?)(.?)(.?)

replace:

\9\8\7\6\5\4\3\2\1

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.

0
  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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .