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
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).
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:
Vim Keyboard Shortcuts
ggMove to the first line of the fileGMove to the last linegg=GReindent the whole filegvReselect 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_lDDelete all the trailing whitespace on the lineeaAppend to the end of the current wordgfJump to the file name under the cursorxpSwap character forwardXpSwap character backwardyypDuplicate the current lineyapPDuplicate the current paragraphdatDelete around an HTML tag, including the tagditDelete inside an HTML tag, excluding the tagwMove one word to the rightbMove one word to the leftddDelete the current linezcClose current foldzoOpen current foldzaToggle current foldziToggle folding entirely<<Outdent current line>>Indent current linez=Show spelling correctionszgAdd to spelling dictionaryzwRemove from spelling dictionary~Toggle case of current charactergUwUppercase until end of word (u for lower, ~ to toggle)gUiwUppercase entire word (u for lower, ~ to toggle)gUUUppercase 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 lineSDelete current line and go into insert modeIinsert at the beginning of the lineci"Change what’s inside the next double-quoted stringca{Change inside the curly braces (try [, (, etc.)vawVisually select worddapDelete the whole paragraphrReplace 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 linesZZSave the current file and quit Vim
Sorting Stuff in the VIM Buffer
Suppose I have a file that I am editing in vim with the following contents:
$ cat file.txtRed 4Blue 3Green 1Orange 7Black 8Yellow 6Purple 2White 5
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.