Vim: More Copying Tips
Blocks of lines can be copied in Vim using ranges, i.e., two numbers separated by a comma indicating the start and end of the block of lines.
Some Examples
To copy line 20-25, use:
:20,25yank
To copy five lines just above the cursor:
:-4,.yank
where the period (.) indicates the current line.
To copy five lines just below the cursor:
:.,+4yank
Tags: cli, vim, copy, ranges, motd
Vim: How to Copy & Paste
Most Vim tutorials and cheatsheets out there will cover how to copy and paste. Pasting is the easy part. Let’s talk about copying!
Copying
To copy the current line (line in which the cursor is placed), simply
press double lower y
(yy
) or single upper case
y
(Y
).
yy
or Y
: copies/yanks the current line,
including the newline character at the end of the line.
To copy a specific number of lines under the cursor (beginning from
the current location of the cursor) simply precede the command with the
number of lines that you wish to copy, i.e., Nyy
(where N
is the number of lines).
You can also use the following operation to copy two lines:
:.,+yank
where the dot (.) refers to the current line and plus sign (+) means plus one line below the cursor.
So, in order to use this syntax to copy 3 lines, use 3yy
or :.,+2yank
will work.
You can also copy a specific number of lines above the
cursor From the current cursor location use yN-1k
, where
N-1
is the number of lines you want to copy. So, to copy
two lines from the current position of the cursor, press (in normal
mode, or escape mode), y2-1k
, or y1k
.
To copy 5 lines above the current cursor position:
y4k
You can also use other operations. for example, to copy only the fifth line just above the cursor:
Copying & Pasting
:-4yank
You can can copy lines and paste them right above the cursor in one
shot using the operation :Nt-
, where N
is the
line number you want to copy.
You can can copy specific line number and paste right below the
cursor directly using the operation :Nt.
, where
N
is the line number you want to copy.
For example, to copy line two and paste just above the current cursor position;
:2t-
To copy line two and paste just below the current cursor position;
:2t.
The above commands also work with a range of lines rather than a
single line, such as :4,7t.
will yank lines 4-7 and paste
them below the cursor.
Pasting
p
or P
: pastes one or more lines, basically
whatever is in the buffer, under or above the cursor respectively.
Vim: Underlining a Line
Of course, Vim is just a text editor but you can
underline a line using a set of keystrokes that copies the
string to be underlined to the next line and then replaces the string
with hyphens using this set of Vim commands: YpVr-
So if you have a line:
This Is My Page Title
just position the cursor at the start of that line and enter the abovementioned commands and it will turn it into:
This Is My Page Title
---------------------
This would save you a few keystrokes if you use Vim for creating a lot of text documentation and use hyphens a lot in your section headers.
Vim Keyboard Shortcuts
gg
Move to the first line of the fileG
Move to the last linegg=G
Reindent the whole filegv
Reselect the last visual selection<
Jump to beginning of last visual selection>
Jump to end of last visual selection^
Move to first non-blank character of the lineg_
Move the last non-blank character of the line (but you remove trailing whitespace, right)g_lD
Delete all the trailing whitespace on the lineea
Append to the end of the current wordgf
Jump to the file name under the cursorxp
Swap character forwardXp
Swap character backwardyyp
Duplicate the current lineyapP
Duplicate the current paragraphdat
Delete around an HTML tag, including the tagdit
Delete inside an HTML tag, excluding the tagw
Move one word to the rightb
Move one word to the leftdd
Delete the current linezc
Close current foldzo
Open current foldza
Toggle current foldzi
Toggle folding entirely<<
Outdent current line>>
Indent current linez=
Show spelling correctionszg
Add to spelling dictionaryzw
Remove from spelling dictionary~
Toggle case of current charactergUw
Uppercase until end of word (u for lower, ~ to toggle)gUiw
Uppercase entire word (u for lower, ~ to toggle)gUU
Uppercase entire linegu$
Lowercase until the end of the lineda"
Delete the next double-quoted string+
Move to the first non-whitespace character of the next lineS
Delete current line and go into insert modeI
insert at the beginning of the lineci"
Change what’s inside the next double-quoted stringca{
Change inside the curly braces (try [, (, etc.)vaw
Visually select worddap
Delete the whole paragraphr
Replace a character[
Jump to beginning of last yanked text]
Jump to end of last yanked textg;
Jump to the last change you madeg,
Jump back forward through the change list&
Repeat last substitution on current lineg&
Repeat last substitution on all linesZZ
Save the current file and quit Vim
Tags: cli, vim, cheatsheet, shortcuts, motd
Sorting Stuff in the VIM Buffer
Suppose I have a file that I am editing in vim with the following contents:
$ cat file.txt
Red 4
Blue 3
Green 1
Orange 7
Black 8
Yellow 6
Purple 2
White 5
If I want to sort this data in vim, the first thing to do is to format it into columns using the column command:
:%!column -t
Which will put the data in the following format:
Red 4 Blue 3 Green 1 Orange 7 Black 8 Yellow 6 Purple 2 White 5
Then we can sort on the 2nd column using the sort command:
:%!sort -k2nr
Which will update the buffer like this:
Black 8 Orange 7 Yellow 6 White 5 Red 4 Blue 3 Purple 2 Green 1
Vim Tips: Searching a File for Multiple Strings
In Vim you can perform a search like this: /string1/;/string2
This will will combine two searches. It tells Vim:
- Search for string1, then
- From there search for string2 on any line that also contains string1.
Edit a Remote File Using Vim
To edit a file remotely using the vim editor, just do the following in a terminal window:
$ scp://username@IP-address//path/to/file.txt
The user must have access to the file being edited.