Just like deleting a line with ctrl+d or something else in normal coding, is there some shortcut to delete a whole cell?
Asked
Active
Viewed 376 times
2
Αλέξανδρος Ζεγγ
- 9,783
- 3
- 20
- 41
narip
- 381
- 1
- 9
1 Answers
4
The command below will temporarily put a 'Delete cell' action on the Insert menu which can be activated with ctrl+k. It only persists for the current session.
If your cursor is initially positioned between cells activation will delete the cell below.
FrontEndExecute[FrontEnd`AddMenuCommands[
"DuplicatePreviousOutput", {Delimiter,
MenuItem["Delete cell", FrontEnd`KernelExecute[
Module[{nb},
nb = SelectedNotebook[];
SelectionMove[nb, Previous, Cell];
SelectionMove[nb, Next, Cell];
NotebookDelete[nb];
SelectionMove[nb, Next, Cell]
]],
MenuKey["k", Modifiers -> {"Control"}],
System`MenuEvaluator -> Automatic]}]]
To make it a permanent menu item add the following to MenuSetup.tr. (Make a backup first though.)
Item["Delete cell", KernelExecute[
Module[{nb},
nb = SelectedNotebook[];
SelectionMove[nb, Previous, Cell];
SelectionMove[nb, Next, Cell];
NotebookDelete[nb];
SelectionMove[nb, Next, Cell]
]],
MenuKey["k", Modifiers -> {"Control"}],
MenuEvaluator -> Automatic]
I can see myself adopting this useful function.
Chris Degnen
- 30,927
- 2
- 54
- 108
-
Thank you for your answer, I still have two points unget. First, I don't know the exact place of MenuSetup.tr to add, there are too many
Menu["..."]inside, and nested. Second, if I use the first command, should I also add it into MenuSetup.tr? – narip Apr 09 '22 at 08:40 -
-
Alternatively, save the first commend as a package, e.g. 'Delete cell.m' and load it with preinitialisation, i.e. set by
InitializationValue[$PreInitialization] = Hold[Get[FileNameJoin[{$HomeDirectory, "Documents", "Delete cell.m"}]]]That will load 'Delete cell.m' every time Mathematica is started. Seems to slow down startup a bit though. – Chris Degnen Apr 09 '22 at 10:32 -
TextResources\MenuSetup.tr does not seem to exist in
FileNameJoin[{$UserBaseDirectory, "SystemFiles", "FrontEnd"}]on version 13 for Macs. – Chris Degnen Apr 09 '22 at 10:42
ctrl+shift+.at least for me. – narip Apr 09 '22 at 10:59