-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtext-editing.tex
848 lines (781 loc) · 39.5 KB
/
text-editing.tex
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
% Created 2019-03-04 Mon 10:18
% Intended LaTeX compiler: pdflatex
\documentclass[11pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{graphicx}
\usepackage{grffile}
\usepackage{longtable}
\usepackage{wrapfig}
\usepackage{rotating}
\usepackage[normalem]{ulem}
\usepackage{amsmath}
\usepackage{textcomp}
\usepackage{amssymb}
\usepackage{capt-of}
\usepackage{hyperref}
\usepackage{hyperref}
\hypersetup{colorlinks=true, allcolors=blue}
\author{Hassan Shabbir}
\date{\today}
\title{Editing Text With Vim}
\hypersetup{
pdfauthor={Hassan Shabbir},
pdftitle={Editing Text With Vim},
pdfkeywords={},
pdfsubject={},
pdfcreator={Emacs 26.1 (Org mode 9.1.14)},
pdflang={English}}
\begin{document}
\maketitle
\tableofcontents
\newpage
\section{The main text editors}
\label{sec:org8842b2f}
There are two main (extensible) text editors for Unix and GNU/Linux\footnote{I'd just like to interject for a moment. What you usually refer
to as Linux, is in fact, GNU/Linux, or as I've recently taken to calling
it, GNU plus Linux. Linux is not an operating system unto itself, but
rather another free component of a fully functioning GNU system made
useful by the GNU corelibs, shell utilities and vital system components
comprising a full OS as defined by POSIX. (See GNU Linux copypasta.)}
operating systems. These are the VI and Emacs lines of text editors.
Both of these text editors are highly configurable and therefore are highly
recommended by many programmers, so much so that since their inception many
programmers have been in a "war", arguing over which text editor is
better.\footnote{See \href{https://en.wikipedia.org/wiki/Editor\_war}{The Editor Wars}.}
\subsection{VI}
\label{sec:org9994cc5}
VI (pronounced "vee-eye"), and other text editors based off of it are great at
creating and editing text, so much so that you will hear VI users call it a
"language" for editing text. It is (usually) a basic text editor, however, newer
versions of it allow a user to customize it and get IDE-like features within
it.\footnote{This is usually not encouraged, especially at the beginning,
since having plugins hinder your ability to understand Vim, and are nice
to have and not necessarily mandatory for the functioning of Vim. In
fact, for GNU/Linux users, the IDE is the operating system. See
\href{https://sanctum.geek.nz/arabesque/series/unix-as-ide/}{Unix as IDE}.} The main feature of VI is the cohesive "language" it provides for
editing text.
\subsection{GNU Emacs}
\label{sec:org3ce9159}
GNU\footnote{GNU stands for "GNU's Not Unix", a recursive acronym.} Emacs (pronounced "g'noo ee-maks") is a text editor that is extremely
configurable and allows for a lot more IDE-like features. This is due to the
fact that Emacs is a text editor written and extensible in the general purpose
functional programming language Lisp. Emacs can also emulate VI keybindings
using EVIL mode (EVIL stands for Extensible VI Layer). It is also interesting to
note that Richard Stallman,\footnote{\href{https://en.wikipedia.org/wiki/Richard\_Stallman}{Richard Stallman}.} the creator of GNU Emacs, also created the
first alternative to Unix, which was GNU, the Free Software Foundation, as well
as the GNU General Public Licence.
From here on out we will be talking about the VI line of editors. The curious
reader is encouraged to read more about Emacs, if interested.
\section{Which VI version should I use?}
\label{sec:orgb68054b}
The only differences between the different versions of VI are stated below.
There may be more differences, but those won't be important to a beginner.
I will also assume that you will be using Vim or NeoVim for the rest of this
file, but will generally refer to them as Vim.
The three different VI versions you should be familiar with are VI, Vim,
and NeoVim.
\subsection{VI}
\label{sec:org985298e}
VI\footnote{VI pronounced "vee-eye", also pronounced "vy" but that is an
unofficial pronunciation.} is the oldest text editor of the three\footnote{Technically, the "ed" and "ex" editors are even older, but are
quite cumbersome to use. For example, they require you to print a range
of lines to be able to see them.}, which was created in
1976.\footnote{This is where the command mode in VI comes from, see below.
Also, see \href{https://sanctum.geek.nz/arabesque/actually-using-ed/}{Actually Using Ed} for some extreme masochism.} VI is short for Visual, differentiating it from line editors. If
you plan to work with many servers, you should expect literally expect every
server to have it. It is very minimalistic so it won't tell you when you are in
insert mode in any way (see below), for example. This makes it harder to
understand for beginners, and doesn't have all the features of Vim.
\subsection{Vim}
\label{sec:org30e7b7e}
Vim\footnote{Vim pronounced "vim".} is the newer version of VI, first released in 1991. Due to it being
very popular for a long time, and still is (last stable release: 17 May 2018),
it has many resources available on the internet for it. It is also recently
modern, so it shouldn't be too difficult to use. Therefore, whenever searching
for general vim resources use vim in the search terms.
\subsection{NeoVim}
\label{sec:org8bb2574}
NeoVim\footnote{NeoVim pronounced "neo-vim".} is the newest version of vim, first released in 2015. This has the
most interactive features and therefore is the one that I would recommend to new
users. For example, when you switch between different modes, the cursor changes,
helping you to remember which mode you are in.
\noindent\rule{\textwidth}{0.5pt}
\newpage
\section{Installing Vim/NeoVim}
\label{sec:org68fc9d8}
Packages (programs) can easily be installed in any Linux distribution using the
command line. For example, to manage packages on Ubuntu Linux, use the package
manager \texttt{apt}. The full command to install Vim would then be \texttt{sudo apt install
vim}, and the command to install NeoVim would be \texttt{sudo apt install nvim}.\footnote{The command to both run and install it is \texttt{nvim} NOT \texttt{neovim}.}
\section{Modal editing with Vim}
\label{sec:orgfce45e4}
The first thing you need to know about Vim is that there are four main modes in
which you operate. Each of these modes changes what the keys on your keyboard
will do.
In general, when editing text you will mostly be making small changes, and very
rarely do you create whole documents without mistakes from start to finish (\texttt{cat
> foo.txt} anyone?). For this reason, Vim is optimized for modifying text.
Understanding modal editing (along with composability, repeatability, and text
objects) is the key to understanding Vim.\footnote{For more on how vim works see this awesome answer on StackOverflow
\href{https://stackoverflow.com/questions/1218390/what-is-your-most-productive-shortcut-with-vim}{Your problem with Vim is that you don't grok vi} and \href{http://www.viemu.com/a-why-vi-vim.html}{Why, oh WHY, do those \#?@!
nutheads use vi?}.}
\subsection{Normal Mode (Modification; Movement)}
\label{sec:orgd3ea1de}
Normal mode allows you to issue commands to Vim, which will then do something,
other than putting the key that you pressed into the file. When you first open a
file you will be in Normal Mode. Pressing keys such as \texttt{j} or \texttt{l} will move the
cursor rather than adding those letters to the file. You should almost always be
in Normal Mode since editing text requires the ability to move around the
document, and deleting, replacing, copying text, etc. which are all possible in
Normal mode. Thus this mode is called "normal" since it is the default mode when
using Vim. There are three different ways to get to Normal mode: by pressing
the \texttt{Esc} key, \texttt{ctrl-c}, or \texttt{ctrl-[}.\footnote{Most Vim users prefer to remap the Caps Lock key to Escape, for
convenience.}
The two main things you can do from within Normal Mode are either modifying the
text, or moving the cursor around. However, as stated before, Vim gives you
an easy language to talk about changes by combining the modification command
with a motion command. An optional number prefix allows you to repeat the
complete command that many number of times. Therefore, the template of a
complete Vim command is \texttt{<count> <operator> <motion/text-object>}\footnote{Text objects will be covered much later.}.
Some examples of the Vim text editing language are listed below, in English:
\begin{itemize}
\item Delete the default amount (uses <operator>)
\item Move to the end of the line (uses <motion>)
\item Delete to the end of the line (uses both <operator> and <motion>)
\item Delete the current paragraph (uses both <operator> and <text-object)
\item Paste the default amount 10 times (uses both <count> and <operator>)
\item Paste the current paragraph 10 times (uses <count>, <operator> and <text-object>)
\end{itemize}
\subsection{Insert Mode (Add Text)}
\label{sec:orgea49134}
When opening a document with Vim, you will be in Normal Mode. To get into Insert
Mode, for example, you can press keys such as \texttt{i} or \texttt{a} and then you will be in
Insert Mode. If you are using NeoVim, you will see the cursor become thin, and
in both Vim and NeoVim, you will see \texttt{-{}-INSERT-{}-} at the bottom of the
terminal.\footnote{In VI you will neither see the cursor change nor the
\texttt{-{}-INSERT-{}-} at the bottom.} You can then use the arrow keys to get to the location and press
the keys to add them to the document. To get back to Normal Mode press escape.
(This is not recommended but can help you get used to Vim. Movement commands
should be done using Normal Mode, not the arrow keys or the mouse, allowing your
hand to stay on home row\footnote{See \href{https://www.youtube.com/watch?v=tQCRVkSFFEc}{Why Vim doesn't need the mouse} for more.}.) You will notice that the cursor will become a block
again in NeoVim.
\subsection{Command Mode (System Commands; Ed commands)}
\label{sec:org038b4cc}
For now, the most important command mode you need to know will be the commands
to exit Vim (which is accessible from Command Mode). This is such a problem for
Vim beginners that \href{https://stackoverflow.com/questions/11828270/how-to-exit-the-vim-editor}{this StackOverflow answer} has 4,000 upvotes, and around 1.8
million views. First enter normal mode by pressing the escape key, then press
\texttt{:}. You will now see a colon on the last line of the terminal. If you wish to
save your changes type \texttt{wq}, and then press enter. This command stands for write
(save) the file then quit Vim. If you wish to throw away your changes type \texttt{q!}
instead and then press enter.
To get a deeper understanding of how Vim deals with files and why it has many
different commands for exiting Vim, let's understand how Vim edits files. When
you are in the terminal, the file you wish to edit resides in your hard drive.
Then when you open the file in Vim, a copy of the file is put into memory for
easy access. You then modify the file using Vim. If you try to close Vim now, it
will be unsure if you were sure you wished to discard the changes or if you
forgot you made changes. At this point, you can either tell it that you wish to
discard the changes (\texttt{:q!}), or you forgot to save the changes (\texttt{:wq}). Once you
choose to save the file the copy of the file that was in memory will then
replace the version of the file on the hard drive.
\subsection{Visual Mode (Select Text)}
\label{sec:org5c03edb}
Visual Mode is used for performing an operation over all of the characters in
the text. This can be useful when you don't know how to run operations using
text objects. Text objects allow you to refer to regions of text, such as "in
braces", "in tag", "all paragraph", etc. Text objects will replace most simple
uses of Visual Mode.
\begin{figure}[htbp]
\centering
\includegraphics[width=.9\linewidth]{./modes.jpg}
\caption{\label{fig:org8c828b4}
General overview of Vim Modes. Will be covered in depth later.}
\end{figure}
\section{Vim editing commands}
\label{sec:org6d1b628}
\subsection{Before learning more Vim commands}
\label{sec:orgb0200da}
Before continuing to learn more Vim commands, there are a few general things
to cover.
Firstly, there will be a few different modes used below. If you are
ever unsure of which mode you're in, you can always press the escape key to
get you back into Normal Mode, from which you can run another command or
go into another mode.
Secondly, commands in Vim are quite mnemonic and cohesive. This means that you
can easily learn the key associated with a command by remembering what it stands
for. An example of this is the \texttt{d} command which stands for delete. Also,
commands that are related, but do something different are capitalized. For
example, \texttt{D} deletes to the end of the line. Finally, the default action is defined by
repeating the key for the command. For example, \texttt{dd} deletes the line, which is
the default action for the delete command.
Third, instead of making your own notes of this document, you can use VI/Vim
cheat sheets, such as the following cheat sheet.
\begin{figure}[htbp]
\centering
\includegraphics[width=.9\linewidth]{./vi-vim-cheat-sheet.png}
\caption{\label{fig:orgeaf0f98}
A cheat sheet for many Vim commands explained below. (Don't get overwhelmed by all of the commands!)}
\end{figure}
Finally, the headings will help you to organize the information you learn into
compartments. The headings will be in the form \texttt{CommandName (from StartingMode)},
with subheadings dividing the topic into \texttt{beginner}\footnote{The 'Beginner' subheading will show the commands that I personally
think a new Vim user should learn. Also, you only need to learn the commands
that you think are necessary for yourself, coming back later to deepen your
knowledge of Vim.} and \texttt{intermediate} commands.
The commands themselves will be in the form \texttt{COMMAND: (mnemonic device)
Description of command}.
\subsection{Entering NeoVim (from bash prompt)}
\label{sec:org3e2fa4c}
You can enter NeoVim from the command line (not to be confused with Vim's
Command Mode) by typing \texttt{nvim file.txt}\footnote{You can also type multiple files after the command and switch
between them using \texttt{:bn} (buffer next) and \texttt{:bp} (buffer previous). A buffer can
be thought of as a file that Vim has open for you, that you are currently not
editing.}, replacing \texttt{file.txt} for the file you
want to edit. If the file doesn't exist, it will be created. You will now be in
NeoVim.
If you wish to use Vim, replace \texttt{nvim} in the command above with \texttt{vim}.
\subsection{Movement Commands (from Normal Mode)}
\label{sec:orgfa22d8d}
\begin{itemize}
\item \href{https://www.youtube.com/watch?v=tQCRVkSFFEc}{Why Vim doesn't need the mouse}
\end{itemize}
\subsubsection{Character Movement}
\label{sec:orgbe3b410}
\begin{enumerate}
\item Beginner
\label{sec:org876e037}
\begin{itemize}
\item \texttt{h}: Move cursor left
\item \texttt{j}: Move cursor down
\item \texttt{k}: Move cursor up
\item \texttt{l}: Move cursor right
\end{itemize}
The way to remember this is that the \texttt{h} key is on the left of the four keys,
\texttt{l} is on the right, \texttt{j} is written with the hook below the line, and \texttt{k} has
the vertical line above the line.
Character movement can also be prefixed with a number such as \texttt{5l}, to go 5
characters right.
\begin{figure}[htbp]
\centering
\includegraphics[width=.9\linewidth]{./hjkl.png}
\caption{\label{fig:orgde825b9}
A graphical depiction of h, j, k, l}
\end{figure}
\end{enumerate}
\subsubsection{Line Movement}
\label{sec:orgc0ae1f2}
\begin{enumerate}
\item Beginner
\label{sec:org67b2ee7}
\begin{itemize}
\item \texttt{\textasciicircum{}}: (This is from Regexes\footnote{Regexes, or regular expressions, are a way of doing things like
parsing and substituting in a file. The regex \texttt{\textasciicircum{}hi} says to match the
line starting with \texttt{hi} and the regex \texttt{\textasciicircum{}\$} says match the empty line
(ie. the line that starts and ends with nothing in between).}) Go to start of line
\item \texttt{\$}: (This is from Regexes) Go to end of line
\end{itemize}
\end{enumerate}
\subsubsection{File Movement}
\label{sec:orgd1d1f8c}
\begin{enumerate}
\item Beginner
\label{sec:orgae4b41d}
\begin{itemize}
\item \texttt{gg}: (Go) Go to start of file
\item \texttt{G}: (Go) Go to end of file
\end{itemize}
\item Intermediate
\label{sec:org197d9e3}
\begin{itemize}
\item \texttt{50gg}: (Go) Go to line 50
\end{itemize}
\end{enumerate}
\subsubsection{Word Movement}
\label{sec:org895fc42}
\begin{enumerate}
\item Intermediate
\label{sec:orgddcd561}
Frankly, I used to just spam \texttt{h} and \texttt{l} for quite a while, so these commands
aren't strictly necessary.
\begin{itemize}
\item \texttt{w}: (Word) Go forward by one word
\item \texttt{b}: (Back) Go back by one word
\item \texttt{e}: (End) Go to the next end-of-word
\end{itemize}
\end{enumerate}
\subsubsection{Find Char Movement}
\label{sec:orgf75845b}
\begin{enumerate}
\item Beginner
\label{sec:org0e86b46}
\begin{itemize}
\item \texttt{fx}: (Find Char) Find character 'x' forwards
\item \texttt{;}: Run \texttt{f} / \texttt{F} again
\end{itemize}
\item Intermediate
\label{sec:orgfbf7017}
\begin{itemize}
\item \texttt{Fx}: (Find Char) Find character 'x' backwards
\item \texttt{,}: Run \texttt{f} / \texttt{F} again in opposite direction
\item \texttt{tx}: ('Til/Until) Go up until character 'x', forwards
\item \texttt{Tx}: ('Til/Until) Go up until character 'x', backwards
\end{itemize}
\end{enumerate}
\subsubsection{Search Term Movement}
\label{sec:org59c3c79}
\begin{enumerate}
\item Beginner
\label{sec:org4a645c8}
\begin{itemize}
\item \texttt{/}: (Search\footnote{Similar to regexes and its uses in awk commands, etc.}) Input search term then press enter, searches forward
\item \texttt{n}: (Next, same direction) Go to next location matching search term
\end{itemize}
\item Intermediate
\label{sec:org76792da}
\begin{itemize}
\item \texttt{N}: (Next, opposite direction) Go to previous location matching search term
\item \texttt{?}: Input search term then press enter, searches backward
\end{itemize}
\end{enumerate}
\subsection{Insert Commands (from Normal Mode)}
\label{sec:orgab87128}
These commands will change you automatically from Normal Mode to Insert Mode.
\subsubsection{Beginner}
\label{sec:org8cca391}
\begin{itemize}
\item \texttt{i}: (Insert) Enter Insert Mode before current character
\item \texttt{I}: (Insert) Enter Insert Mode at the beginning of the line
\item \texttt{a}: (Append) Enter Insert Mode after current character
\item \texttt{A}: (Append) Enter Insert Mode at the end of the line
\end{itemize}
\subsubsection{Intermediate}
\label{sec:orgb60cd62}
\begin{itemize}
\item \texttt{o}: (Open) Add new line below and go into insert mode
\item \texttt{O}: (Open) Add new line above and go into insert mode
\end{itemize}
\subsection{Deletion Commands (from Normal Mode)}
\label{sec:org8563a4f}
NOTE: The composable nature of Vim should be apparent in this section.\footnote{See \href{https://www.youtube.com/watch?v=wlR5gYd6um0}{Mastering the Vim language} for more.}
\subsubsection{Beginner}
\label{sec:org562ca01}
\begin{itemize}
\item \texttt{x}: Delete character under cursor
\item \texttt{dd}: (Delete, Default) Delete current line
\item \texttt{dw}: (Delete Word) Delete until the end of the word
\item \texttt{dfc}: (Delete Find 'c') Delete including the first 'c' on the right of the cursor
\item \texttt{diw}: (Delete In Word) Delete the whole word
\item \texttt{diW}: (Delete In Word) Delete the whole space delimited word
\end{itemize}
\subsubsection{Intermediate}
\label{sec:orgde4ccbf}
I can't really be bothered to count how many words I want to delete. I prefer
doing things like \texttt{dw..} instead, see below.
\begin{itemize}
\item \texttt{d3w}: (Delete Word) Delete 3 number of words, etc.
\end{itemize}
\subsection{Deletion Commands (from Visual Mode)}
\label{sec:org105f03a}
\subsubsection{Beginner}
\label{sec:org031e8b1}
\begin{itemize}
\item \texttt{d}: (Delete) Delete current visual selection
\item \texttt{x}: (Delete) Delete current visual selection
\end{itemize}
\subsection{Change Commands (from Normal Mode)}
\label{sec:org610786d}
Change deletes something then puts you in Insert Mode to add text.
\subsubsection{Beginner}
\label{sec:orgb14b967}
\begin{itemize}
\item \texttt{cc}: (Change, Default) Delete line, then go into Insert Mode
\item \texttt{cw}: (Change Word) Delete until the end of the word, then go into Insert Mode
\item \texttt{ciw}: (Change In Word) Delete the whole word, then go into Insert Mode
\item \texttt{ciW}: (Change In Word) Delete the whole space delimited word, then go into Insert Mode
\end{itemize}
\subsubsection{Intermediate}
\label{sec:org599df6d}
I can't really be bothered to count how many words I want to change. I prefer
doing things like \texttt{cw..} instead, see below.
\begin{itemize}
\item \texttt{c3w}: (Change Word) Delete 3 number of words, etc., then go into Insert Mode
\end{itemize}
\subsection{Misc. Normal Commands (from Normal Mode)}
\label{sec:org1432392}
These commands change the file while keeping you in Normal Mode.
\begin{itemize}
\item \texttt{>>}: (Indent) Indent the current line
\item \texttt{>ip}: (Indent In Paragraph) Indent the current paragraph
\item \texttt{<<}: (Outdent) Outdent the current line
\item \texttt{<ip}: (Outdent In Paragraph) Outdent the current paragraph
\end{itemize}
\subsection{Yank (Copy) Commands (from Normal Mode)}
\label{sec:orgdfe0cb0}
NOTE: To copy text to use in other applications, use the \texttt{"+} prefix, which may
not work in VI/Vim, also see registers below.
\subsubsection{Beginner}
\label{sec:org770720c}
\begin{itemize}
\item \texttt{yy}: (Yank, Default) Yank (copy) the current line, for Vim use only
\item \texttt{yiw}: (Yank) Yank (copy) the current line, for Vim use only
\item \texttt{"+yy}: (Yank, Default) Yank (copy) the current line, for any application
\item \texttt{"+yiw}: (Yank) Yank (copy) the current line, for any application
\end{itemize}
\subsection{Yank (Copy) Commands (from Visual Mode)}
\label{sec:orge25efd1}
\subsubsection{Beginner}
\label{sec:orge326b86}
\begin{itemize}
\item \texttt{y}: (Yank) Yank (copy) current visual selection, for Vim use only
\item \texttt{"+y}: (Yank) Yank (copy) the current selection, for any application
\end{itemize}
\subsection{Paste Commands (from Normal Mode)}
\label{sec:org6819a8a}
\subsubsection{Beginner}
\label{sec:org2b9e6d9}
\begin{itemize}
\item \texttt{p}: (Paste) Paste the last deletion/yank, from Vim only
\item \texttt{"+p}: (Paste) Paste the last copied text, from any application
\end{itemize}
\subsection{Paste Commands (from Visual Mode)}
\label{sec:org7afe984}
\subsubsection{Beginner}
\label{sec:org96b5300}
\begin{itemize}
\item \texttt{p}: (Paste) Paste, replacing current visual selection
\end{itemize}
\subsection{Undo Command (from Normal Mode)}
\label{sec:orgab943ea}
\subsubsection{Beginner}
\label{sec:orgc4bdde9}
\begin{itemize}
\item \texttt{u}: (Undo) Undo last (atomic) change
\end{itemize}
\subsection{Visual Mode Commands (from Normal Mode)}
\label{sec:org4facaf4}
First, enter Visual Mode using any of the below, then make the selection using
the movement commands as you would from Normal Mode. Then run the command on the
selection, such as yank, delete, etc.
\subsubsection{Beginner}
\label{sec:org13c1009}
\begin{itemize}
\item \texttt{v}: (Visual) Enter character-wise Visual Mode
\item \texttt{V}: (Visual) Enter line-wise Visual Mode
\end{itemize}
\subsubsection{Intermediate}
\label{sec:orgcbdebcf}
\begin{itemize}
\item \texttt{ctrl-v}: (Visual) Enter block-wise Visual Mode
\end{itemize}
NOTE: To comment out lines, use block-wise selection with \texttt{ctrl-v}, then press
\texttt{I}, and type the character comment (\texttt{//} for example), and hit escape. It can
also be used as a poor man's version of a macro (see below). Another way would
be to use the Vim Commentary plugin (see below), with the command \texttt{gc}.
\subsection{Command Mode (from Normal Mode)}
\label{sec:orgc8f95dd}
\subsubsection{Beginner}
\label{sec:orgbd4765b}
All of the below can be simplified to just \texttt{:w} and \texttt{:q}\footnote{See the command mode section above for more detail.}, since Vim will warn
you if you try to quit with unsaved changes.
\begin{itemize}
\item \texttt{:w}: (Write) Write the file
\item \texttt{:q}: (Quit) Quit Vim, without having modified the file
\item \texttt{:q!}: (Quit!) Quit Vim, throwing away modifications
\item \texttt{:wq}: (Write-Quit) Write the file, then quit Vim
\item \texttt{:x}: (Exit) Shorthand for \texttt{:wq}
\end{itemize}
\subsubsection{Intermediate}
\label{sec:org8ffac94}
\begin{itemize}
\item \texttt{:! date}: (\texttt{!} is similar to \texttt{|}) Run bash command \texttt{date} and show the result without adding to file
\item \texttt{:r! date}: (Read) Run bash command \texttt{date} and read in the result into the file
\item \texttt{:s/foo/bar/g}\footnote{The text \texttt{foo} can be either a literal string or a regex,
such as \texttt{\textasciicircum{}foo}.}: (Substitute) Substitute 'foo' with 'bar', globally (ie. each occurrence)
\end{itemize}
\subsection{Command Mode (from Visual Mode)}
\label{sec:orgb7e04bd}
Visually select text then enter Command Mode using \texttt{:}. NOTE: you will see
\texttt{:'<,'>}\footnote{So the command will run in the range \texttt{x,y}, and a \texttt{'a} refers to
the mark a, with the \texttt{<} referring to the first and \texttt{>} referring to the
last selection. So all together it says "run the command from the
beginning of the selection to the end of the selection."} instead. This just tells Vim to run the command over the whole
selection.
\subsubsection{Intermediate}
\label{sec:orge2b03f9}
\begin{itemize}
\item \texttt{:'<,'>! wc -l}: Run bash command \texttt{wc -l} on visually selected text, replacing with the result
\end{itemize}
\section{Composability and repeatability}
\label{sec:org69549f1}
This section should introduce you to even more advanced concepts.
\subsection{Text Objects}
\label{sec:org31314fe}
NOTE: All text objects can be used with delete, yank, copy, etc. "In" deletes
the text inside, while "All" deletes quotes, braces, and a single space (so the
spaces around it end up balanced).
\subsubsection{Beginner}
\label{sec:org3ed771f}
\begin{itemize}
\item \texttt{iw}: (In Word)
\item \texttt{aw}: (All Word)
\item \texttt{is}: (In Sentence)
\item \texttt{as}: (All Sentence)
\item \texttt{ip}: (In Paragraph)
\item \texttt{ap}: (All Paragraph)
\item \texttt{i"}: (In Quote)
\item \texttt{a"}: (All Quote)
\item \texttt{i\}}: (In Brace)
\item \texttt{a\}}: (All Brace)
\item \texttt{it}: (In Tag) Used in HTML
\item \texttt{at}: (All Tag) Used in HTML
\end{itemize}
\subsection{Dot (\texttt{.}) command}
\label{sec:org398ae1f}
\subsubsection{Beginner}
\label{sec:orgbcd70b1}
The dot command repeats the last complete command that you ran. For example if
you changed a word to "Hi" using \texttt{ciwHi} and then escape, you can change another
word to "Hi" using the dot command.
Expanding on the above, one way\footnote{The other way would be to run a search and replace, such as
\texttt{:s/foo/bar/g}, which would replace all occurrences of \texttt{foo} with \texttt{bar}.} to quickly rename variables would be to
first search for a variable using \texttt{/}, then using \texttt{ciw} to change the variable
to something else. Finally, repeat this change all throughout the document using
\texttt{n} to go to the next instance, and \texttt{.} to apply the change.
\subsection{Number Prefixes}
\label{sec:org9c30c80}
\subsubsection{Intermediate}
\label{sec:org484b13e}
Most commands can be prefixed, meaning you can run commands like \texttt{d5w} which
will delete the next 5 words.
\subsection{Macros}
\label{sec:org0300354}
\subsubsection{Intermediate}
\label{sec:orgb394d34}
Macros can be used for creating groups of repeatable commands. In other words,
start the macro, run general commands (ie. \texttt{w} rather than \texttt{llllllll}), stop the
macro, run the macro previously defined on the text you want. The steps are:
\begin{enumerate}
\item \texttt{qa}: Record Macro in register \texttt{a}, see below
\item \texttt{q}: While recording, it will end the macro
\item \texttt{@a}: Run Macro in register \texttt{a}
\end{enumerate}
Fun fact: you can also define recursive\footnote{Here's a quick introduction to recursion. Recursion is defining
something in terms of what you are defining. For example, a directory
can contain multiple files and multiple directories. A math example
would be an equation like \texttt{x = 1 + x}, and replacing x on the right with
\texttt{1 + x} giving us \texttt{x = 1 + 1 + x}, and continuing to infinity would give
us \texttt{x = 1 + 1 + 1 + 1 + ...}. A similar expansion can be carried out
on the acronym \texttt{GNU}, left as an exercise for the reader.} macros\footnote{See \href{https://sanctum.geek.nz/arabesque/advanced-vim-macros/}{Advanced Vim macros}.}. This
allows you to create a single macro that runs forever (of course, Vim will stop
the macro at the end of the document, for example). An example of this is the
following key sequences: \texttt{qaqqaV\textasciitilde{}j@aq@a}\footnote{Usually the \texttt{q} register is used for macros, but this can get
confusing when first starting out. The command would then be \texttt{qqqqqV\textasciitilde{}j@qq@q}.
Also, this macro can be simplified to \texttt{ggVG\textasciitilde{}}.}, which switches the case of every
character until the end of the document.
\subsection{Registers}
\label{sec:orgc9f4037}
\subsubsection{Intermediate}
\label{sec:orgd588cb2}
The most important part about registers is that the \texttt{"+}\footnote{The \texttt{"} is used to retrieve registers, with \texttt{+} referring to
the name of the register to be accessed, (in this case it is the
special "global register").} prefix is used
to store the global clipboard, which can be accessed by any program. Frankly, I
don't use any register other than the global one.
Other actions, such as yanks and deletions can be prefixed with a register, for
later retrieval.
A useful combination is using registers for editing a macro you wrote. To
continue from the macros section, you can write an incorrect macro, paste it
into the file, modify it, copy it back to the register, and then run that macro.
This seems quite difficult, but there can be really long macros that you would
rather go through the above to change a character than to remake the macro from
the beginning.
\section{Extending Vim for yourself}
\label{sec:org28d5361}
\subsection{Configuration File}
\label{sec:org8e6499a}
To change the default behaviour of Vim, and to keep it even after quitting, you
must modify a configuration (also known as a dot file\footnote{Because the file starts with a \texttt{.}, I know, so original.}) file for Vim. For
GNU/Linux and NeoVim users this file is \texttt{\textasciitilde{}/.config/nvim/init.vim}, for Vim users
the file is \texttt{\textasciitilde{}/.vimrc}. If you use Windows, the file will be \texttt{\_vimrc}\footnote{\href{https://superuser.com/questions/86246/where-should-the-vimrc-file-be-located-on-windows-7}{Locate Home in Windows}.} (in
the home directory in Windows).
For example, typing \texttt{:colorscheme elflord}\footnote{To find a colour scheme you like from the preinstalled colour
schemes, go to \href{https://stackoverflow.com/questions/7331940/how-to-get-the-list-of-all-installed-color-schemes-in-vim}{List installed colorschemes}.} from Normal Mode, will change
the colour scheme to elflord for the current Vim session. Once you close Vim
this setting will be gone. To save this setting, save the following lines in the
configuration file as:
\begin{verbatim}
" Set colour scheme to elflord
colorscheme elflord
\end{verbatim}
Notice the lack of a colon at the beginning of the line. The \texttt{"} indicates a
line comment.
Here are some other settings you may wish to add to your Vim configuration
file.\footnote{This is mostly taken from \href{https://github.com/amix/vimrc}{Amix's Vimrc}.} In general, you should always copy the comments along with the
actual code. (NOTE: always understand what every command does before adding it
to your configuration file.)
The below file is also available as \texttt{configuration.vim} at
\href{https://github.com/Hassan-Shabbir/vim-introduction}{Hassan-Shabbir/vim-introduction}.
\begin{verbatim}
" Enable filetype plugins, such as syntax highlighting for files.
filetype plugin indent on
" Enable syntax highlighting.
syntax enable
" Set line numbering to change based on Mode.
" This is especially helpful when moving n lines up.
set number relativenumber
" Set autoread to true. When a file is changed from the outside,
" the file will be reloaded.
set autoread
" With a map leader it's possible to do extra key combinations
" like '<leader>w' saves the current file. 'mapleader' is
" usually the backslash key ('\'), however, below we set it
" to the ',' key, since it is easier to reach.
let mapleader = ","
" This is how you would define "in normal mode, if I press
" the leader key (see above), followed by the 'w' key,
" map it to be the same as writing the file".
nmap <leader>w :w!<cr>
" Set a space of 3 lines between the cursor and the top/bottom
" of the window, making it easier to get the context of the code.
set so=3
" Turn on a completion menu on the bottom. Used when you try to
" tab-complete something in command mode.
set wildmenu
set wildmode=list:longest,full
" Configure backspace so it acts as it should act. Namely,
" allow backspace to delete new lines, delete past the start
" of insert mode, and delete autoindent.
set backspace=eol,start,indent
" Ignore case when searching, so '/hi' will match 'hi' in the
" text, along with 'Hi'.
set ignorecase
" If a case is used, however, search match using case. So
" '/Hi' will only match 'Hi', and not 'hi', (since we
" explicitly told the case).
set smartcase
" Highlight search results.
set hlsearch
" Make search act like search in modern browsers.
set incsearch
" Don't redraw while executing macros (for performance).
set lazyredraw
" Return to last edit position when opening files
" NOTE: Put this all on one line
au BufReadPost *
if line("'\"") > 1 && line("'\"") <= line("$")
| exe "normal! g'\"" | endif
\end{verbatim}
\subsection{Plugins}
\label{sec:orge5ea23e}
These are a few plugins that I would consider quite useful.
\begin{itemize}
\item \href{https://github.com/junegunn/vim-plug}{Vim Plug}: Vim plugin manager
\end{itemize}
To be able to use the below plugins you need to install a plugin manager, this
is the one I personally use, (no real reason).
\begin{itemize}
\item \href{https://github.com/tpope/vim-sensible}{Vim Sensible}: set default settings for Vim
\end{itemize}
This is useful for starting off in Vim. (Not needed for NeoVim.)
\begin{itemize}
\item \href{https://www.github.com/myusuf3/numbers.vim}{Numbers Vim}: add relative line numbers to Vim (great for going n lines up or down)
\item \href{https://www.github.com/tpope/vim-commentary}{Vim Commentary}: (un)comment lines of code with a text object
\item \href{https://www.github.com/tpope/vim-surround}{Vim Surround}: surround text objects with text
\item \href{https://www.github.com/tpope/vim-vinegar}{Vim Vinegar}: simple file browser in Vim
\item \href{https://www.github.com/mattn/emmet-vim}{Emmet Vim}: create HTML easily
\item \href{https://github.com/ctrlpvim/ctrlp.vim}{Ctrlp Vim}: fuzzy find files
\item \href{https://vimawesome.com/plugin/targets-vim}{Targets Vim}: add more text objects to Vim
\end{itemize}
More plugins for Vim can be found on \url{https://vimawesome.com}.
\subsubsection{ColorSchemes}
\label{sec:org24cab0a}
\begin{itemize}
\item \href{https://www.github.com/liuchengxu/space-vim-dark}{Space Vim Dark}
\item \href{https://github.com/altercation/solarized}{Solarized}
\end{itemize}
\subsubsection{Vim in other places}
\label{sec:org16058f8}
\begin{itemize}
\item Bash/Zsh: Both Bash and Zsh have Vim modes that can be enabled in their respective dot-files
\item \href{https://github.com/ardagnir/athame}{Athame}: Full Vim in the terminal, ie. when writing bash commands
\item \href{https://chrome.google.com/webstore/detail/vimium/dbepggeogbaibhgnhhndojpepiihcmeb}{Vimium}: Vim in Chrome
\end{itemize}
There are also other applications that will use Vim-like keybindings by default,
such as \texttt{man}.
\section{More Resources}
\label{sec:org1e99aa8}
\subsection{Vimtutor}
\label{sec:org24c0a42}
The \texttt{vimtutor} command can be used from your shell (ie. bash). Once run,
\texttt{vimtutor} will guide you through some basic Vim commands.
\subsection{Vim's \texttt{:help} command}
\label{sec:org2ad77ca}
A great way to learn more about a particular command in Vim is to use vim's
amazing built-in help system using \texttt{:help}, followed by the command details
(mode, modifier, key, etc.; see \texttt{:help}). This is usually recommended for
understanding what a command does, since Google is sometimes less helpful.
If you only use \texttt{:help} and press enter, the introduction to the help system
will be shown.
Vim also has the \texttt{:helpgrep} command which searches the help system for the
searchterm you entered.
\subsection{VimCasts.org}
\label{sec:orgb5c2a9e}
There are 76 Vim casts in total. It is highly recommended to start from \#1,
since the Vim casts get more advanced later on. All Vim casts are highly
recommended.
\begin{itemize}
\item \href{http://vimcasts.org/episodes/page/8/}{Vim Casts}
\end{itemize}
The below are paid books from the creator of the Vim casts:
\begin{itemize}
\item \href{https://pragprog.com/book/dnvim2/practical-vim-second-edition}{Practical Vim}
\item \href{https://pragprog.com/book/modvim/modern-vim}{Modern Vim}
\end{itemize}
\subsection{Youtube Videos}
\label{sec:org3e090e0}
These are some Youtube videos on Vim. They have been sorted from beginner to
advanced. Videos by Luke Smith (in general) and ThoughtBot (on Vim) are
recommended.
\begin{itemize}
\item \href{https://www.youtube.com/watch?v=\_NUO4JEtkDw}{Learning Vim in a week}
\item \href{https://www.youtube.com/watch?v=tQCRVkSFFEc}{Why Vim doesn't need the mouse}
\item \href{https://www.youtube.com/watch?v=wlR5gYd6um0}{Mastering the Vim language}
\item \href{https://www.youtube.com/watch?v=cTBgtN-s2Zw}{Vimrc and Vim plug-in overview}
\item \href{https://www.youtube.com/watch?v=wRFEBw02aT8}{Vim macros: why and how to!}
\item \href{https://www.youtube.com/watch?v=NzD2UdQl5Gc}{How Vim makes my daily life easier}
\item \href{https://www.youtube.com/watch?v=XA2WjJbmmoM}{How to do 90\% of what plugins do (with just Vim)}
\end{itemize}
\subsection{Blog Posts}
\label{sec:org8bfbd1b}
The following blog post helps to motivate the use of Vim.
\begin{itemize}
\item \href{http://www.viemu.com/a-why-vi-vim.html}{Why, oh WHY, do those \#?@! nutheads use vi?}
\end{itemize}
All arabesque posts are recommended, for example the following:
\begin{itemize}
\item \href{https://sanctum.geek.nz/arabesque/series/unix-as-ide/}{Unix as IDE}
\item \href{https://sanctum.geek.nz/arabesque/advanced-vim-macros/}{Advanced Vim macros}
\end{itemize}
\subsection{Miscellaneous}
\label{sec:orge2c5ae4}
The following are fun stories written about a fictitious master
and a desciple.
\begin{itemize}
\item \href{https://sanctum.geek.nz/arabesque/vim-koans/}{Vim Koans}
\item \href{https://blog.samwhited.com/2015/04/the-dharma-of-vi/}{The Dharma of Vi}
\item \href{https://sanctum.geek.nz/etc/emperor-sh-and-the-traveller.txt}{Emperor Sh and the traveller}
\end{itemize}
The Vim cheat sheet given above:
\begin{itemize}
\item \href{http://www.viemu.com/a\_vi\_vim\_graphical\_cheat\_sheet\_tutorial.html}{VI/Vim cheat sheet}
\end{itemize}
\section{Conclusion}
\label{sec:orgbfac31d}
Congratulations on finishing this whole document! You should now know enough to
be able to use vim, and look up whatever you need on the internet\footnote{Such as \href{http://vim.wikia.com/wiki/Vim\_Tips\_Wiki}{Vim Tips Wiki}.}. To
become proficient with Vim, you should use it repeatedly, until the Beginner
commands come to you without much thought.
Thanks for reading, and good luck on your journey to become a Vim master!
\end{document}