如何在 Vim 中注释掉 Python 代码块

发布于 2024-08-27 08:51:15 字数 332 浏览 5 评论 0原文

我想知道 Vim 中是否有任何键映射允许我缩进某些代码行(无论这些行是在可视模式下选择的,还是当前光标位置上方/下方的 n 行)。

因此,基本上将以下内容转换

def my_fun(x, y):
    return x + y

#def my_fun(x, y):
#    return x + y

我可以使用 #""" 来注释相关行。理想情况下,我也希望使用相同的键映射如果给定的行已被注释掉,请取消注释这些行。

I was wondering if there was any key mapping in Vim to allow me to indent certain lines of code (whether those lines have been selected in visual mode, or n lines above/below current cursor position).

So basically something that converts the following

def my_fun(x, y):
    return x + y

to

#def my_fun(x, y):
#    return x + y

I am okay with using either # or """ for commenting out the relevant lines. Ideally, I would also like the same keymapping to uncomment the lines if the given lines have been commented out.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(13

删除→记忆 2024-09-03 08:51:15

第 1 步:转到您要评论的第一行的第一列。

初始状态

第 2 步: 按:Ctrl +v 并选择要评论的行:

选择行

第 3 步: Shift-I#space (进入左侧插入模式,输入要插入的字符。选择将消失,但其中的所有行将在步骤 4 后修改。)

Comment

第 4 步: Esc

<Esc>

Step 1: Go to the the first column of the first line you want to comment.

Initial State

Step 2: Press: Ctrl+v and select the lines you want to comment:

Select lines

Step 3: Shift-I#space (Enter Insert-at-left mode, type chars to insert. The selection will disappear, but all lines within it will be modified after Step 4.)

Comment

Step 4: Esc

<Esc>

姜生凉生 2024-09-03 08:51:15

手动一种方式

:set number
:10,12s/^/#

one way manually

:set number
:10,12s/^/#
天涯离梦残月幽梦 2024-09-03 08:51:15

您可以将以下映射添加到您的 .vimrc

vnoremap <silent> # :s/^/#/<cr>:noh<cr>
vnoremap <silent> -# :s/^#//<cr>:noh<cr>

突出显示您的块:

Shift+v

# 以注释第一列中的行。

-# 以同样的方式取消注释。

You could add the following mapping to your .vimrc

vnoremap <silent> # :s/^/#/<cr>:noh<cr>
vnoremap <silent> -# :s/^#//<cr>:noh<cr>

Highlight your block with:

Shift+v

# to comment your lines from the first column.

-# to uncomment the same way.

墟烟 2024-09-03 08:51:15

使用以下命令突出显示您的块:ShiftV

使用以下命令注释所选块::norm i#(小写 i)

要取消注释,请突出显示您的块再次,并取消注释: :norm ^x

:norm 命令对每个选定的行执行一个操作。注释将在每行的开头插入一个 #,取消注释将删除该 #

Highlight your block with: ShiftV

Comment the selected block out with: :norm i# (lower case i)

To uncomment, highlight your block again, and uncomment with: :norm ^x

The :norm command performs an action for every selected line. Commenting will insert a # at the start of every line, and uncommenting will delete that #.

海风掠过北极光 2024-09-03 08:51:15

我通常会扫出一个可视块 (),然后搜索第一个字符并将其替换为:(

:'<,'>s/^/#

使用选定的可视块进入命令模式会自动将 '<,'> 放在命令行)然后我可以通过清除相同的视觉块来取消注释该块,并且:

:'<,'>s/^#//

I usually sweep out a visual block (<C-V>), then search and replace the first character with:

:'<,'>s/^/#

(Entering command mode with a visual block selected automatically places '<,'> on the command line) I can then uncomment the block by sweeping out the same visual block and:

:'<,'>s/^#//
瑾兮 2024-09-03 08:51:15

有一些很好的插件可以帮助注释/取消注释行。例如书呆子评论者

来自 NERD 评论者的快捷方式示例:

[count]|<Leader>|cc |NERDCommenterComment|
Comment out the current line or text selected in visual mode.

[count]|<Leader>|cu |NERDCommenterUncomment|
Uncomments the selected line(s).

完整文档位于此处

There are some good plugins to help comment/uncomment lines. For example The NERD Commenter.

Sample shortcuts from the NERD Commenter:

[count]|<Leader>|cc |NERDCommenterComment|
Comment out the current line or text selected in visual mode.

[count]|<Leader>|cu |NERDCommenterUncomment|
Uncomments the selected line(s).

Full documentation is located here.

鱼窥荷 2024-09-03 08:51:15

无需插件或映射。尝试使用内置的“norm”命令,该命令实际上可以在每个选定的行上执行您想要的任何操作。

添加 # 条评论

1. shift V to visually select lines
2. :norm i#

删除 # 条评论

1. visually select region as before
2. :norm x

或者,如果您的评论缩进,您可以执行 :norm ^x

请注意,这些只是前面的普通 vim 命令通过“:norm”在每一行执行它们。

在此处的答案之一中使用“norm”命令的更详细答案

在 Vim 中注释/取消注释行的快速方法是什么?

No plugins or mappings required. Try the built-in "norm" command, which literally executes anything you want on every selected line.

Add # Comments

1. shift V to visually select lines
2. :norm i#

Remove # Comments

1. visually select region as before
2. :norm x

Or if your comments are indented you can do :norm ^x

Notice that these are just ordinary vim commands being preceded by ":norm" to execute them on each line.

More detailed answer for using "norm" command in one of the answers here

What's a quick way to comment/uncomment lines in Vim?

浅浅淡淡 2024-09-03 08:51:15

我的 .vimrc 中有以下几行:

" comment line, selection with Ctrl-N,Ctrl-N
au BufEnter *.py nnoremap  <C-N><C-N>    mn:s/^\(\s*\)#*\(.*\)/\1#\2/ge<CR>:noh<CR>`n
au BufEnter *.py inoremap  <C-N><C-N>    <C-O>mn<C-O>:s/^\(\s*\)#*\(.*\)/\1#\2/ge<CR><C-O>:noh<CR><C-O>`n
au BufEnter *.py vnoremap  <C-N><C-N>    mn:s/^\(\s*\)#*\(.*\)/\1#\2/ge<CR>:noh<CR>gv`n

" uncomment line, selection with Ctrl-N,N
au BufEnter *.py nnoremap  <C-N>n     mn:s/^\(\s*\)#\([^ ]\)/\1\2/ge<CR>:s/^#$//ge<CR>:noh<CR>`n
au BufEnter *.py inoremap  <C-N>n     <C-O>mn<C-O>:s/^\(\s*\)#\([^ ]\)/\1\2/ge<CR><C-O>:s/^#$//ge<CR><C-O>:noh<CR><C-O>`n
au BufEnter *.py vnoremap  <C-N>n     mn:s/^\(\s*\)#\([^ ]\)/\1\2/ge<CR>gv:s/#\n/\r/ge<CR>:noh<CR>gv`n

只要它们以 # 开头(# 后面有空格),快捷键就会保留您的光标位置和注释。例如:

# variable x
x = 0

注释后:

# variable x
#x = 0

取消注释后:

# variable x
x = 0

I have the following lines in my .vimrc:

" comment line, selection with Ctrl-N,Ctrl-N
au BufEnter *.py nnoremap  <C-N><C-N>    mn:s/^\(\s*\)#*\(.*\)/\1#\2/ge<CR>:noh<CR>`n
au BufEnter *.py inoremap  <C-N><C-N>    <C-O>mn<C-O>:s/^\(\s*\)#*\(.*\)/\1#\2/ge<CR><C-O>:noh<CR><C-O>`n
au BufEnter *.py vnoremap  <C-N><C-N>    mn:s/^\(\s*\)#*\(.*\)/\1#\2/ge<CR>:noh<CR>gv`n

" uncomment line, selection with Ctrl-N,N
au BufEnter *.py nnoremap  <C-N>n     mn:s/^\(\s*\)#\([^ ]\)/\1\2/ge<CR>:s/^#$//ge<CR>:noh<CR>`n
au BufEnter *.py inoremap  <C-N>n     <C-O>mn<C-O>:s/^\(\s*\)#\([^ ]\)/\1\2/ge<CR><C-O>:s/^#$//ge<CR><C-O>:noh<CR><C-O>`n
au BufEnter *.py vnoremap  <C-N>n     mn:s/^\(\s*\)#\([^ ]\)/\1\2/ge<CR>gv:s/#\n/\r/ge<CR>:noh<CR>gv`n

The shortcuts preserve your cursor position and your comments as long as they start with # (there is space after #). For example:

# variable x
x = 0

After commenting:

# variable x
#x = 0

After uncomennting:

# variable x
x = 0
向地狱狂奔 2024-09-03 08:51:15

NERDcommenter 是一个优秀的评论插件,它可以自动检测许多文件类型及其关联的评论字符。使用 Pathogen 安装非常简单。

使用 cc 进行评论。使用 cu 取消注释。并使用 c 切换注释。

(vim 中默认的 键是 \

NERDcommenter is an excellent plugin for commenting which automatically detects a number of filetypes and their associated comment characters. Ridiculously easy to install using Pathogen.

Comment with <leader>cc. Uncomment with <leader>cu. And toggle comments with <leader>c<space>.

(The default <leader> key in vim is \)

沫雨熙 2024-09-03 08:51:15

坦率地说,我为该链接使用了一个tcomment插件。它可以处理几乎所有语法。它定义了很好的动作,将它与一些特定于 python 的文本块匹配器一起使用使其成为一个强大的工具。

Frankly I use a tcomment plugin for that link. It can handle almost every syntax. It defines nice movements, using it with some text block matchers specific for python makes it a powerful tool.

清旖 2024-09-03 08:51:15

vim 有很多评论插件 - 其中一些是多语言的 - 不仅仅是 python。如果您使用像 Vundle 这样的插件管理器,那么您可以搜索它们(一旦您安装了 Vundle),使用例如:

:PluginSearch comment

您将得到一个结果窗口。或者,您可以在 vim-scripts 中搜索评论插件

正如其他答案中提到的 NERDCommenter 是一个不错的选择 - 有关使用它的更多信息,请参阅 这个答案。注意: 键通常是 \。例如,注释掉一行 - 输入: \cc

There's a lot of comment plugins for vim - a number of which are multi-language - not just python. If you use a plugin manager like Vundle then you can search for them (once you've installed Vundle) using e.g.:

:PluginSearch comment

And you will get a window of results. Alternatively you can just search vim-scripts for comment plugins.

As mentioned in other answers NERDCommenter is good one - for more info on using it see this answer. Note: That the <leader> key is usually \. E.g. so to comment out a line - type: \cc

黎歌 2024-09-03 08:51:15

一个非常小的轻量级插件:vim-commentary。

gcc 注释一行
gcgc 取消注释。查看插件页面了解更多信息。

v+k/j 突出显示该块,然后 gcc 注释该块。

A very minimal light weight plugin: vim-commentary.

gcc to comment a line
gcgc to uncomment. check out the plugin page for more.

v+k/j highlight the block then gcc to comment that block.

凉风有信 2024-09-03 08:51:15

CtrlK 进行注释(可视模式):

vnoremap <silent> <C-k> :s#^#\##<cr>:noh<cr>

CtrlU 进行注释取消注释(视觉模式):

vnoremap <silent> <C-u> :s#^\###<cr>:noh<cr>

CtrlK for comment (Visual Mode):

vnoremap <silent> <C-k> :s#^#\##<cr>:noh<cr>

CtrlU for uncomment (Visual Mode):

vnoremap <silent> <C-u> :s#^\###<cr>:noh<cr>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文