简体   繁体   English

在Vim模式下使用Codemirror

[英]Using Codemirror in Vim Mode

I am having a little trouble making this thing up. 我做这件事有点麻烦。 I want to use Codemirror edit feature in Vim mode only, without any syntax highlighting. 我想只在Vim模式下使用Codemirror编辑功能,没有任何语法高亮。 That is, if I click in the text area, it is converted in a similar text editor as shown on the demo page VIM Demo . 也就是说,如果我单击文本区域,它将在类似的文本编辑器中转换,如演示页面VIM Demo所示 Thanks! 谢谢!

EDIT : If not Codemirror, Please tell me the method how can I convert the text area in which the user clicks to a VIM type edit area. 编辑 :如果不是Codemirror,请告诉我如何将用户单击的文本区域转换为VIM类型编辑区域的方法。 I want to make it as a plugin, so that whenever I click on a text area on some page, it gives me a VIM like (not exactly vim) environment to edit. 我想把它作为一个插件,这样每当我点击某个页面上的文本区域时,它就会给我一个像VIM那样(不完全是vim)的环境来编辑。 How should I go about the keybinding thing? 我该如何处理键绑定的事情? Please help! 请帮忙!

CodeMirror takes care of all the key bindings (and key mapping for vim mode), so all you have to do is create an editor instance for the textarea at hand. CodeMirror负责处理所有键绑定(以及vim模式的键映射),因此您所要做的就是为手头的textarea创建一个编辑器实例。

Check out CodeMirror.fromTextArea() on the docs, under the section on static methods , to see how it's done. 在静态方法部分的文档上查看CodeMirror.fromTextArea() ,看看它是如何完成的。

You can also refer to the vim bindings demo and simply take a look at the source. 您还可以参考vim绑定演示 ,只需查看源代码即可。 This is how the CodeMirror instance is initialized there: 这是CodeMirror实例在那里初始化的方式:

1  | CodeMirror.commands.save = function () {
2  |     alert("Saving");
3  | };
4  |
5  | var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
6  |     lineNumbers: true,
7  |     mode: "text/x-csrc",
8  |     keyMap: "vim",
9  |     showCursorWhenSelecting: true
10 | });

Let's take this apart, following line no': 让我们分开,继续行':

  • 1-3: Assigning a handler for the save command, which maps to :w on vim key bindings. 1-3:save命令分配一个处理程序,它在vim键绑定上映射到:w From the docs, on the key bindings section: 从文档中,在键绑定部分:

    The values of properties in keymaps can be either functions of a single argument (the CodeMirror instance), strings, or false. 键映射中的属性值可以是单个参数(CodeMirror实例),字符串或false的函数。 Such strings refer to properties of the CodeMirror.commands object, which defines a number of common commands that are used by the default keybindings, and maps them to functions 此类字符串引用CodeMirror.commands对象的属性,该对象定义了默认键绑定使用的许多常用命令,并将它们映射到函数

  • 4: The sound of silence. 4:沉默的声音。

  • 5: Fetching the textarea element (denoted with code as its ID) from the DOM, and passing it to a static method that will create a CodeMirror instance based on that element. 5:从DOM中获取textarea元素(用code表示其ID),并将其传递给静态方法,该方法将基于该元素创建CodeMirror实例。

  • 6-9: Setting various options: 6-9:设置各种选项:

    • 6: Showing line numbers in the gutter. 6:在排水沟中显示行号。

    • 7: Setting editor mode to C-like to highlight C code. 7:将编辑器模式设置为C-like以突出显示C代码。

    • 8: Assign the vim key bindings. 8:分配vim密钥绑定。

    • 9: Well, showing cursor when selecting. 9:好吧,选择时显示光标。

  • 10: Wrap it up. 10:把它包起来。

In order for the editor mode and key bindings to work, the required scripts need to be loaded, so we'd wanna include those as well: 为了使编辑器模式和键绑定工作,需要加载所需的脚本,所以我们也想包括这些:

<script src="../lib/codemirror.js"></script> <!-- main script -->
<script src="../addon/dialog/dialog.js"></script> <!-- addon for vim messages -->
<script src="../addon/search/searchcursor.js"></script> <!-- addon for vim messages -->
<script src="../mode/clike/clike.js"></script> <!-- mode for C-like languages -->
<script src="../keymap/vim.js"></script> <!-- key bindings for vim -->

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM