def escape_command(cmd: str, needs_enter: bool = True) -> str:
    """
    Escapes a given command for use in tmux shell commands
    If there is a "C-m" at the end of the command, assume this is already quoted

    Parameters
    ----------
    cmd : string
        The command to escape
    needs_enter : bool
        Whether the command needs an "enter" keystroke at the end of it

    Returns
    -------
    string
        The command, formatted for use in tmux
    """
    command = ""
    if not cmd or len(cmd.strip()) == 0:
        pass
    elif cmd.endswith('"C-m"'):
        command = cmd
    else:
        command = '"' + cmd.replace('"', '\\"') + '"'
        command += ' "C-m"' if needs_enter else ""

    return command