diff --git a/README.md b/README.md index 386cb683..e3d7f3f4 100644 --- a/README.md +++ b/README.md @@ -279,6 +279,7 @@ following rules are enabled by default: * `quotation_marks` – fixes uneven usage of `'` and `"` when containing args'; * `path_from_history` – replaces not found path with similar absolute path from history; * `react_native_command_unrecognized` – fixes unrecognized `react-native` commands; +* `remove_shell_prompt_literal` – remove leading shell prompt symbol `$`, common when copying commands from documentations; * `remove_trailing_cedilla` – remove trailling cedillas `รง`, a common typo for european keyboard layouts; * `rm_dir` – adds `-rf` when you try to remove a directory; * `scm_correction` – corrects wrong scm like `hg log` to `git log`; diff --git a/tests/rules/test_remove_shell_prompt_literal.py b/tests/rules/test_remove_shell_prompt_literal.py new file mode 100644 index 00000000..f6fd7ba7 --- /dev/null +++ b/tests/rules/test_remove_shell_prompt_literal.py @@ -0,0 +1,32 @@ +import pytest +from thefuck.rules.remove_shell_prompt_literal import match, get_new_command +from thefuck.types import Command + + +@pytest.mark.parametrize('command', + [ + Command('$ cd newdir', '$: command not found'), + Command(' $ cd newdir', '$: command not found'), + ]) +def test_match(command): + assert match(command) + + +@pytest.mark.parametrize('command', + [ + Command('$', ''), + Command('$?', ''), + Command(' $?', ''), + Command('', ''), + ]) +def test_not_match(command): + assert not match(command) + + +@pytest.mark.parametrize('command, new_command', + [ + (Command('$ cd newdir', ''), 'cd newdir'), + (Command('$ python3 -m virtualenv env', ''), 'python3 -m virtualenv env'), + ]) +def test_get_new_command(command, new_command): + assert get_new_command(command) == new_command diff --git a/thefuck/rules/remove_shell_prompt_literal.py b/thefuck/rules/remove_shell_prompt_literal.py new file mode 100644 index 00000000..6942b67c --- /dev/null +++ b/thefuck/rules/remove_shell_prompt_literal.py @@ -0,0 +1,20 @@ +"""Fixes error for commands containing the shell prompt symbol '$'. + +This usually happens when commands are copied from documentations +including them in their code blocks. + +Example: +> $ git clone https://github.com/nvbn/thefuck.git +bash: $: command not found... +""" + +import re + +def match(command): + return ("$: command not found" in command.output and + re.search(r"^[\s]*\$ [\S]+", command.script) is not None) + +def get_new_command(command): + return command.script.replace("$", "", 1).strip() + +requires_output = True \ No newline at end of file