当前位置: 代码网 > it编程>开发工具>Eclipse > 通过操作实例,看看怎么在atom中添加自定义快捷键

通过操作实例,看看怎么在atom中添加自定义快捷键

2025年03月30日 Eclipse 我要评论
怎么在atom中添加自定义快捷键?本篇文章给大家以language-markdown为例,介绍一下实现markdown多级标题快捷设定的方法,希望对大家有所帮助!问题的描述在使用markdown写学习

怎么在atom中添加自定义快捷键?本篇文章给大家以language-markdown为例,介绍一下实现markdown多级标题快捷设定的方法,希望对大家有所帮助!

通过操作实例,看看怎么在atom中添加自定义快捷键

问题的描述

在使用markdown写学习笔记的时候,一开始选择markdownpad 2作为编辑器,但是markdownpad对latex公式,以及贴图的使用十分不友好,但存在着一些友好的快捷键,如ctrl+1快速添加1级标题,也设置了一个toolbar能够快速的进行对文本加粗,插入网址超链接等操作,比较适合新手。但是markdownpad 2对latex等数学公式、贴入图片等方面使用效果不好。

atom是一款非常好的markdown编辑器,(下载网址),支持多种编程语言格式,同时开源,有很多的第三方package以及theme来使得编辑器更加的人性化。【相关推荐:atom】

其中的language-markdown是atom必装的markdown增强库,其中设定了一系列的快捷,如:

language-markdown的快捷键
但atom中却没有快速添加markdown标题的快捷键。为了解决这个问题,需要自定义快捷键。(ps:截至到发博,未见有其他类似教程)现在是我整个分析和操作的思路,如果看官没有时间,建议直接下载我修改好的文件,覆盖覆盖language-markdown目录下的同名文件夹,并重启atom即可:csdn下载链接

atom自定义快捷键-keymaps解析及应用

atom中的快捷键功能非常强大, 同一个快捷键,在atom的不同窗口上实现的功能是不一样的,同时还支持自定义。在atom的settings-keybindings中进行查看

在这里插入图片描述

可以发现ctrl++就对应着好3条功能,从sorce上在不同的view里确实是实现了不同的功能,按照界面的提示,我们复制在markdown-preview-plus中的快捷键语法,如下:

'.platform-win32 .markdown-preview-plus':
  'ctrl-+': 'markdown-preview-plus:zoom-in'
登录后复制

对比一下在keybindings的描述:
在这里插入图片描述

我们可以发现,atom快捷键设定的语法特点是:

'selector':
  'keystroke': 'command'
登录后复制

keystroke是我们要设定的快捷键,command是快捷键执行的命令,而source指示的是该快捷键在哪个package中,而selector是选择器,可以认为跟css选择器差不多,都是定位元素位置,在atom中大概是识别监测快捷键发生的上下文位置把。重点分析command,感觉这个好像是引用了包中的一个函数。

修改language-markdown包,实现atom中markdown多级标题快捷设定

查看language-markdown中设定的一个快捷键:

'atom-text-editor[data-grammar="text md"]':
  '*': 'markdown:strong-emphasis'
登录后复制

在package中,搜索strong-emphasis的关键字,发现在lib文件的’main.js`中有多处匹配记录,并发现有以下的内容(189-202行):

  addcommands () {
    this.subscriptions.add(atom.commands.add('atom-text-editor', 'markdown:indent-list-item', (event) => this.indentlistitem(event)))
    this.subscriptions.add(atom.commands.add('atom-text-editor', 'markdown:outdent-list-item', (event) => this.outdentlistitem(event)))
    this.subscriptions.add(atom.commands.add('atom-text-editor', 'markdown:emphasis', (event) => this.emphasizeselection(event, '_')))
    this.subscriptions.add(atom.commands.add('atom-text-editor', 'markdown:strong-emphasis', (event) => this.emphasizeselection(event, '**')))
    this.subscriptions.add(atom.commands.add('atom-text-editor', 'markdown:strike-through', (event) => this.emphasizeselection(event, '~~')))
    this.subscriptions.add(atom.commands.add('atom-text-editor', 'markdown:link', (event) => this.linkselection(event)))
    this.subscriptions.add(atom.commands.add('atom-text-editor', 'markdown:image', (event) => this.linkselection(event, true)))
    this.subscriptions.add(atom.commands.add('atom-text-editor', 'markdown:toggle-task', (event) => this.toggletask(event)))
    if (atom.indevmode()) {
      this.subscriptions.add(atom.commands.add('atom-workspace', 'markdown:compile-grammar-and-reload', () => this.compilegrammar()))
    }
  },
登录后复制

这一段代码出现了问题描述中所展示的language-markdown包的快捷键描述的command,并发现strong-emphasis是调用了js中的emphasizeselection函数。由于strong-emphasis实现了文字的加粗显示功能,而在markdown中的文字加粗显示其实就是在要加粗的文字前后加**,而markdown设定标题其实就是在文本前后加多个#。故可以分析emphasizeselection函数来达到我们的目的,emphasizeselection函数如下:

emphasizeselection (event, token) {
    let didsomewrapping = false
    if (atom.config.get('language-markdown.emphasisshortcuts')) {
      const editor = atom.workspace.getactivetexteditor()
      if (!editor) return

      const ranges = this.getselectedbufferrangesreversed(editor)
      for (const range of ranges) {
        const text = editor.gettextinbufferrange(range)
        /*
        skip texts that contain a line-break, or are empty.
        multi-line emphasis is not supported 'anyway'.

        if afterwards not a single selection has been wrapped, cancel the event
        and insert the character as normal.

        if two cursors were found, but only one of them was a selection, and the
        other a normal cursor, then the normal cursor is ignored, and the single
        selection will be wrapped.
        */
        if (text.length !== 0 && text.indexof('\n') === -1) {
          const wrappedtext = this.wraptext(text, token)
          editor.settextinbufferrange(range, wrappedtext)
          didsomewrapping = true
        }
      }
    }
    if (!didsomewrapping) {
      event.abortkeybinding()
    }
    return
  },
登录后复制

从源代码中,我们分析得知,在判断一系列条件下:当有选中文字,且为单行时,就在text前后加token,而token正是addcommand函数中设定的**。但是由于markdown设定标题,是文本前后各有一个空格,然后再加#: # header1 #。所以我们可以对这个函数进行非常简单的修改,即在调用的this.wraptext(text, token)时,直接在text然后加上空格符就行了,如复制一份emphasizeselection代码,并命名为addwords:

  addwords (event, token) {
    let didsomewrapping = false
    if (atom.config.get('language-markdown.emphasisshortcuts')) {
      const editor = atom.workspace.getactivetexteditor()
      if (!editor) return

      const ranges = this.getselectedbufferrangesreversed(editor)
      for (const range of ranges) {
        const text = editor.gettextinbufferrange(range)
        /*
        skip texts that contain a line-break, or are empty.
        multi-line emphasis is not supported 'anyway'.

        if afterwards not a single selection has been wrapped, cancel the event
        and insert the character as normal.

        if two cursors were found, but only one of them was a selection, and the
        other a normal cursor, then the normal cursor is ignored, and the single
        selection will be wrapped.
        */
        if (text.length !== 0 && text.indexof('\n') === -1) {
          //2021年2月4日 14:55:26,这里需要在text文本上前后加空格,不然,不能正常的设定1-3级标题
          const wrappedtext = this.wraptext(" "+text+" ", token)
          editor.settextinbufferrange(range, wrappedtext)
          didsomewrapping = true
        }
      }
    }
    if (!didsomewrapping) {
      event.abortkeybinding()
    }
    return
  }
登录后复制

在addcommands中中添加三行关于 addwords的设定,即可完成快捷键command的设定,当选中文本调用'markdown:header1',便会自动将文本设定为一级标题,修改后的addcommands:

  addcommands () {
    this.subscriptions.add(atom.commands.add('atom-text-editor', 'markdown:indent-list-item', (event) => this.indentlistitem(event)))
    this.subscriptions.add(atom.commands.add('atom-text-editor', 'markdown:outdent-list-item', (event) => this.outdentlistitem(event)))
    this.subscriptions.add(atom.commands.add('atom-text-editor', 'markdown:emphasis', (event) => this.emphasizeselection(event, '_')))
    this.subscriptions.add(atom.commands.add('atom-text-editor', 'markdown:strong-emphasis', (event) => this.emphasizeselection(event, '**')))
    this.subscriptions.add(atom.commands.add('atom-text-editor', 'markdown:strike-through', (event) => this.emphasizeselection(event, '~~')))
    this.subscriptions.add(atom.commands.add('atom-text-editor', 'markdown:link', (event) => this.linkselection(event)))
    this.subscriptions.add(atom.commands.add('atom-text-editor', 'markdown:image', (event) => this.linkselection(event, true)))
    this.subscriptions.add(atom.commands.add('atom-text-editor', 'markdown:toggle-task', (event) => this.toggletask(event)))
    this.subscriptions.add(atom.commands.add('atom-text-editor', 'markdown:header1', (event) => this.addwords(event, '#')))
    this.subscriptions.add(atom.commands.add('atom-text-editor', 'markdown:header2', (event) => this.addwords(event, '##')))
    this.subscriptions.add(atom.commands.add('atom-text-editor', 'markdown:header3', (event) => this.addwords(event, '###')))

    if (atom.indevmode()) {
      this.subscriptions.add(atom.commands.add('atom-workspace', 'markdown:compile-grammar-and-reload', () => this.compilegrammar()))
    }
  },
登录后复制

现在已经完成快捷键的设定了,然后就可以用我们在分析keybindings分析得的快捷键语法,在keymap文件中设定快捷键,如:

'atom-text-editor[data-grammar="text md"]':
  'ctrl-1': 'markdown:header1'
  'ctrl-2': 'markdown:header2'
  'ctrl-3': 'markdown:header3'
登录后复制

ctrl+数字的方法跟markdownpad2中的快捷键保持一致,但要注意这里只设计到三级标题,可以应对大部分的写作情况。当然,也可分析源码,自定义其他的功能函数,来实现更为复杂的命令。

另外一种设定快捷键的方式,是直接改写language-markdown的快捷键配置文件。在atom中,快捷键的自定义设定在keymaps.cson文件中设定,分析language-markdown发现,其存在keymaps中的文件夹,其中有一个cson文件,打开文件,发现果然是有关快捷键的设定:

'.platform-darwin atom-workspace':
  'cmd-alt-ctrl-c': 'markdown:compile-grammar-and-reload''.platform-win32 atom-workspace':
  'shift-alt-ctrl-c': 'markdown:compile-grammar-and-reload''.platform-linux atom-workspace':
  'shift-alt-ctrl-c': 'markdown:compile-grammar-and-reload''.platform-darwin atom-text-editor[data-grammar="text md"]':
  'cmd-shift-x': 'markdown:toggle-task''.platform-win32 atom-text-editor[data-grammar="text md"]':
  'ctrl-shift-x': 'markdown:toggle-task''.platform-linux atom-text-editor[data-grammar="text md"]':
  'ctrl-shift-x': 'markdown:toggle-task''atom-text-editor[data-grammar="text md"]':
  'tab': 'markdown:indent-list-item'
  'shift-tab': 'markdown:outdent-list-item'
  '_': 'markdown:emphasis'
  '*': 'markdown:strong-emphasis'
  '~': 'markdown:strike-through'
  '@': 'markdown:link'
  '!': 'markdown:image'
登录后复制

我们将上述的三条ctrl+数字的命令粘贴在这里,重启atom后,发现成功添加了快捷键,在markdown测试也正常:

在这里插入图片描述经过对比发现,在keymaps文件中重载快捷键,其source为user,而在language-markdown中的cson中修改,其source显示为language-markdown。显然后者看起来更统一,符合强迫症患者的需求…

【相关推荐:《atom》】

以上就是通过操作实例,看看怎么在atom中添加自定义快捷键的详细内容,更多请关注代码网其它相关文章!

(0)

相关文章:

  • atom运行c++程序

    atom运行c++程序

    在 atom 中运行 c++ 程序需要以下步骤:安装 c++ builder 插件。配置编译器(如 g++ 或 clang++)。编写 c++ 代码并保存为 "... [阅读全文]
  • Atom中如果配置小程序文件,让代码高亮显示!

    Atom中如果配置小程序文件,让代码高亮显示!

    本篇文章给大家介绍一下atom配置微信小程序文件代码高亮的方法。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。小程序相关的文件后缀名是 .wx... [阅读全文]
  • atom运行代码的按键

    atom运行代码的按键

    atom 运行代码的快捷键为:windows 和 linux:control + entermacos:command + enter按下快捷键将运行选定的代码... [阅读全文]
  • 浅谈Linux安装Atom编辑器的方法

    本篇文章给大家介绍一下在linux中安装atom编辑器的方法。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。atom 文本编辑器,由 github 出品的基于 e…

    2025年03月30日 开发工具
  • atom怎么编译c语言

    atom怎么编译c语言

    在 atom 中编译 c 语言需要安装 "compile-c" 插件。安装后,可在 atom 中打开 c 语言代码文件,将光标放置在代码中,在 "工具" 菜单下... [阅读全文]
  • 浅谈Atom支持WePY的方法

    浅谈Atom支持WePY的方法

    本篇文章给大家介绍一下atom完美支持wepy的方法。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。相关推荐:《atom》为何使用wepy?我... [阅读全文]

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com