Focus on a specific word by hotkey


#1

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.


#2

You can put the following in your init.coffee

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:

'atom-text-editor':
  'F1': 'custom:select-next-chapter'

#3

There’s an error when I press F1:

/home/lom/.atom/init.coffee:3
Hide Stack Trace
TypeError: undefined is not a function
  at atom-text-editor.<anonymous> (/home/lom/.atom/init.coffee:3:55)
  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)

#4

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


#5

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


#6

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