Focus on a specific word by hotkey


Basically I want bind the word “Chapter” to a key (F1), so that everytime I press it, atom focuses on the next encounter of that word.
Preferably it would be independent of find-and-replace package.


You can put the following in your

atom.commands.add 'atom-text-editor', 'custom:select-next-chapter', ->
  textEditor = atom.workspace.getActiveTextEditor()
  textEditor.scanInBufferRange /Chapter/, [textEditor.getCursorBufferPosition(), [Infinity, 0]], ({range}) ->
    textEditor.setSelectedBufferRange range

and bind the command to the F1 key (or any other key you like) in your keymap.cson:

  'F1': 'custom:select-next-chapter'


There’s an error when I press F1:

Hide Stack Trace
TypeError: undefined is not a function
  at atom-text-editor.<anonymous> (/home/lom/.atom/
  at CommandRegistry.module.exports.CommandRegistry.handleCommandEvent (/usr/share/atom/resources/app.asar/src/command-registry.js:245:29)
  at /usr/share/atom/resources/app.asar/src/command-registry.js:3:61
  at KeymapManager.module.exports.KeymapManager.dispatchCommandEvent (/usr/share/atom/resources/app.asar/node_modules/atom-keymap/lib/keymap-manager.js:524:16)
  at KeymapManager.module.exports.KeymapManager.handleKeyboardEvent (/usr/share/atom/resources/app.asar/node_modules/atom-keymap/lib/keymap-manager.js:347:22)
  at HTMLDocument.module.exports.WindowEventHandler.onKeydown (/usr/share/atom/resources/app.asar/src/window-event-handler.js:177:20)


I’m sorry, getCursorPosition should be getCursorBufferPosition. I’ll update the answer above.


That did the trick, thanks.
I also created second hotkey for searching upwards by changing [Infinity, 0] to [0, Infinity]]


You should change that to [0, 0], otherwise it won’t find a match on the 1st line of a file, and maybe you want to use textEditor.backwardsScanInBufferRange to make sure you get the last match before the cursor