Skip to content

Instantly share code, notes, and snippets.

@enderahmetyurt
Created July 14, 2025 09:11
Show Gist options
  • Save enderahmetyurt/cb38fb76a640da43bb905e7d947d424e to your computer and use it in GitHub Desktop.
Save enderahmetyurt/cb38fb76a640da43bb905e7d947d424e to your computer and use it in GitHub Desktop.
Given a multi-line string and a sequence of Vim navigation commands
# h for left, j for down, k for up, and l for right)
def get_char_after_vim_commands(string, commands)
lines = string.lines.map(&:chomp)
row, col = 0, 0
commands.each_char do |cmd|
dr, dc = {"h" => [0, -1], "j" => [1, 0], "k" => [-1, 0], "l" => [0, 1]}[cmd] || [0, 0]
row = (row + dr).clamp(0, lines.size - 1)
col = (col + dc).clamp(0, [lines[row].size - 1, 0].max)
end
lines[row][col]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment