-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvim-tutorial.html
More file actions
1731 lines (1649 loc) · 73.2 KB
/
Copy pathvim-tutorial.html
File metadata and controls
1731 lines (1649 loc) · 73.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Learn Vim — Interactive Tutorial with In-Browser Simulator</title>
<meta name="description" content="Learn Vim interactively with a working in-browser simulator. 13 progressive lessons covering motions, text objects, visual mode, macros, and more. Zero dependencies, works offline.">
<meta name="author" content="Nathan Bost">
<meta name="robots" content="index, follow">
<meta property="og:title" content="Learn Vim — Interactive Tutorial">
<meta property="og:description" content="Practice Vim commands in your browser with a working simulator. 13 lessons from basics to advanced, with exercise tracking.">
<meta property="og:type" content="website">
<meta property="og:locale" content="en_US">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:title" content="Learn Vim — Interactive Tutorial">
<meta name="twitter:description" content="Practice Vim commands in your browser with a working simulator. 13 lessons from basics to advanced.">
<link rel="canonical" href="https://nbost130.github.io/vim-tutorial/">
<style>
:root {
--bg: #1a1b26; --bg2: #24283b; --bg3: #292e42;
--fg: #c0caf5; --fg2: #a9b1d6; --fg3: #565f89;
--green: #9ece6a; --blue: #7aa2f7; --purple: #bb9af7;
--red: #f7768e; --orange: #ff9e64; --yellow: #e0af68;
--cyan: #7dcfff; --teal: #73daca;
}
* { margin: 0; padding: 0; box-sizing: border-box; }
body { font-family: 'SF Mono', 'Fira Code', 'Cascadia Code', monospace; background: var(--bg); color: var(--fg); display: flex; height: 100vh; overflow: hidden; }
/* Sidebar */
.sidebar { width: 280px; background: var(--bg2); border-right: 1px solid var(--bg3); overflow-y: auto; flex-shrink: 0; }
.sidebar h1 { padding: 20px; font-size: 18px; color: var(--green); border-bottom: 1px solid var(--bg3); }
.sidebar h1 span { color: var(--fg3); font-weight: normal; font-size: 12px; display: block; margin-top: 4px; }
.nav-section { padding: 8px 0; border-bottom: 1px solid var(--bg3); }
.nav-section-title { padding: 6px 20px; font-size: 11px; text-transform: uppercase; color: var(--fg3); letter-spacing: 1px; }
.nav-item { padding: 8px 20px 8px 28px; cursor: pointer; font-size: 13px; color: var(--fg2); transition: all 0.15s; display: flex; align-items: center; gap: 8px; }
.nav-item:hover { background: var(--bg3); color: var(--fg); }
.nav-item.active { background: var(--bg3); color: var(--blue); border-left: 2px solid var(--blue); }
.nav-item .check { color: var(--green); font-size: 11px; opacity: 0; }
.nav-item.completed .check { opacity: 1; }
.progress-bar { margin: 12px 20px; height: 4px; background: var(--bg3); border-radius: 2px; }
.progress-fill { height: 100%; background: var(--green); border-radius: 2px; transition: width 0.3s; width: 0%; }
/* Main */
.main { flex: 1; display: flex; flex-direction: column; overflow: hidden; }
/* Lesson area */
.lesson { flex: 1; overflow-y: auto; padding: 40px 60px; max-width: 900px; }
.lesson h2 { color: var(--blue); font-size: 24px; margin-bottom: 8px; }
.lesson h3 { color: var(--purple); font-size: 16px; margin: 24px 0 8px; }
.lesson .subtitle { color: var(--fg3); margin-bottom: 24px; font-size: 14px; }
.lesson p { line-height: 1.7; margin-bottom: 12px; font-size: 14px; }
.lesson ul { margin: 8px 0 16px 20px; }
.lesson li { line-height: 1.7; font-size: 14px; margin-bottom: 4px; }
.lesson code { background: var(--bg3); padding: 2px 6px; border-radius: 3px; color: var(--orange); font-size: 13px; }
.lesson .kbd { display: inline-block; background: var(--bg3); border: 1px solid var(--fg3); border-radius: 4px; padding: 1px 8px; font-size: 13px; color: var(--yellow); min-width: 24px; text-align: center; }
.lesson pre { background: var(--bg2); border: 1px solid var(--bg3); border-radius: 6px; padding: 16px; margin: 12px 0; overflow-x: auto; font-size: 13px; line-height: 1.5; }
.lesson .tip { background: rgba(122,162,247,0.1); border-left: 3px solid var(--blue); padding: 12px 16px; margin: 16px 0; border-radius: 0 6px 6px 0; font-size: 13px; }
.lesson .try-it { background: rgba(158,206,106,0.1); border-left: 3px solid var(--green); padding: 12px 16px; margin: 16px 0; border-radius: 0 6px 6px 0; font-size: 13px; }
.lesson .try-it strong { color: var(--green); }
.lesson table { width: 100%; border-collapse: collapse; margin: 12px 0; font-size: 13px; }
.lesson th { text-align: left; padding: 8px 12px; border-bottom: 2px solid var(--bg3); color: var(--purple); }
.lesson td { padding: 6px 12px; border-bottom: 1px solid var(--bg3); }
.lesson td:first-child { color: var(--yellow); font-weight: bold; white-space: nowrap; }
/* Vim Simulator */
.sim-container { border-top: 1px solid var(--bg3); background: var(--bg2); }
.sim-header { display: flex; justify-content: space-between; align-items: center; padding: 8px 16px; background: var(--bg3); }
.sim-title { font-size: 12px; color: var(--fg3); }
.sim-mode { font-size: 12px; font-weight: bold; padding: 2px 10px; border-radius: 3px; }
.sim-mode.normal { background: var(--blue); color: var(--bg); }
.sim-mode.insert { background: var(--green); color: var(--bg); }
.sim-mode.visual { background: var(--purple); color: var(--bg); }
.sim-mode.command { background: var(--orange); color: var(--bg); }
.sim-editor { height: 200px; padding: 8px 0; overflow-y: auto; cursor: text; outline: none; position: relative; }
.sim-line { display: flex; font-size: 13px; line-height: 1.6; padding: 0 16px; }
.sim-linenum { color: var(--fg3); width: 32px; text-align: right; margin-right: 12px; user-select: none; flex-shrink: 0; }
.sim-text { white-space: pre; flex: 1; }
.sim-text .cursor { position: relative; }
.sim-text .cursor::after { content: ''; position: absolute; left: 0; top: 0; width: 8px; height: 100%; }
.sim-text .cursor-block::after { background: rgba(122,162,247,0.7); }
.sim-text .cursor-line::after { width: 2px; background: var(--green); }
.sim-text .visual-sel { background: rgba(187,154,247,0.3); }
.sim-status { padding: 4px 16px; font-size: 12px; color: var(--fg3); display: flex; justify-content: space-between; border-top: 1px solid var(--bg3); min-height: 24px; }
.sim-status .cmd-line { color: var(--fg); }
.sim-status .pos { color: var(--fg3); }
.sim-feedback { padding: 6px 16px; font-size: 12px; color: var(--teal); background: rgba(115,218,202,0.08); min-height: 28px; display: flex; align-items: center; gap: 8px; }
.sim-feedback .action { color: var(--yellow); }
/* Exercise */
.exercise-box { margin: 16px 0; padding: 16px; background: var(--bg2); border: 1px solid var(--bg3); border-radius: 6px; }
.exercise-box h4 { color: var(--teal); margin-bottom: 8px; font-size: 14px; }
.exercise-steps { list-style: none; margin: 0; padding: 0; }
.exercise-steps li { padding: 4px 0; font-size: 13px; display: flex; align-items: center; gap: 8px; }
.exercise-steps li .step-check { width: 16px; height: 16px; border: 1px solid var(--fg3); border-radius: 50%; display: flex; align-items: center; justify-content: center; font-size: 10px; flex-shrink: 0; }
.exercise-steps li.done .step-check { border-color: var(--green); color: var(--green); }
.next-btn { display: inline-block; margin-top: 20px; padding: 10px 24px; background: var(--blue); color: var(--bg); border: none; border-radius: 6px; cursor: pointer; font-family: inherit; font-size: 14px; font-weight: bold; }
.next-btn:hover { opacity: 0.9; }
.attribution { padding: 16px 20px; border-top: 1px solid var(--bg3); font-size: 11px; color: var(--fg3); line-height: 1.6; margin-top: auto; }
.attribution a { color: var(--blue); text-decoration: none; }
.attribution a:hover { text-decoration: underline; }
.sidebar { display: flex; flex-direction: column; }
.sidebar::-webkit-scrollbar, .lesson::-webkit-scrollbar, .sim-editor::-webkit-scrollbar { width: 6px; }
.sidebar::-webkit-scrollbar-thumb, .lesson::-webkit-scrollbar-thumb, .sim-editor::-webkit-scrollbar-thumb { background: var(--bg3); border-radius: 3px; }
</style>
</head>
<body>
<div class="sidebar">
<h1>Learn Vim<span>Interactive Tutorial</span></h1>
<div class="progress-bar"><div class="progress-fill" id="progressFill"></div></div>
<div id="navContainer"></div>
<div class="attribution">
Based on <a href="https://danielmiessler.com/blog/vim" target="_blank" rel="noopener">Learn Vim For the Last Time</a> by <a href="https://danielmiessler.com" target="_blank" rel="noopener">Daniel Miessler</a>. Interactive simulator by <a href="https://github.com/nbost130" target="_blank" rel="noopener">Nathan Bost</a>.
</div>
</div>
<div class="main">
<div class="lesson" id="lessonContent"></div>
<div class="sim-container" id="simContainer">
<div class="sim-header">
<span class="sim-title">VIM SIMULATOR — click here and type to practice</span>
<span class="sim-mode normal" id="simMode">NORMAL</span>
</div>
<div class="sim-editor" id="simEditor" tabindex="0"></div>
<div class="sim-feedback" id="simFeedback"><span class="action"></span></div>
<div class="sim-status">
<span class="cmd-line" id="simCmd"></span>
<span class="pos" id="simPos"></span>
</div>
</div>
</div>
<script>
// ===== LESSONS =====
const lessons = [
{
id: 'intro', section: 'Foundations', title: 'Why Vim?',
content: `
<h2>Why Learn Vim?</h2>
<p class="subtitle">An interactive tutorial based on <a href="https://danielmiessler.com/blog/vim" target="_blank" rel="noopener" style="color: var(--blue);">Learn Vim For the Last Time</a> by <a href="https://danielmiessler.com" target="_blank" rel="noopener" style="color: var(--blue);">Daniel Miessler</a></p>
<p>Vim is everywhere. Every Linux server, every Mac, every container. Learn one editor and use it for everything — from quick config edits to full development sessions.</p>
<h3>Three Pillars</h3>
<ul>
<li><strong>Ubiquity</strong> — It's on every system you'll ever SSH into</li>
<li><strong>Scalability</strong> — Simple config edits or full IDE-like development</li>
<li><strong>Power</strong> — Vim acts like a <em>language</em> for editing text. Once you learn the grammar, you can compose new commands on the fly.</li>
</ul>
<h3>The Five Levels of Mastery</h3>
<table>
<tr><th>Level</th><th>Description</th></tr>
<tr><td>0</td><td>Never heard of Vim</td></tr>
<tr><td>1</td><td>Know basics — can open, edit, save, quit</td></tr>
<tr><td>2</td><td>Comfortable with Visual Mode</td></tr>
<tr><td>3</td><td>Know motions and can compose commands</td></tr>
<tr><td>4</td><td>Don't need Visual Mode — think in text objects</td></tr>
</table>
<p>This tutorial will take you from Level 0 to a solid Level 3. Ready?</p>
<div class="try-it"><strong>Try it below:</strong> The simulator is running. Click the editor area below, then try pressing <span class="kbd">h</span> <span class="kbd">j</span> <span class="kbd">k</span> <span class="kbd">l</span> to move the cursor around. Welcome to Vim!</div>
`,
buffer: [
'Welcome to the Vim Interactive Tutorial!',
'',
'You are currently in NORMAL mode.',
'Use h/j/k/l to move the cursor around.',
'',
'h = left j = down',
'k = up l = right',
'',
'When you are ready, click "Next Lesson"',
'in the lesson pane above.',
],
exercises: [
{ desc: 'Move down with j', check: (s) => s.actions.includes('move-down') },
{ desc: 'Move up with k', check: (s) => s.actions.includes('move-up') },
{ desc: 'Move right with l', check: (s) => s.actions.includes('move-right') },
{ desc: 'Move left with h', check: (s) => s.actions.includes('move-left') },
]
},
{
id: 'modes', section: 'Foundations', title: 'Modes',
content: `
<h2>Understanding Modes</h2>
<p class="subtitle">The key insight that makes Vim different</p>
<p>Vim has four primary modes. This is what makes it powerful — keys do different things depending on which mode you're in.</p>
<table>
<tr><th>Mode</th><th>Purpose</th><th>Enter With</th></tr>
<tr><td>Normal</td><td>Navigate and run commands</td><td><span class="kbd">Esc</span></td></tr>
<tr><td>Insert</td><td>Type and edit text</td><td><span class="kbd">i</span> <span class="kbd">a</span> <span class="kbd">o</span></td></tr>
<tr><td>Visual</td><td>Select text</td><td><span class="kbd">v</span> <span class="kbd">V</span></td></tr>
<tr><td>Command</td><td>Run ex commands</td><td><span class="kbd">:</span></td></tr>
</table>
<p><strong>Normal mode</strong> is home base. You'll spend most of your time here. It's where motions, deletions, and all the powerful Vim commands live.</p>
<p><strong>Insert mode</strong> is where you actually type text — like a normal editor. Press <span class="kbd">i</span> to enter, <span class="kbd">Esc</span> to leave.</p>
<div class="try-it"><strong>Practice:</strong> Press <span class="kbd">i</span> to enter Insert mode. Type some text. Press <span class="kbd">Esc</span> to return to Normal mode. Watch the mode indicator change!</div>
`,
buffer: [
'This file is for practicing modes.',
'',
'Step 1: Press i to enter INSERT mode',
'Step 2: Type anything you want here',
'Step 3: Press Esc to go back to NORMAL',
'',
'Try it now!',
],
exercises: [
{ desc: 'Enter Insert mode with i', check: (s) => s.actions.includes('enter-insert') },
{ desc: 'Type some text', check: (s) => s.actions.includes('typed-text') },
{ desc: 'Return to Normal mode with Esc', check: (s) => s.actions.includes('exit-insert') },
]
},
{
id: 'movement', section: 'Movement', title: 'Basic Motions',
content: `
<h2>Basic Motions</h2>
<p class="subtitle">Moving efficiently without touching the mouse</p>
<h3>Home Row Movement</h3>
<table>
<tr><th>Key</th><th>Motion</th></tr>
<tr><td>h</td><td>Left one character</td></tr>
<tr><td>j</td><td>Down one line</td></tr>
<tr><td>k</td><td>Up one line</td></tr>
<tr><td>l</td><td>Right one character</td></tr>
</table>
<h3>Line Movement</h3>
<table>
<tr><th>Key</th><th>Motion</th></tr>
<tr><td>0</td><td>Beginning of line</td></tr>
<tr><td>$</td><td>End of line</td></tr>
<tr><td>^</td><td>First non-blank character</td></tr>
</table>
<h3>Word Movement</h3>
<table>
<tr><th>Key</th><th>Motion</th></tr>
<tr><td>w</td><td>Forward one word</td></tr>
<tr><td>b</td><td>Back one word</td></tr>
<tr><td>e</td><td>End of word</td></tr>
<tr><td>W / B</td><td>Big word (space-delimited)</td></tr>
</table>
<div class="try-it"><strong>Practice:</strong> Use <span class="kbd">w</span> to jump word-by-word, <span class="kbd">b</span> to go back. Try <span class="kbd">0</span> and <span class="kbd">$</span> to jump to line boundaries. Then try <span class="kbd">gg</span> and <span class="kbd">G</span> for top/bottom of file.</div>
`,
buffer: [
'The quick brown fox jumps over the lazy dog.',
'Vim motions let you navigate without a mouse.',
'Each motion can be combined with a count:',
' 3w = forward three words',
' 5j = down five lines',
'',
'Words are separated by punctuation:',
'hello-world has three words (hello, -, world)',
'hello world has two WORDS (big word movement)',
'',
'Practice moving around this text!',
'Try w, b, e, 0, $, gg, G',
],
exercises: [
{ desc: 'Jump forward a word with w', check: (s) => s.actions.includes('word-forward') },
{ desc: 'Jump back a word with b', check: (s) => s.actions.includes('word-back') },
{ desc: 'Go to line start with 0', check: (s) => s.actions.includes('line-start') },
{ desc: 'Go to line end with $', check: (s) => s.actions.includes('line-end') },
{ desc: 'Go to file top with gg', check: (s) => s.actions.includes('file-top') },
{ desc: 'Go to file bottom with G', check: (s) => s.actions.includes('file-bottom') },
]
},
{
id: 'movement2', section: 'Movement', title: 'Screen & Search',
content: `
<h2>Screen Navigation & Searching</h2>
<p class="subtitle">Moving faster across large files</p>
<h3>Screen Position</h3>
<table>
<tr><th>Key</th><th>Motion</th></tr>
<tr><td>H</td><td>Top of visible screen</td></tr>
<tr><td>M</td><td>Middle of visible screen</td></tr>
<tr><td>L</td><td>Bottom of visible screen</td></tr>
</table>
<h3>Searching</h3>
<table>
<tr><th>Key</th><th>Action</th></tr>
<tr><td>/pattern</td><td>Search forward for pattern</td></tr>
<tr><td>n</td><td>Next match</td></tr>
<tr><td>N</td><td>Previous match</td></tr>
<tr><td>*</td><td>Search word under cursor</td></tr>
<tr><td>f{char}</td><td>Jump to next {char} on line</td></tr>
<tr><td>t{char}</td><td>Jump to just before {char}</td></tr>
<tr><td>;</td><td>Repeat last f/t forward</td></tr>
<tr><td>,</td><td>Repeat last f/t backward</td></tr>
</table>
<div class="try-it"><strong>Practice:</strong> Try <span class="kbd">f</span> followed by a character to jump to it. Use <span class="kbd">;</span> to repeat the jump. Try <span class="kbd">/</span> to search — type a word, press Enter, then <span class="kbd">n</span>/<span class="kbd">N</span> to navigate matches.</div>
`,
buffer: [
'Searching is one of Vim\'s superpowers.',
'Use /pattern to search forward in the file.',
'Press n to go to the next match.',
'Press N to go to the previous match.',
'',
'The f and t commands search within a line:',
'f( jumps to the next open parenthesis (like this)',
't) jumps to just before the close parenthesis)',
'',
'Try searching for "the" using /the',
'Then use n and N to cycle through matches.',
'',
'The semicolon ; repeats the last f/t jump.',
],
exercises: [
{ desc: 'Use f to jump to a character', check: (s) => s.actions.includes('f-jump') },
{ desc: 'Start a search with /', check: (s) => s.actions.includes('search-start') },
]
},
{
id: 'language', section: 'Vim Language', title: 'Verbs + Nouns',
content: `
<h2>Vim as a Language</h2>
<p class="subtitle">The most important concept in Vim</p>
<p>Vim commands have a grammar: <strong>Verb + Modifier + Noun</strong>. Once you learn the parts, you can compose any command.</p>
<h3>Verbs (Actions)</h3>
<table>
<tr><th>Key</th><th>Action</th></tr>
<tr><td>d</td><td>Delete</td></tr>
<tr><td>c</td><td>Change (delete + enter Insert)</td></tr>
<tr><td>y</td><td>Yank (copy)</td></tr>
<tr><td>v</td><td>Visually select</td></tr>
</table>
<h3>Modifiers</h3>
<table>
<tr><th>Key</th><th>Meaning</th></tr>
<tr><td>i</td><td>Inside</td></tr>
<tr><td>a</td><td>Around</td></tr>
<tr><td>2, 3...</td><td>Count</td></tr>
<tr><td>t</td><td>Till (stop before)</td></tr>
<tr><td>f</td><td>Find (land on)</td></tr>
</table>
<h3>Nouns (Text Objects)</h3>
<table>
<tr><th>Key</th><th>Object</th></tr>
<tr><td>w</td><td>Word</td></tr>
<tr><td>s</td><td>Sentence</td></tr>
<tr><td>p</td><td>Paragraph</td></tr>
<tr><td>b or (</td><td>Block (parentheses)</td></tr>
<tr><td>t</td><td>Tag</td></tr>
<tr><td>" ' \`</td><td>Quoted strings</td></tr>
</table>
<h3>Composing Commands</h3>
<pre>dw → delete word (verb=d, noun=w)
d2w → delete two words (verb=d, count=2, noun=w)
ciw → change inside word (verb=c, mod=i, noun=w)
yip → yank inside para (verb=y, mod=i, noun=p)
dt. → delete till period (verb=d, mod=t, noun=.)</pre>
<div class="try-it"><strong>Practice:</strong> Try <span class="kbd">dw</span> to delete a word. Then <span class="kbd">dd</span> to delete a line. Use <span class="kbd">u</span> to undo! Try <span class="kbd">x</span> to delete single characters.</div>
`,
buffer: [
'Practice the Vim language here!',
'',
'DELETE these unnecessary words from this sentence.',
'Try dw on the word DELETE above.',
'',
'This entire line should be removed with dd.',
'',
'Change this WORD using ciw then type a replacement.',
'',
'Remember: u to undo any mistakes!',
'Delete till the period in this.sentence.with.dots',
],
exercises: [
{ desc: 'Delete a word with dw', check: (s) => s.actions.includes('delete-word') },
{ desc: 'Delete a line with dd', check: (s) => s.actions.includes('delete-line') },
{ desc: 'Undo with u', check: (s) => s.actions.includes('undo') },
]
},
{
id: 'insert', section: 'Editing', title: 'Inserting Text',
content: `
<h2>Inserting Text</h2>
<p class="subtitle">Six ways to enter Insert mode</p>
<p>There isn't just one way to start typing. Each entry point positions your cursor differently:</p>
<table>
<tr><th>Key</th><th>Action</th></tr>
<tr><td>i</td><td>Insert before cursor</td></tr>
<tr><td>a</td><td>Append after cursor</td></tr>
<tr><td>I</td><td>Insert at line beginning</td></tr>
<tr><td>A</td><td>Append at line end</td></tr>
<tr><td>o</td><td>Open new line below</td></tr>
<tr><td>O</td><td>Open new line above</td></tr>
</table>
<h3>Other Insert-Adjacent Commands</h3>
<table>
<tr><th>Key</th><th>Action</th></tr>
<tr><td>r</td><td>Replace single character (stays in Normal)</td></tr>
<tr><td>s</td><td>Substitute character (delete + Insert)</td></tr>
<tr><td>S</td><td>Substitute entire line</td></tr>
<tr><td>C</td><td>Change from cursor to line end</td></tr>
</table>
<div class="try-it"><strong>Practice:</strong> Try <span class="kbd">o</span> to open a new line below and type something. Try <span class="kbd">A</span> to append to the end of a line. Use <span class="kbd">I</span> to insert at the beginning.</div>
`,
buffer: [
'Practice different ways to enter Insert mode.',
'',
'Use A to append text at the end of this line',
'Use I to insert text at the start of this line',
'',
'Use o to open a new line below this one',
'Use O to open a new line above this one',
'',
'Fix this tpyo with r (hover over p, press rY)',
'',
'Each method saves you extra keystrokes!',
],
exercises: [
{ desc: 'Append with a', check: (s) => s.actions.includes('append') },
{ desc: 'Open line below with o', check: (s) => s.actions.includes('open-below') },
{ desc: 'Open line above with O', check: (s) => s.actions.includes('open-above') },
]
},
{
id: 'copydelete', section: 'Editing', title: 'Copy, Delete, Paste',
content: `
<h2>Copy, Delete & Paste</h2>
<p class="subtitle">Moving text around with yank and put</p>
<h3>Yanking (Copying)</h3>
<table>
<tr><th>Key</th><th>Action</th></tr>
<tr><td>yy</td><td>Yank entire line</td></tr>
<tr><td>yw</td><td>Yank word</td></tr>
<tr><td>y$</td><td>Yank to end of line</td></tr>
<tr><td>yiw</td><td>Yank inside word</td></tr>
</table>
<h3>Deleting (also cuts)</h3>
<table>
<tr><th>Key</th><th>Action</th></tr>
<tr><td>dd</td><td>Delete line</td></tr>
<tr><td>dw</td><td>Delete word</td></tr>
<tr><td>x</td><td>Delete character</td></tr>
<tr><td>D</td><td>Delete to end of line</td></tr>
<tr><td>J</td><td>Join line below to current</td></tr>
</table>
<h3>Pasting</h3>
<table>
<tr><th>Key</th><th>Action</th></tr>
<tr><td>p</td><td>Paste after cursor / below line</td></tr>
<tr><td>P</td><td>Paste before cursor / above line</td></tr>
</table>
<div class="tip">Quick trick: <code>ddp</code> swaps two lines — delete the current line, then paste it below!</div>
<div class="try-it"><strong>Practice:</strong> Yank a line with <span class="kbd">yy</span>, move somewhere else, paste with <span class="kbd">p</span>. Try <span class="kbd">dd</span> then <span class="kbd">p</span> to move a line. Use <span class="kbd">x</span> to delete individual characters.</div>
`,
buffer: [
'Line 1: Yank me with yy, then paste with p',
'Line 2: This line is in the wrong position',
'Line 3: Try ddp to swap lines 2 and 3',
'',
'Remove the XXX from this XXX sentence with x',
'',
'Delete this entire line with dd',
'',
'Yank this word and paste it elsewhere.',
],
exercises: [
{ desc: 'Yank a line with yy', check: (s) => s.actions.includes('yank-line') },
{ desc: 'Paste with p', check: (s) => s.actions.includes('paste') },
{ desc: 'Delete a character with x', check: (s) => s.actions.includes('delete-char') },
]
},
{
id: 'visual', section: 'Intermediate', title: 'Visual Mode',
content: `
<h2>Visual Mode</h2>
<p class="subtitle">Select text, then act on it</p>
<p>Visual mode lets you highlight text and apply commands to the selection — like using Shift+arrow keys in other editors, but more powerful.</p>
<h3>Entering Visual Mode</h3>
<table>
<tr><th>Key</th><th>Mode</th></tr>
<tr><td>v</td><td>Character-wise selection</td></tr>
<tr><td>V</td><td>Line-wise selection</td></tr>
<tr><td>Ctrl-v</td><td>Block (column) selection</td></tr>
</table>
<h3>Workflow</h3>
<p>1. Enter Visual mode with <span class="kbd">v</span> or <span class="kbd">V</span></p>
<p>2. Move cursor to expand selection (any motion works)</p>
<p>3. Apply action: <span class="kbd">d</span> delete, <span class="kbd">y</span> yank, <span class="kbd">c</span> change, <span class="kbd">></span> indent</p>
<h3>Smart Selections with Text Objects</h3>
<pre>vi( → select inside parentheses
vi" → select inside quotes
viw → select inside word
vip → select inside paragraph
vi{ → select inside braces</pre>
<div class="try-it"><strong>Practice:</strong> Press <span class="kbd">v</span> to enter Visual mode, move with <span class="kbd">w</span> or <span class="kbd">l</span> to select text, then press <span class="kbd">d</span> to delete the selection or <span class="kbd">y</span> to yank it.</div>
`,
buffer: [
'Select this text using Visual mode.',
'',
'Try v then move with w to select words.',
'Press d to delete the selection.',
'',
'Use V to select entire lines at once.',
'This is great for moving blocks of code.',
'',
'function example(arg1, arg2) {',
' return arg1 + arg2;',
'}',
],
exercises: [
{ desc: 'Enter Visual mode with v', check: (s) => s.actions.includes('enter-visual') },
{ desc: 'Enter Visual Line mode with V', check: (s) => s.actions.includes('enter-visual-line') },
]
},
{
id: 'textobjects', section: 'Intermediate', title: 'Text Objects',
content: `
<h2>Text Objects</h2>
<p class="subtitle">The secret weapon of efficient editing</p>
<p>Text objects let you operate on structured chunks of text <em>from anywhere inside them</em>. You don't need to position your cursor at the start — just be inside the object.</p>
<h3>The Two Modifiers</h3>
<table>
<tr><th>Prefix</th><th>Meaning</th><th>Example</th></tr>
<tr><td>i</td><td>Inside (excludes delimiters)</td><td><code>ci"</code> changes text between quotes</td></tr>
<tr><td>a</td><td>Around (includes delimiters)</td><td><code>da"</code> deletes quotes and content</td></tr>
</table>
<h3>All Text Objects</h3>
<table>
<tr><th>Object</th><th>Inside</th><th>Around</th></tr>
<tr><td>Word</td><td>iw</td><td>aw</td></tr>
<tr><td>Sentence</td><td>is</td><td>as</td></tr>
<tr><td>Paragraph</td><td>ip</td><td>ap</td></tr>
<tr><td>Single quotes</td><td>i'</td><td>a'</td></tr>
<tr><td>Double quotes</td><td>i"</td><td>a"</td></tr>
<tr><td>Parentheses</td><td>i( or ib</td><td>a( or ab</td></tr>
<tr><td>Brackets</td><td>i[</td><td>a[</td></tr>
<tr><td>Braces</td><td>i{ or iB</td><td>a{ or aB</td></tr>
<tr><td>Tags</td><td>it</td><td>at</td></tr>
</table>
<h3>Real-World Examples</h3>
<pre>ciw → change word (cursor anywhere in word)
di( → delete contents of parentheses
ya" → yank string including quotes
ci{ → change inside braces (great for functions)
dap → delete entire paragraph including blank lines</pre>
<div class="try-it"><strong>Practice:</strong> Move your cursor inside the quoted strings or parentheses below, then try commands like <span class="kbd">diw</span> to delete a word or <span class="kbd">dd</span> on a line. Use <span class="kbd">u</span> to undo!</div>
`,
buffer: [
'Text objects work from ANYWHERE inside them.',
'',
'Change the word "placeholder" using ciw.',
'Delete this (parenthesized content) with di(',
'',
'const name = "change this string";',
'const items = [delete, these, brackets];',
'',
'function greet(name) {',
' return "Hello, " + name;',
'}',
'',
'Text objects are the key to Level 3 mastery.',
],
exercises: [
{ desc: 'Delete a word with diw or daw', check: (s) => s.actions.includes('delete-word') },
{ desc: 'Delete a line with dd', check: (s) => s.actions.includes('delete-line') },
{ desc: 'Undo with u', check: (s) => s.actions.includes('undo') },
]
},
{
id: 'dotrepeat', section: 'Intermediate', title: 'Dot & Macros',
content: `
<h2>The Dot Command & Macros</h2>
<p class="subtitle">Do it once, repeat it everywhere</p>
<h3>The Dot Command</h3>
<p>The <span class="kbd">.</span> key repeats your last change. This is incredibly powerful when combined with search:</p>
<pre># Workflow: append semicolons to multiple lines
A;⟨Esc⟩ → append ; to current line
j. → move down, repeat
j. → again
j. → again</pre>
<pre># Workflow: change a word everywhere
/oldname → search for it
ciw → change the word, type replacement, Esc
n. → next match, repeat the change
n. → next match, repeat again</pre>
<h3>Macros</h3>
<p>For more complex multi-step edits, record a macro:</p>
<table>
<tr><th>Key</th><th>Action</th></tr>
<tr><td>qa</td><td>Start recording macro into register "a"</td></tr>
<tr><td>q</td><td>Stop recording</td></tr>
<tr><td>@a</td><td>Play back macro "a"</td></tr>
<tr><td>@@</td><td>Repeat last played macro</td></tr>
<tr><td>5@a</td><td>Play macro 5 times</td></tr>
</table>
<div class="tip">Think of macros as "recordable dot commands" — they capture an entire sequence of actions, not just the last one.</div>
<div class="try-it"><strong>Practice:</strong> Delete a word with <span class="kbd">dw</span>, then move to another word and press <span class="kbd">.</span> to repeat the deletion. Try <span class="kbd">x</span> on a character then <span class="kbd">.</span> to keep deleting characters.</div>
`,
buffer: [
'The dot command repeats your last change.',
'',
'DELETE all the CAPS words in these lines:',
'This REMOVE sentence HAS extra WORDS in it.',
'Another LINE with UNNECESSARY words SCATTERED.',
'',
'Strategy: dw on first CAPS word, then',
'navigate to each one and press . to repeat!',
'',
'Macros are for multi-step repeatable edits.',
'Record with qa, stop with q, play with @a.',
],
exercises: [
{ desc: 'Make a change (dw, x, or similar)', check: (s) => s.actions.includes('delete-word') || s.actions.includes('delete-char') },
{ desc: 'Repeat with . (dot)', check: (s) => s.actions.includes('dot-repeat') },
]
},
{
id: 'registers', section: 'Advanced', title: 'Registers',
content: `
<h2>Registers</h2>
<p class="subtitle">Vim's multi-clipboard system</p>
<p>Every yank and delete goes into a register. You have access to 26+ named clipboards.</p>
<h3>Key Registers</h3>
<table>
<tr><th>Register</th><th>Contents</th></tr>
<tr><td>""</td><td>Unnamed — last yank or delete</td></tr>
<tr><td>"0</td><td>Last yank specifically</td></tr>
<tr><td>"a - "z</td><td>Named registers (you control)</td></tr>
<tr><td>"+</td><td>System clipboard</td></tr>
<tr><td>"_</td><td>Black hole (delete without storing)</td></tr>
</table>
<h3>Usage</h3>
<pre>"ayy → yank line into register a
"ap → paste from register a
"+yy → yank line to system clipboard
"+p → paste from system clipboard
"_dd → delete line without clobbering registers</pre>
<p>View all registers with <code>:registers</code> or <code>:reg</code></p>
<div class="tip">Common pattern: yank important text into "a, then freely delete other text without losing your yank. Paste from "a when ready.</div>
`,
buffer: [
'Registers give you multiple clipboards.',
'',
'Yank this line into register a with "ayy',
'Then delete other lines freely with dd.',
'Paste from register a with "ap',
'',
'The "0 register always holds your last yank,',
'even if you delete something afterward.',
'',
'The "_ black hole register discards text.',
'"_dd deletes without storing anywhere.',
],
exercises: [
{ desc: 'Yank a line with yy', check: (s) => s.actions.includes('yank-line') },
{ desc: 'Paste with p', check: (s) => s.actions.includes('paste') },
]
},
{
id: 'substitution', section: 'Advanced', title: 'Find & Replace',
content: `
<h2>Substitution (Find & Replace)</h2>
<p class="subtitle">Powerful pattern-based text replacement</p>
<h3>The Substitute Command</h3>
<pre>:s/old/new/ → replace first on current line
:s/old/new/g → replace ALL on current line
:%s/old/new/g → replace ALL in entire file
:%s/old/new/gc → replace ALL, asking confirmation each time</pre>
<h3>Anatomy</h3>
<pre>:%s/old/new/g
│ │ │ │ │
│ │ │ │ └─ g = global (all occurrences per line)
│ │ │ └──── replacement string
│ │ └──────── search pattern
│ └─────────── s = substitute command
└───────────── % = entire file (omit for current line only)</pre>
<h3>Useful Patterns</h3>
<pre>:%s/\\s\\+$// → remove trailing whitespace
:%s/foo/bar/gi → case-insensitive replace
:%s/\\r//g → remove Windows line endings
:5,10s/old/new/g → replace only on lines 5-10</pre>
<div class="tip">The substitute command supports full regular expressions. Use <code>\\(group\\)</code> for capture groups and <code>\\1</code> for backreferences.</div>
`,
buffer: [
'Substitution is Vim\'s find-and-replace.',
'',
'foo foo foo - replace all foo with bar',
'Use :%s/foo/bar/g for the whole file.',
'',
'Trailing whitespace ',
'Can be removed with ',
':%s/\\s\\+$// ',
'',
'Lines 5-10 only: :5,10s/old/new/g',
],
exercises: []
},
{
id: 'config', section: 'Advanced', title: 'Configuration',
content: `
<h2>Configuring Vim</h2>
<p class="subtitle">Essential .vimrc settings</p>
<p>Your <code>~/.vimrc</code> file customizes Vim. Here are the most impactful settings:</p>
<h3>Essential Settings</h3>
<pre>syntax on " syntax highlighting
set number " line numbers
set relativenumber " relative line numbers (great for motions!)
set noswapfile " no .swp files
set hlsearch " highlight search matches
set ignorecase " case-insensitive search
set smartcase " ...unless you use capitals
set incsearch " show matches as you type
set expandtab " spaces instead of tabs
set tabstop=2 " tab = 2 spaces
set shiftwidth=2 " indent = 2 spaces
set autoindent " maintain indent level</pre>
<h3>Key Remaps</h3>
<pre>" Escape from Insert mode with jk
inoremap jk <Esc>
" Leader key (space is popular)
let mapleader = " "
" Quick save
nnoremap <leader>w :w<CR>
" Clear search highlighting
nnoremap <leader>h :nohlsearch<CR></pre>
<h3>Remap CAPSLOCK</h3>
<p>Remap CAPSLOCK to Ctrl at the <strong>OS level</strong>. Your left pinky is already there — make Ctrl effortless:</p>
<ul>
<li><strong>macOS:</strong> System Settings → Keyboard → Modifier Keys</li>
<li><strong>Linux:</strong> <code>setxkbmap -option caps:ctrl_modifier</code></li>
</ul>
<div class="tip">Relative line numbers (<code>set relativenumber</code>) show how many lines away each line is — making commands like <code>5j</code> or <code>d3k</code> intuitive!</div>
`,
buffer: [
'# ~/.vimrc',
'',
'syntax on',
'set number',
'set relativenumber',
'set noswapfile',
'set hlsearch',
'set ignorecase',
'set smartcase',
'set incsearch',
'',
'" Remap Escape',
'inoremap jk <Esc>',
'let mapleader = " "',
],
exercises: []
},
{
id: 'cheatsheet', section: 'Reference', title: 'Cheat Sheet',
content: `
<h2>Vim Cheat Sheet</h2>
<p class="subtitle">Quick reference for everything you've learned</p>
<h3>Movement</h3>
<table>
<tr><td>h j k l</td><td>Left, Down, Up, Right</td></tr>
<tr><td>w b e</td><td>Word forward, back, end</td></tr>
<tr><td>0 $ ^</td><td>Line start, end, first non-blank</td></tr>
<tr><td>gg G</td><td>File top, bottom</td></tr>
<tr><td>{ }</td><td>Paragraph up, down</td></tr>
<tr><td>H M L</td><td>Screen top, middle, bottom</td></tr>
<tr><td>Ctrl-u/d</td><td>Half page up, down</td></tr>
<tr><td>f/t + char</td><td>Jump to/before char on line</td></tr>
<tr><td>/ + pattern</td><td>Search, n/N for next/prev</td></tr>
</table>
<h3>Editing</h3>
<table>
<tr><td>i a I A o O</td><td>Enter Insert mode (various positions)</td></tr>
<tr><td>d + motion</td><td>Delete (dw, dd, d$, diw, di(, ...)</td></tr>
<tr><td>c + motion</td><td>Change (delete + Insert mode)</td></tr>
<tr><td>y + motion</td><td>Yank (copy)</td></tr>
<tr><td>p P</td><td>Paste after/before</td></tr>
<tr><td>x X</td><td>Delete char forward/back</td></tr>
<tr><td>r</td><td>Replace single character</td></tr>
<tr><td>u Ctrl-r</td><td>Undo / Redo</td></tr>
<tr><td>.</td><td>Repeat last change</td></tr>
<tr><td>J</td><td>Join lines</td></tr>
<tr><td>~ </td><td>Toggle case</td></tr>
<tr><td>>> <<</td><td>Indent / Outdent</td></tr>
</table>
<h3>Visual Mode</h3>
<table>
<tr><td>v V Ctrl-v</td><td>Char, Line, Block selection</td></tr>
<tr><td>vi( vi" viw</td><td>Select inside object</td></tr>
<tr><td>d y c</td><td>Act on selection</td></tr>
</table>
<h3>Files & Buffers</h3>
<table>
<tr><td>:w :q :wq ZZ</td><td>Save, Quit, Save+Quit</td></tr>
<tr><td>:e file</td><td>Open file</td></tr>
<tr><td>:bn :bp</td><td>Next/prev buffer</td></tr>
<tr><td>:split :vsplit</td><td>Horizontal/vertical split</td></tr>
<tr><td>Ctrl-w h/j/k/l</td><td>Navigate splits</td></tr>
</table>
<h3>Search & Replace</h3>
<table>
<tr><td>:%s/old/new/g</td><td>Replace all in file</td></tr>
<tr><td>:%s/old/new/gc</td><td>Replace with confirmation</td></tr>
<tr><td>*</td><td>Search word under cursor</td></tr>
</table>
<p style="margin-top: 24px; color: var(--green); font-size: 16px;"><strong>Congratulations!</strong> You've completed the tutorial. Keep practicing in the simulator below, or open a real terminal and type <code>vimtutor</code> for the built-in Vim tutorial.</p>
<div class="tip" style="margin-top: 24px;">
<strong>Credits:</strong> This interactive tutorial is based on <a href="https://danielmiessler.com/blog/vim" target="_blank" rel="noopener" style="color: var(--blue);">Learn Vim For the Last Time</a> by <a href="https://danielmiessler.com" target="_blank" rel="noopener" style="color: var(--blue);">Daniel Miessler</a>. His original article is an excellent reference — go read it for the full depth, including plugin recommendations and advanced workflows not covered here.<br><br>
<strong>Recommended reading:</strong> <em>Practical Vim</em> by Drew Neil, <a href="https://vimcasts.org" target="_blank" rel="noopener" style="color: var(--blue);">Vimcasts.org</a>, and <a href="https://learnvimscriptthehardway.stevelosh.com" target="_blank" rel="noopener" style="color: var(--blue);">Learn Vimscript the Hard Way</a> by Steve Losh.
</div>
`,
buffer: [
'Congratulations on completing the tutorial!',
'',
'You now know:',
' - Modal editing (Normal, Insert, Visual)',
' - Motions (hjkl, w/b/e, 0/$, gg/G)',
' - The Vim language (verb + modifier + noun)',
' - Text objects (iw, i(, i", ip, ...)',
' - Visual mode selections',
' - The dot command for repetition',
' - Registers and macros',
' - Search and substitution',
'',
'Next steps:',
' 1. Run "vimtutor" in your terminal',
' 2. Read "Practical Vim" by Drew Neil',
' 3. Use Vim for real work — start small',
'',
'Keep practicing here! Try combining',
'everything you have learned.',
],
exercises: []
},
];
// ===== VIM SIMULATOR =====
class VimSim {
constructor(el) {
this.editorEl = el;
this.modeEl = document.getElementById('simMode');
this.cmdEl = document.getElementById('simCmd');
this.posEl = document.getElementById('simPos');
this.feedbackEl = document.getElementById('simFeedback');
this.mode = 'normal'; // normal, insert, visual, visual-line, command
this.lines = [];
this.row = 0; this.col = 0;
this.vRow = 0; this.vCol = 0; // visual anchor
this.pending = ''; // pending operator/motion
this.count = '';
this.lastChange = null;
this.yankBuf = '';
this.yankIsLine = false;
this.undoStack = [];
this.cmdBuf = '';
this.searchPattern = '';
this.lastFT = null; // {type:'f'|'t', char:'x', dir:1|-1}
this.actions = new Set();
this.onAction = null;
this.editorEl.addEventListener('keydown', (e) => this.handleKey(e));
this.editorEl.addEventListener('click', () => this.editorEl.focus());
}
loadBuffer(lines) {
this.lines = lines.map(l => l);
this.row = 0; this.col = 0;
this.mode = 'normal';
this.pending = '';
this.count = '';
this.undoStack = [];
this.actions = new Set();
this.render();
}
saveUndo() {
this.undoStack.push({ lines: this.lines.map(l => l), row: this.row, col: this.col });
if (this.undoStack.length > 50) this.undoStack.shift();
}
clampCursor() {
if (this.row < 0) this.row = 0;
if (this.row >= this.lines.length) this.row = this.lines.length - 1;
const maxCol = this.mode === 'insert' ? this.lines[this.row].length : Math.max(0, this.lines[this.row].length - 1);
if (this.col < 0) this.col = 0;
if (this.col > maxCol) this.col = maxCol;
}
getCount() {
const n = this.count ? parseInt(this.count) : 1;
this.count = '';
return n;
}
feedback(msg, action) {
this.feedbackEl.innerHTML = `<span class="action">${msg}</span>`;
if (action) {
this.actions.add(action);
if (this.onAction) this.onAction();
}
}
wordBoundary(row, col, forward) {
const line = this.lines[row];
if (forward) {
if (col >= line.length - 1) {
if (row < this.lines.length - 1) return { row: row + 1, col: 0 };
return { row, col };
}
let i = col;
const isWord = c => /\w/.test(c);
const startType = isWord(line[i]);
while (i < line.length && (isWord(line[i]) === startType) && !/\s/.test(line[i])) i++;
while (i < line.length && /\s/.test(line[i])) i++;
if (i >= line.length && row < this.lines.length - 1) return { row: row + 1, col: 0 };
return { row, col: Math.min(i, line.length - 1) };
} else {
if (col <= 0) {
if (row > 0) return { row: row - 1, col: Math.max(0, this.lines[row - 1].length - 1) };
return { row, col: 0 };
}
let i = col - 1;
while (i > 0 && /\s/.test(line[i])) i--;
const isWord = c => /\w/.test(c);
const endType = isWord(line[i]);
while (i > 0 && (isWord(line[i - 1]) === endType) && !/\s/.test(line[i - 1])) i--;
return { row, col: i };
}
}
handleKey(e) {
if (e.ctrlKey && e.key === 'r' && this.mode !== 'command') {
// redo — not implemented, just prevent browser refresh
e.preventDefault(); return;
}
// Let browser handle Ctrl+C, Ctrl+V for accessibility
if (e.ctrlKey && ['c','v','a','z'].includes(e.key)) return;
e.preventDefault();
if (this.mode === 'command') {
this.handleCommandMode(e); return;
}
if (this.mode === 'insert') {
this.handleInsertMode(e); return;
}
if (this.mode === 'visual' || this.mode === 'visual-line') {
this.handleVisualMode(e); return;
}
this.handleNormalMode(e);
}
handleInsertMode(e) {
if (e.key === 'Escape') {
this.mode = 'normal';
this.col = Math.max(0, this.col - 1);
this.feedback('Back to NORMAL mode', 'exit-insert');
this.render(); return;
}
if (e.key === 'Backspace') {
if (this.col > 0) {
const line = this.lines[this.row];
this.lines[this.row] = line.slice(0, this.col - 1) + line.slice(this.col);
this.col--;
} else if (this.row > 0) {
this.col = this.lines[this.row - 1].length;
this.lines[this.row - 1] += this.lines[this.row];
this.lines.splice(this.row, 1);
this.row--;
}
this.render(); return;
}
if (e.key === 'Enter') {
const line = this.lines[this.row];
this.lines[this.row] = line.slice(0, this.col);
this.lines.splice(this.row + 1, 0, line.slice(this.col));
this.row++; this.col = 0;
this.render(); return;
}
if (e.key.length === 1) {
const line = this.lines[this.row];
this.lines[this.row] = line.slice(0, this.col) + e.key + line.slice(this.col);
this.col++;
this.actions.add('typed-text');
if (this.onAction) this.onAction();
this.render();
}
}
handleVisualMode(e) {
const key = e.key;
if (key === 'Escape') {
this.mode = 'normal';
this.feedback('Back to NORMAL mode');
this.render(); return;
}
// Movement in visual
const moved = this.applyMotion(key);
if (moved) { this.render(); return; }
// Actions on selection
if (key === 'd' || key === 'x') {
this.saveUndo();
if (this.mode === 'visual-line') {
const start = Math.min(this.vRow, this.row);