English 中文(简体)
在 vim 的存储映射中自动执行 < enter> 失败
原标题:Having trouble executing <enter> automatically in a stored mapping in vim

I read this stack thread on how to save a mapping permanently in vim. I went to /etc/vim/vimrc and added the :map ,k :!python % <enter> in the first line of the file. When I open a new python file with vim the command responds by showing :!python % <enter> at the bottom of the screen. But it doesn t execute automatically. How do I fix this command to execute enter automatically?

最佳回答

我要找的答案是粘贴 :map :! python% & lt; enter>

问题回答

i 认为您更希望本地设置使用本地版本 而不是编辑文件 /etc/vim/vimrc 中的全局设置。 您可以添加 :

let maplocalleader=","
map <localleader>k :!python %<cr>

i 建议不要使用 ZyX 评论中解释的对 ghoti s 回答 < a href=>的解释

< 强力 > 编辑 < /强 >

ZyX评论我更改了上面的代码

filetype plugin on
augroup ft_python
    au!
    au FileType python nnoremap ,k :!python %<cr>
augroup END

使 ,k 无法与其它东西乱搞

  • filetype plugin on says that we want to have things done if a file has a certain filetype - in our case python
  • augroup ft_python says i want to have a group of autocommands := "commands that are invoked when opening a file" with that group-name
  • au! removes all autocommands
  • au Filetype python declares an autocommand=au for all files of type python
  • augroup END leaves the group ft_python and goes back to general settings

这样就可以让 ,k 以 python 格式进行编译, 但是当我不小心按下 ,k 在另一个文件中说有源代码时, 它什么都不做( 或者我告诉 ,k 的东西, 成为我.vimrc 中其他地方的源代码)。

将返回字符插入字面运输返回字符 - 按 Ctrl + V, 写地图时输入 :

:map ,k :!python %^M

键入 Ctrl + V 序列后,您可以看到运回显示为 , 然后按下输入键 。

Ctrl + V 键告诉 vim 插入您键入的下一个字符。





相关问题
Autoupdate VIM Plugins?

Is it possible to update vim plugins automatically?

how to unindent in vim without leaving edit mode?

I m writing a lot of python code recently, and i used the tab-to-space mode in vim. I was just wondering how would i unindent in vim without leaving edit mode for example after i finished if...: block....

Scrolling inside Vim in Mac s Terminal

I ve been googling around trying to figure out if it s possible to use my mouse wheel to scroll while inside Vim in Mac s Terminal, with no luck. It seems as if only X11 or iTerm support this. Before ...

Vim - Deleting XML Comments

How do I delete comments in XML? If the opening and the closing comment tags are on the same line, I use :g/^<!--.*-->$/d to delete the comment. How to delete the comments that are spread ...

Limiting a match in vim to certain filetypes?

I have the following in my .vimrc to highlight lines longer than 80 chars: highlight OverLength ctermbg=red ctermfg=white guibg=#592929 match OverLength /\%81v.*/ This works quite well. However, the ...

Profiling Vim startup time

I’ve got a lot of plugins enabled when using Vim – I have collected plugins over the years. I’m a bit fed up with how long Vim takes to start now, so I’d like to profile its startup and see which of ...