7
$\begingroup$

Is there any way to highlight the whole selected cell? some times if I want to delete a certain cell and if the code is long and cells are without output, it is hard to know which cell that you are selecting. For example, in this simple file, I want to select a certain cell as seen:

enter image description here

If I want to select the second cell, I may make mistake and select the above or below cell (especially if the code is long).

is there any way to highlight the whole cell once I selected it from the right?

$\endgroup$
  • 3
    $\begingroup$ This is not an answer to your question, but you may find it helpful to know that you can press Ctrl + . repeatedly to make progressively larger selections. $\endgroup$ – mfvonh Jun 22 '14 at 21:55
  • 2
    $\begingroup$ If you want something "nice", add some keyevents to KeyEventsTranslation.tr file to change/revert background colors/// $\endgroup$ – ciao Jun 22 '14 at 22:30
  • $\begingroup$ @mfvonh thanks for this nice tip:). I am using what you have said but I thought it would be nice if I can get the cell highlighted when selected. $\endgroup$ – Algohi Jun 22 '14 at 22:44
  • $\begingroup$ @rashed, I will try your suggestion. thanks $\endgroup$ – Algohi Jun 22 '14 at 22:46
  • $\begingroup$ do you know guys how to suggest some features or ideas to Wolfram people? $\endgroup$ – Algohi Jun 22 '14 at 22:54
9
$\begingroup$

I agree that it can be hard to tell that you have selected the correct cell bracket and that having a global option to highlight the whole cell when it is selected would be good. But I don't know how to do that. What I have done to alleviate the problem is to set Option Inspector > Global Preferences > Cell Options > Display Options > CellBracketOptions > "HoverColor" to RGBColor[1, 0, 0], which is red. With this setting, when I hover the mouse cursor over a cell bracket, it turns red, confirming that, if I click, this cell with the red bracket will be the one selected. It's nowhere as good as full-cell highlighting would be, but it does help.

hover_color

$\endgroup$
  • $\begingroup$ Nice one - had not noticed this option before. +1 $\endgroup$ – ciao Jun 22 '14 at 23:08
  • 1
    $\begingroup$ good god this is amazing. MMA's design is incredible in some ways and in others it just makes me go WTF. $\endgroup$ – seismatica Jun 22 '14 at 23:47
  • 2
    $\begingroup$ @seismatica. Welcome to the club:) $\endgroup$ – m_goldberg Jun 22 '14 at 23:52
  • $\begingroup$ @m_goldberg - Thank you very much $\endgroup$ – eldo Jun 23 '14 at 0:22
  • 1
    $\begingroup$ @Algohi. Do you have the same mixed emotions as seismtica when you use Mathematica? If you do, I am happy to extend my welcome to you, too. $\endgroup$ – m_goldberg Jun 23 '14 at 0:59
4
$\begingroup$

Initial approach

The obvious thing to try is to give the Background a Dynamic based on SelectedCells / EvaluationCell, but this didn't work. It seems that SelectedCells doesn't get triggered when the mouse clicks the cell brackets.

After thinking about this for a while, I came up with the following idea. Give the notebook a NotebookEventActions option that counts mouse clicks. Then, trigger the background dynamic object on changes in mouse clicks. This did work. Here are more details:

NotebookEventActions

SetOptions[
    InputNotebook[],
    NotebookEventActions -> {
        "MouseClicked" :> If[IntegerQ@i, i++, i=1],
        PassEventsDown->True
    }
]

Whenever the mouse is clicked in the notebook, the variable i is incremented (or initialized to 1 if it doesn't have a value). Then, the PassEventsDown is option is set to True so that you can actually click in the notebook. It's not really important that i is incremented, the important thing is that it changes for every mouse click. However, it doesn't hurt to have a mouse click counter.

Stylesheet

Next, we add a Dynamic background to "Input" cells. The relevant style data is:

Cell[StyleData["Input"],
    CellFrameMargins->0,
    Background->Dynamic @ If[
        And[
            IntegerQ[i],
            MemberQ[SelectedCells[InputNotebook[]], EvaluationCell[]],
            MatchQ[
                "CursorPosition" /. Developer`CellInformation[EvaluationCell[]],
                "CellBracket"
            ]
        ],
        LightBlue,
        None    
    ]
]

Here, I include IntegerQ[i] so that the body of the dynamic is triggered each time i changes, i.e., every time a mouse click occurs in the notebook. Then, determine whether the cell is included in SelectedCells. If it is, make sure the cursor is in the cell bracket (I think highlighting the cell while editing would be highly distracting). If the cell is selected and the cursor is in the cell bracket, than set the background to LightBlue, otherwise set it to None. The CellFrameMargins option is included so that adding a background color doesn't change the size of the cell.

It is also possible to use Developer`CellInformation[InputNotebook[]], as this will return cell information for all selected cells, including cursor position and the ExpressionUUID. Just check that the cursor is in the bracket, and the cell's ExpressionUUID is included in the list of cell information.

Package

It would be good if we put the variable i into a private context. Also, it would be nice to have the stylesheet and notebook modified appropriately when a package is loaded. So, here is the above code put into a package:

BeginPackage["HighlightSelectedCells`"];

Begin["`Private`"];

SetOptions[
    InputNotebook[],
    CellEventActions -> {
        "MouseClicked" :> If[IntegerQ[i], i++, i=0],
        PassEventsDown->True
    }
]

SetOptions[InputNotebook[],
    StyleDefinitions -> Notebook[
        {
        Cell[StyleData[StyleDefinitions->"Default.nb"]],
        Cell[StyleData["Input"],
            CellFrameMargins->0,
            Background->Dynamic @ If[
                And[
                    IntegerQ[i],
                    MemberQ[SelectedCells[InputNotebook[]], EvaluationCell[]],
                    MatchQ[
                        "CursorPosition" /. Developer`CellInformation[EvaluationCell[]],
                        "CellBracket"
                    ]
                ],
                LightBlue,
                None    
            ]
        ]
        },
        StyleDefinitions->"PrivateStylesheetFormatting.nb"
    ]
]

End[];

EndPackage[];

Put the above code into the file "HighlightSelectedCells.m" in a location on your path. Then:

<<HighlightSelectedCells`

will load the package. Here is an animated gif showing how it works:

enter image description here

$\endgroup$
3
$\begingroup$

I'm not sure if this solves your problem but if you want to select a particular cell (without mistakenly selecting others), just put your horizontal bar--whatever it's called--above or below the cell, whichever position that's more convenient to you (for example, for an output cell, it makes sense to put the bar below it instead of navigating up to the narrow strip between the input and output cell).

Then, you can press Shift+Up or Shift+Down to select the cell above/below the bar. The nice thing about this is it will always select only one cell even if this cell belongs to a group or is joined by a hidden cell. To select other cells, just continue pressing Up and Down while holding Shift.

$\endgroup$
  • $\begingroup$ Another neat thing that I found by accident just now is that once you select a cell (either by clicking on its right-side bracket or by using the Shift method in my answer), you can just press Up or Down (without holding Shift) to navigate to the correct cell in the event that you found you'd just selected the wrong cell. $\endgroup$ – seismatica Jul 11 '14 at 19:54
1
$\begingroup$

You want to delete a certain cell, but you are unsure whether you selected the right bracket:

Click the bracket, select Style and then Title. If you hit the wrong bracket, select Input.

Another way (if you deleted the wrong cell) is to immediately press Ctrl + z

$\endgroup$
  • $\begingroup$ I can also use Alt+4 (or any number from 1 to 7) to quickly change the style of the selected cell. thanks for the suggestion. $\endgroup$ – Algohi Jun 22 '14 at 22:50
  • $\begingroup$ @Algohi - very nice ! Alt+1 to highlight and Alt+9 to go back to InputForm :) $\endgroup$ – eldo Jun 22 '14 at 22:59

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.