# Wrapping selected text [edit]

This Help topic describes how to use a short Eclipse Monkey script to wrap an area of selected text.

## Introduction

Although the Editors in Aptana do not currently have a "Word Wrap" feature, you can use an Eclipse Monkey script to wrap a long line of text by adding new lines. The example script below will add a menu item named "Wrap Selection" to your scripts menu; however, you can change the metadata to remove this option and/or add a hotkey shortcut to the script. (See Adding metadata to an Eclipse Monkey script for more information.)

## Instructions

1. Create a new, empty Eclipse Monkey script. See Creating a new Eclipse Monkey script for instructions.

   Name the file something related to the actual snippet that you want to execute (e.g. execute_bold.js).

2. Copy and paste the JavaScript below into your new file:

       /*
        * Menu: 00_MyScripts > Wrap Selection
        * Kudos: Paul Colton (Aptana, Inc.)
        * License: EPL 1.0
        * DOM: http://download.eclipse.org/technology/dash/update/org.eclipse.eclipsemonkey.lang.javascript
        */
       function main(){
           var sourceEditor = editors.activeEditor;
           var defaultWidth = "60"; // default value to place in prompt
           // make sure we have an editor
           if (sourceEditor === undefined) {
               valid = false;
               showError("No active editor");
           }
           else {
               var range = sourceEditor.selectionRange;
               var offset = range.startingOffset;
               var deleteLength = range.endingOffset - range.startingOffset;
               var source = sourceEditor.source;
               var selection = source.substring(range.startingOffset, range.endingOffset);
               var wrapWidth = prompt("Column to wrap text at: ", defaultWidth);

               if (wrapWidth != undefined) {
                   var currentLen = 0;
                   var newSelection = "";
                   var newWidth = parseInt(wrapWidth);
                   var parts = selection.split(' ');
                   for (var i = 0; i < parts.length; i++) {
                       if (currentLen + parts[i].length > newWidth) {
                           newSelection += "\n";
                           currentLen = 0;
                       }

                       else {
                           currentLen += parts[i].length;
                       }

                       newSelection += parts[i] + " ";
                   }

                   // apply edit and reveal in editor
                   sourceEditor.applyEdit(offset, deleteLength, newSelection);
                   sourceEditor.selectAndReveal(offset, newSelection.length);
               }
           }
       }

3. In the Scripts View, click the Refresh button to refresh your scripting environment.

You should now be able to execute this script using the menu item in the sample, or any other metadata that you added on your own.

## Related Topics

- About Aptana Snippets
- Editing with Snippets
- Adding a new Snippet
- Editing a Snippet
- Scripting with Eclipse Monkey
- Adding metadata to an Eclipse Monkey script
- Assigning a keyboard shortcut to a snippet

[[Category:AptanaHelp==

Retrieved from "http://aptana.com/docs/index.php/Wrapping_selected_text"