Is there a way to make TeXShop insert an arbitrary number of spaces when I press the tab key instead of inserting a tab? This functionality is in TeXnicle and I really like it, but TeXShop is my favorite editor.

A related question is how to easily indent multiple lines of code with spaces (not tabs) in TeXShop. When I think about it, I don't even know how to indent multiple lines of code with tabs!

The setting in TeXnicle is shown in the following picture:

TeXnicle Functionality: Replace Tab with Spaces


I have put together some code which will add a single space (or more) in front of an arbitary number of selected lines in TeXShop. I need some help here, however. After I run the script, TeXShop deselects the original selection. Why is this bad? You cannot quickly rerun the script on the same selection. It wouldn't be efficient to make a key binding for this script.

--repeat with each line in theselection
--do shell script "sed 's/^/ /'   --THIS THING JUST ADDS A SPACE IN FRONT OF EACH LINE OF INPUT

tell application "TeXShop"
    --get the front document
    set thisDoc to the front document
    set mySel to selection of thisDoc
    set selContent to content of selection of thisDoc
    set outcome to do shell script "echo " & (quoted form of selContent) & "|sed 's/^/ /' "
    set content of selection of thisDoc to outcome
end tell


Selection Before Script



Is there a way to fix this script so that it leaves the selection selected after running?


Here is a script that inserts a single space in front of each selected line, and leaves the lines selected.

It works by saving the location of the selection, adding a space to each line, and then using reselecting the original selection.

tell application "TeXShop"
    --get the front document
    set thisDoc to the front document
    --get the current selection text, offset, and length
    set selContent to content of selection of thisDoc
    set selOffset to offset of selection of thisDoc
    set selLength to length of selection of thisDoc
end tell

set outcome to ""
set spaceAdded to 0
-- add a space to each line, except for blank lines
repeat with oneLine in (paragraphs of selContent)
    if length of oneLine is equal to 0 then
        set outcome to outcome & return
        set outcome to outcome & " " & oneLine & return
        set spaceAdded to spaceAdded + 1
    end if
end repeat

tell application "TeXShop"
        -- this will avoid the error caused if there is no text selected
        set content of selection of thisDoc to (text 1 thru -2 of outcome)
        set offset of selection of thisDoc to selOffset
        set length of selection of thisDoc to selLength + spaceAdded
    end try
end tell
  • Confirmed, this script is just what I asked for. I would also like the ability to remove spaces in the same way, just in case I misalign a section of code. I will have to analyze your script and see about reverse engineering this to accomplish that as well. Thanks for the script! I am sure this will help many TeXShop users. – Jonathan Komar May 12 '12 at 21:08

What you're talking about are often called soft tabs, and I'd like them too in TeXShop. But so far I haven't found them.

The indent/unindent actions are under the Source menu in TeXShop's menubar. They insert hard tabs. They insert soft tabs (I'm not sure when this change was made). They don't have any keys bound to them by default, but you can use the System Preferences to create some as in this Lifehacker article.

I had thought about creating a TeXShop macro to insert a soft tab, and binding it to the Tab key, but that's more problematic than it sounds. The tab key is used to navigate any dialog box. Perhaps for this reason it's not possible to bind the tab key using TeXShop's macro editor editor (or the "Keyboard" System Preferences panel).

What you really need to do is re-bind the tab key only in the document's editing window. I don't know how to do that.

  • Thanks Matthew. I was really hoping for another answer. I will check out the alternative. Perhaps I will write an AppleScript which does something like "indend selection by 5 spaces (or an arbitrary number of spaces) that can be quickly invoked by a hotkey using Spark or something similar. – Jonathan Komar May 8 '12 at 15:42
  • 1
    @macmadness86: TeXShop has a nice macro editor within it so you could write an AppleScript and bind it to a key combo. I played around with that but got distracted by what I was actually supposed to be doing at the time. The thing I noticed was that it seems impossible to bind to a key combo that includes the tab key. – Matthew Leingang May 10 '12 at 11:04
  • @macmadNess86: also, TexShop is open source so you can try to patch it. Or email the maintainer (who's very nice) about the possibility of implementing it. – Matthew Leingang May 10 '12 at 11:06

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.