mirror of
https://github.com/jbranchaud/til
synced 2026-01-03 07:08:01 +00:00
Add Find The Nth Character Position In A File as a Vim til
This commit is contained in:
@@ -10,7 +10,7 @@ pairing with smart people at Hashrocket.
|
||||
|
||||
For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186).
|
||||
|
||||
_1180 TILs and counting..._
|
||||
_1181 TILs and counting..._
|
||||
|
||||
---
|
||||
|
||||
@@ -1236,6 +1236,7 @@ _1180 TILs and counting..._
|
||||
- [End Of The Word](vim/end-of-the-word.md)
|
||||
- [Escaping Terminal-Mode In An Nvim Terminal](vim/escaping-terminal-mode-in-an-nvim-terminal.md)
|
||||
- [Filter Lines Through An External Program](vim/filter-lines-through-an-external-program.md)
|
||||
- [Find The Nth Character Position In A File](vim/find-the-nth-character-position-in-a-file.md)
|
||||
- [Fix The Spelling Of A Word](vim/fix-the-spelling-of-a-word.md)
|
||||
- [Fold A Visual Selection And Expand It Back](vim/fold-a-visual-selection-and-expand-it-back.md)
|
||||
- [For When That Escape Key Is Hard To Reach](vim/for-when-that-escape-key-is-hard-to-reach.md)
|
||||
|
||||
28
vim/find-the-nth-character-position-in-a-file.md
Normal file
28
vim/find-the-nth-character-position-in-a-file.md
Normal file
@@ -0,0 +1,28 @@
|
||||
# Find The Nth Character Position In A File
|
||||
|
||||
While trying to load a JSON file in a JavaScript program, I got an error
|
||||
message. The error message said that there was an issue parsing the JSON file
|
||||
at the 9010th character position in the file. Though highly specific, this
|
||||
didn't feel particularly actionable. I'm not going to count out 9010 characters
|
||||
in a massive JSON file.
|
||||
|
||||
It turns out that Vim can help with this. After opening the file, I can then
|
||||
run this search:
|
||||
|
||||
```
|
||||
/\%^\_.\{9010}/e
|
||||
```
|
||||
|
||||
This will put my cursor right on the 9010th character.
|
||||
|
||||
It matches on the first character position in the file (`\%^`), then it matches
|
||||
on _any single character or end-of-line_ (`:h /\_.`), and then it matches on
|
||||
that character class the number of times specified (`\{N}`) — in this case,
|
||||
9010 times.
|
||||
|
||||
Lastly, the second `/` marks the end of the search pattern and the `e` tells
|
||||
the search to place the cursor at the end of the match. Without the `e`, the
|
||||
cursor will be placed at the beginning of the match. For a match on thousands
|
||||
of characters, that's not too helpful.
|
||||
|
||||
[source](https://vi.stackexchange.com/a/25308/28962)
|
||||
Reference in New Issue
Block a user