Amazon Cyber Monday deals See deals →ad

Mets Minor League Blog

Music theory, instruments and musicians

Mets Minor League Blog

Music theory, instruments and musicians

PyCharm Comment Shortcut: Toggle Lines and Blocks

In PyCharm, press Ctrl+/ on Windows or Linux, or ⌘/ on macOS, to toggle line comments for the selected line or lines. For block comments, use Ctrl+Shift+/ on Windows or Linux, or ⌥⌘/ on macOS; the exact behavior can vary by language and keymap.

Which shortcut should you use?

The shortcut most people mean by “comment” is the line-comment toggle. Put the caret on a line—or select several lines—and press the line-comment shortcut. PyCharm adds the language’s line-comment marker where appropriate. Pressing the same shortcut again removes comments from lines it recognizes as commented.

  • Windows and Linux, line comment: Ctrl+/
  • macOS, line comment: ⌘/ (Command+/)
  • Windows and Linux, block comment: Ctrl+Shift+/
  • macOS, block comment: ⌥⌘/ (Option+Command+/)

These are PyCharm’s standard keymap bindings, not universal shortcuts for every editor or operating system. Keyboard layout, a customized keymap, or an operating-system shortcut can affect what happens when you press the keys. If the shortcut does not work, use the IDE’s action search or check the active keymap, as described below.

Line comments and block comments

A line comment marks text on a line as non-executable or non-parsed code, according to the language. For example, PyCharm can turn a Python line such as print("Hello") into # print("Hello"). In JavaScript, the corresponding marker is usually //. The IDE chooses the syntax based on the file’s language; you do not need to type the marker yourself.

A block comment surrounds a region using the comment syntax supported by the language, such as /* ... */ in Java or CSS. Block-comment commands are useful for marking a larger section, but they are not interchangeable with line comments in every language. Python, for instance, has no dedicated block-comment syntax: its conventional comments begin with #, so block-comment behavior may differ from languages that support paired delimiters.

Commenting code is not the same as deleting it. The text remains in the file, but the language generally treats the commented portion as a comment. Be mindful that some languages have special rules for comments inside expressions, documentation comments, or nested comment delimiters.

How to comment one line or a selection

  1. Open a source file in PyCharm and click in the line you want to comment. You do not have to select the whole line.
  2. For multiple lines, select the relevant text or place the caret across the lines, depending on your editing preference.
  3. Press the line-comment shortcut for your operating system. PyCharm applies the appropriate comment marker to the affected lines.
  4. Press the same shortcut again to toggle the comment off when the selection and current comment state allow it.

For example, selecting three Python statements and pressing Ctrl+/ on Windows or Linux adds # to each statement. This is usually safer than manually inserting comment characters because the IDE handles the language’s syntax and line positions.

Find or change the key binding

If the listed keys do nothing, do not assume PyCharm lacks the command. First confirm which keymap is active: open Settings on Windows or Linux, or Preferences on macOS, and look under Keymap. Search for “Comment with Line Comment” or “Comment with Block Comment” to see the assigned shortcut and whether it conflicts with another action.

You can also search for the action by name using PyCharm’s action-search feature. Depending on the version and keymap, the default action-search shortcut is commonly Shift twice (Search Everywhere); you can type the command name and run it without knowing its key binding. Menus and labels can vary between releases, so the Keymap settings are the reliable place to inspect or customize shortcuts.

To assign a different shortcut, find the action in Keymap, open its context menu, and add a keyboard shortcut. Choose a combination that is not already used by an important action. If PyCharm reports a conflict, review it before saving: replacing a binding can make another command harder to access.

Common reasons the shortcut fails

  • Wrong modifier for the platform: use Command on macOS, not Ctrl, for the standard line-comment binding.
  • Keyboard layout differences: slash may require a modifier on some layouts. Try the action from search or inspect its displayed key binding.
  • A customized keymap: imported settings or a selected keymap may assign a different combination.
  • An application conflict: another program or operating-system feature may capture the keys before PyCharm receives them.
  • Unsupported or unusual file context: PyCharm needs to recognize the file’s language to select suitable comment syntax. Check the file type and language association if the result is unexpected.
  • Selection or caret placement: line-comment commands work on the current line even without a selection, but block-comment behavior depends on the language and selected region.

If the shortcut inserts an unexpected character or triggers another feature, check the keyboard layout and keymap before changing code or reinstalling the IDE. Running the comment action from search helps determine whether the issue is the command itself or only its shortcut.

When to use comments—and when not to

Line and block comments are useful for temporarily disabling code during a small experiment, explaining a non-obvious decision, or leaving a concise note for maintainers. They are not a substitute for removing obsolete code, using version control, or writing clear names and structure. Large commented-out sections can become stale and make active code harder to scan; delete code you no longer need and rely on Git history to recover it.

Also distinguish ordinary comments from documentation comments. Some languages and frameworks recognize special forms—such as Java documentation comments beginning with /**—and use them to generate API documentation. A generic comment shortcut may not create the exact documentation template you want, so check the language-specific action or live template when writing API docs.

Frequently Asked Questions

What is the shortcut to comment a line in PyCharm?
Use Ctrl+/ on Windows or Linux, and ⌘/ (Command+/) on macOS. It toggles a line comment for the current line or selected lines.

How do I uncomment code in PyCharm?
Use the same line-comment shortcut again on the commented line or selection. The action toggles the comment state rather than requiring a separate uncomment command in the usual case.

How do I block comment in PyCharm?
The standard block-comment shortcut is Ctrl+Shift+/ on Windows or Linux, and ⌥⌘/ (Option+Command+/) on macOS. Its result depends on whether the current language supports block comments.

Why does Ctrl+/ not work on my keyboard?
The active keymap, keyboard layout, or another application may interfere. Search PyCharm’s actions for “Comment with Line Comment” and inspect its binding under Settings or Preferences > Keymap.

Can I change PyCharm’s comment shortcut?
Yes. Find the comment action in Keymap settings and assign a new combination. Check for conflicts so the new binding does not unintentionally replace another useful command.

PyCharm Comment Shortcut: Toggle Lines and Blocks
Scroll to top