-
Notifications
You must be signed in to change notification settings - Fork 1
/
vim.qmd
501 lines (299 loc) · 17.1 KB
/
vim.qmd
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
---
title: "Vim"
---
## What is VIM?
Vim is a keybinding that has a bit of cult following. It will look and feel different than your traditional key bindings (eg what is in front of your hands today) but over time, you will be will be more productive and efficient. Honestly -- it will excite you.
> Getting started is the hardest because its new -- The Primeagen
:::{.callout-note}
## What is a keybinding?
I'll use the word alot but you can think of a keybinding as a combination of keys that are pressed to do an outcome
For example 'SHIFT + y' produces the capital letter 'Y'
'Alt + =' will produce '<-' in Rstudio
Pression 'Ctrl + left arrow' will skip ahead left by one whole word
:::
How will VIM do this?
This will make more sense as we go along. VIM's real secret is not only how is there a tool for every general or specialized use case but also you can access the built in short cuts or quickly create your own to use most of these tools with minimal hand movement.
the ability to "move mountains" while barely lifting your fingers from your keyboards homerow is incredible.
So how can I do more things, quickly, with fewer key strokes and less hand movement?
This is actually due to previous limitations in early computers where they didn't have an up,down,left,right errors or other specialized keys (eg. 'tab', caps lock) so they had to leverage the existing keys to do many things.
VIM's trick in doing this is that it is not just simple text editor but instead it is a modular editor.
This means VIM has different modes (insert,normal, visual, command and replace) -- each mode has a different "interface" which you can think of as surgeon's tool kit that can be used for a focused purpose.
## How to use VIM in RStudio and why?
While VIM (or its alternative flavors) is its own program -- due to popularity and benefits many of VIM's key bindings are available in other editors, including Rstudio and VScode.
> One limitation upfront, specifically in the case of RStudio is that only a selection of VIM's key bindings are available -- quite honestly you are missing a lot of VIM's firepower -- but for a beginner that wants to graduate to intermediate skill set -- its actually a great place to start.
Additionally, everything you learn is 100% portable to a different editor.
## Why learn VIM in RStudio?
Given that you are essentially learning VIM lite -- why go through with this at all? RStudio as its own shortcuts and utilities, what makes this so special for R users?
**Incremental benefits**
- "Mark" sections of a your script, rmarkdown or quarto document and instantly transport there making navigation a breeze
- Quickly record keyboard macros to do repititive edits, deletions or insertions (think captializing first word of a list or replace '.' with '_' in variable's name,etc)
- Search and replace letters or words without using your mouse
- Instantly jump to specific words or characters in a line
- Navigate your document with minimal handmovement -- maximizing your ergnomic benefits
- Highlight words, lines paragarphs with precision
## How do I get started?
Before we enable VIM in RStudio we need to talk about two key modes, how you switch between and interact with these modes
### Insert Mode
This will be the most familiar to you as this mode is basically the interface you are most used to today.
You press the letter "a" and you see the letter "a" printed on the screen. You want to move around, you press up arrow or left arrow,etc.
There is nothing controversial or hard to learn about this mode -- this mode works as you are used to today.
However, believe it or not -- you will try to spend as little time in this mode as possible. That is because this mode should only be used to enter in text -- not for navigation around your document or between paragraphas, not for highlighting words or sections, not for editing or deleting texts -- you get the point.
Getting out of the habit of using insert mode for nothing more than just inserting text will be an initial source of frustration
The real fun starts in what is known as 'normal mode' -- which is the mode you will first start in which is used mainly for navigating around your document, neutral mode that you will transition to other modes from,
::: {.callout-note collapse="true"}
## Transfer from Insert mode to Normal mode
> To get out of insert mode and switch to normal mode you need to press <esc> or <ctrl> +[. (note in real vim you can create new key mappings so that you can use any key combination to to escape mode). I recommend to start with esc but eventually you want to move towards <ctrl> + [ because it **should** be less reaching for your pinky fingers.
:::
When you are in normal mode, at first glance everything will be the same -- with one exception.
your cursor will look like a block rather than a slim line.
That is it.
However when you start typing in normal mode that is when you will start to see differences. If you were to type b,t s or q nothing happens. That is because this mode isn't intended to insert new texts instead it is meant motions and operators.
To move around in this mode you can theoritically still use the arrow keys but honestly the quicker you ditch that habit the better.
Instead you use 'hjk' and 'l' to go 'left', 'down', 'up' and 'right' (respecively)
This is a great place for you to start your transformation to a VIM user. Coincidently it will be the same time when you also pull out your hair in frustration.
You use this mode to navigate around your spread sheet, move,change or delete words and line. To insert new text you need to be in insert mode.
::: {.callout-note collapse="true"}
## Transfer from Normal mode to Insert mode
To go into insert mode there are multiple options depending where in the word, paragraph or line you want the cursur to "jump" to. However in practice you will principlly be suing four letters to do so:
i,a,s or o (or there are capital equivalents)
:::
So lots of new information to you -- lets recap some fundamentals.
## Recap
1. Introduced two of four modes so far and their main pursoes
- insert is to insert text into your file (this will be the most familiar)
- normal mode is to navigate around and between your file, delete, copy and paste words/sentences or paragraphes
2. How to switch between each mode
- Press <esc> or <ctrl> +[. to get out of insert mode to normal mode
- Press i,a,s or o to get out of normal mode into insert mode
What can you do with this so far?
- not alot --but patience my friend -- we are establishing vocabulary and framework.
- now you know enough to enable VIM keybindings in Rstudio
::: {.callout-note collapse="true"}
## How to enable VIM in Rstudio?
1. Go to "Global Options"
2. Select "Code"
3. Under keybindings select "VIM"
:::
In RStudio VIM you will get a light flavor of the keybindings, no support for custom keybinds, a taste of the distinct modes, modest macro support and most search & replace functionality with no plugins from the open source community
# VIM keybindings in Rstudio
let's explore the insert optons in more detail:
'i' will *i*nsert your cursor to the left of your bloc
'a' will *a*ppend your cursor to the right of your bloc
'I' will *I*insert your cursor to the beginning of the line
'A' will *A*ppend your cursor to end of the line
'o' will *o*open a new line below the current line to the beginning
'O' will *O*open a new line above the current line to the beginning
Again once you are in insert mode -- the only way to leave insert mode is to esc
Why all the different ways to go into insert mode?
you want to change your mindset towards minimizing the amount of keystrokes required to write your document.
Being precise with where exactly you want your cursor to go enables you to a avoid using arrow keys to move your cursor
You want to be a surgeron --minimal, light touch and ruthless with your efficiency
To improve your productivity let us go to your favorite mode
(you just don't know it yet) -- normal mode
As stated before you will want to try and stay in normal mode because of how addicting your efficiency wil be.
To move around in normal mode you need to use h,j,k and l. Probably the biggest hurdle to you learning VIM starts right here
but stay with it! the pain you fill now will quickly make up for itself with gloarious macros and ridiculous efficiency with VIM.
### motions
These are the keybindings that help you move across your document, you can move move by character, word, line or paragraph (or multiples of them).
We have already covered how to move your line one character at a time with h,j,k and l. However that will be really inefficient.
If you want sail through your line a useful keybinding is 'w','e', 'b' and 'ge'
This will enable to move you forward one "word" at a time.
Whats the differnce between w and e? w will move you to the beginning of a word whereas e will move you to the end of the word.
At this point -- you won't care about this distinction
Want to move two words you can add a count to it -- eg. '2w' will move you two "words". 5w will move you five words.
Additional include '0' and '$' to move you to the beginning or the end of the sentence (still in normal mode)
#### vertical motions
so how to move down a paragraph
who do move down a a screen
#### horizonal motions
### counts
1,2,3 or 4 are all examples of counts
### motions + counts
### actions
d -- delete
x -- delete character
y -- copy
c -- change
p -- paste
### marks
### Registry
# Additional VIM bindings / Resources
## Resources
- [older book but good starting materials](https://www.iopb.res.in/vimbook-OPL.pdf)
- [dunno](https://vimtricks.com/p/vim-jump-list)
- [pragmatic key strokes -- for intermediates](https://fzheng.me/2018/03/20/practical-vim-getting-around-faster/)
{{< pagebreak >}}
# Actual VIM
## How to use VIM if you are in a windows environment
- Your corporate enviorment may have allow you to have 'Git BASH' which is really helpful! because if you have that you have VIM!
- Alternatively your corporate environmetn may have Mysys
## How to find your .vimrc file
In order to customize your vim file, you will need to have a vim configuration file, often times known as 'vimrc' file, however finding this file can be confusing at time.
The below are some helpful tips you can use run in vim to help find where your vim is sourcing your vimrc file
Note, if your vimrc file doesn't exist you can just create a new one
- :version ▶ tells you where VIM is sourcing the .vimrc file
- :echo $VIM ▶ to see the filepath tha vim is looking for the vimrc
- :source filepath ▶ to source a different vim file
## Setting up VIM first time
Below are helpful commands to set up in your vimrc to help making vim sing
[Set's VIM so that it won't be capitible with Vi]{style="color: grey"}
- :set nocompatible
[Gives each line a number]
- :set number (nu)
[Gives each line a number]
- :set relativenumber (rnu)
[Gives each line a number]
- :set hlsearch ▶ highlight words in a search
[Gives each line a number]
- :set ruler ▶ provides row and column index
[Gives each line a number]
- :set autoindent
[Gives each line a number]
- :set incrsearch == incremental search
[Gives each line a number]
- :set autowrite ▶ automatic save
[Gives each line a number]
- set cursorline ▶ highlight current row
[Gives each line a number]
- set cursorcolumn ▶ highlight current column
[Gives each line a number]
- set showcmd
[Gives each line a number]
- set backspace=indent,eol,start
[Gives each line a number]
- set backup
[Gives each line a number]
- let mapleader = "\<space>" ▶ map leader key
[Gives each line a number]
- nobell=all == turns off visual and audio bells
[Gives each line a number]
:::{.callout-note}
## Leaderkey concept
Leaderkey is a
:::
## Additional set ups
- This will depend on your linux set up
- If you are using a barebones linux setup you may require more set up lets go
## Mappings
### Mappings to decrease repititiveness
- imap jk <esc> ▶ custom escape key (to jk)
- crtl + P ▶ to auto fill from words already present in the sheet
- :xnoremap p pgvy ▶ paste, re-copies previous selection and yanks it again
- :abbreviate shortcut fullword ▶ set custom snippets
- :map <key> actions ▶ maps a key, simliar to a register
- `.` ▶ will repeat the last action
### Shortcuts
- nohl ▶ erases current highlights
- :marks [args] ▶ lists all marks
- ' ▶ the last place the cursor was at
- " ▶ line 1
- [ ▶ start of the last insert
- ] ▶ this end of the insert
- :wnext ▶ save current file and go to nextfile
- :next! ▶ force next
- :first ▶ first file
- :last ▶ last file
- :Next ▶ go to previous
- :args ▶ current file that you are on
- :f ▶ will dispaly current file
- :put=range(start_num,end_num,inc) ▶ put range of numbers in script
This is probably better understood as a mimic of VIM key bindings but even this limited capabilities can signifcantly help your producively
### Key Navigation
- J ▶ joins lines
- :t [row number] ▶ copies to row number
- :m [row_number] ▶ moves to row number
- gUU ▶ upper cases entire line
- g~~ ▶ inverts case of line
- { ▶ up a section
- } ▶ down a section
- :Ex == Open up file explorer
- gj == navigate one visual line
- gk == navigate one visual line
## Patterns to know
### Search
VIM has very powefful search functionality with `/`
- Use the '/' to search for a key word
- if multiple matches you can use 'n' to select the next option.
- or you can use ? to go back to a previous option
### Replace
- :[start_num,end_num]s/new_word/old_word/g ▶ replace a word
- start_num,end_num ▶ can be replaced with %
- s -- to substitute
- g ▶ indicates its a global so if it happens more than once to go replace it
- c ▶ confirm before change
- % or 1,$ ▶ full document
### Popular regex
- $ and ^ around single word will only find those words
## Registers (macros)
- Start recording with q
- Assign a letter to the macro
- Do your key bindings step by step
- q to stop macros
- @letter to call macro
- Can add count to do it muliple times
## Text blocks and multiple files
- Cutting, deleting and yanking a word puts it in the registrar
### Marks
- mark a spot with 'm' and a letter
- to go to a mark use ' and the letter
- Useful to delete until a word d'a or to mark a frequently used section
### Filtering
- ! + abs rows + G + command
- !10Gsort ▶ will sort the ten rows from currnet row to row 10
- !10Grev ▶ will rev the rows from point to line 10
- !! command ▶ to directly enter in a different comand
- !!ls ▶ lists files and folders
- !!pwd ▶ lists current directory
- !!date ▶ lists the current date
## Edit multiple files
- vim file1 file2
- this will open up the first file to switch you need :write and :next or :wnext
- :args ▶will display the current file that you are on
- :f ▶ will display current file
- crtl + ^ will get you
## Buffer Copy
- :[numberOfLines]split [file] ▶ split current file or named file-
- :new ▶ to open up a new buffer
- :bnext ▶ next buffer
- :bprevious ▶ previous buffer
- :vsplit [+/pattern]▶ for visual split to go pattern
- ctrl+Ww ▶ -cycle through buffers
- :hide ▶ hide buffer
- :buffers ▶ list buffers
- :buffer [number] ▶ go to list of buffers
## Visual Basic Mode
- v starts line by line visual mode ▶ can only select line wise at a time
- V highlights the whole line
- vaw ▶ visualize around the word
- ctrl + v enters visual block mode
- ctrl + v + I <insert text> + escape ▶ insert text across multiple lines
- use `o` to toggle between start and ending of visual search
## Digraph
- ctrl + k character character number
- • is ctrl+k oo
- ▶ is cktrl+k RP
## Misc.
- `>>` shifts left `<<` shifts right
- set shiftwidth=number ▶ sets the shift command
- :numrange p ▶ print last lines
- :start_range,/search p ▶ print until a serached word ▶ also works with marks'
- ctrl + v :' ▶ will put range in and you can execute commands from there
- o ▶ to insert space beneath the line
- O ▶ to insert space above the current line
- g ctrl-g == display words, lines and bytes
:25t. -- copies line 25 and pastes it below
:t20 -- copies current line and pastes it below line 20
in insert mode ctrl+r+= and arthemtic operation to add calculator to vim
[copy, move, delete in norm mode](https://jovica.org/posts/vim_advanced_copy/)
t or co = copy
m = move
d = delete
. = current line
start_range,end_rate,motion,line
gi == last insert
gv -- last visual
shift+k over bash command will open man pages
gx -- to open url (must have https://)
g+ ctrl+g =count words, lines,etc
ctrl + a == incremental number
ctrl+ x == decrease number