Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|improve this question
add comment

5 Answers

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() 
share|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
add comment

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

share|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
add comment

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.

share|improve this answer
1  
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
add comment

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.

share|improve this answer
add comment
  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.
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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