Gregg's MOTD

Tips & Tricks that I've Encountered Over the Years...

Vim: More Copying Tips

September 17, 2023 — Gregg Szumowski

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

September 15, 2023 — Gregg Szumowski

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.

Tags: cli, vim, motd

Vim: Underlining a Line

September 12, 2023 — Gregg Szumowski

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.

Tags: cli, vim, motd

Vim Keyboard Shortcuts

September 02, 2023 — Gregg Szumowski
  1. gg Move to the first line of the file
  2. G Move to the last line
  3. gg=G Reindent the whole file
  4. gv Reselect the last visual selection
  5. < Jump to beginning of last visual selection
  6. > Jump to end of last visual selection
  7. ^ Move to first non-blank character of the line
  8. g_ Move the last non-blank character of the line (but you remove trailing whitespace, right)
  9. g_lD Delete all the trailing whitespace on the line
  10. ea Append to the end of the current word
  11. gf Jump to the file name under the cursor
  12. xp Swap character forward
  13. Xp Swap character backward
  14. yyp Duplicate the current line
  15. yapP Duplicate the current paragraph
  16. dat Delete around an HTML tag, including the tag
  17. dit Delete inside an HTML tag, excluding the tag
  18. w Move one word to the right
  19. b Move one word to the left
  20. dd Delete the current line
  21. zc Close current fold
  22. zo Open current fold
  23. za Toggle current fold
  24. zi Toggle folding entirely
  25. << Outdent current line
  26. >> Indent current line
  27. z= Show spelling corrections
  28. zg Add to spelling dictionary
  29. zw Remove from spelling dictionary
  30. ~ Toggle case of current character
  31. gUw Uppercase until end of word (u for lower, ~ to toggle)
  32. gUiw Uppercase entire word (u for lower, ~ to toggle)
  33. gUU Uppercase entire line
  34. gu$ Lowercase until the end of the line
  35. da" Delete the next double-quoted string
  36. + Move to the first non-whitespace character of the next line
  37. S Delete current line and go into insert mode
  38. I insert at the beginning of the line
  39. ci" Change what’s inside the next double-quoted string
  40. ca{ Change inside the curly braces (try [, (, etc.)
  41. vaw Visually select word
  42. dap Delete the whole paragraph
  43. r Replace a character
  44. [ Jump to beginning of last yanked text
  45. ] Jump to end of last yanked text
  46. g; Jump to the last change you made
  47. g, Jump back forward through the change list
  48. & Repeat last substitution on current line
  49. g& Repeat last substitution on all lines
  50. ZZ Save the current file and quit Vim

Tags: cli, vim, cheatsheet, shortcuts, motd

Sorting Stuff in the VIM Buffer

August 07, 2023 — Gregg Szumowski

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

Tags: cli, vim, sort, motd

Vim Tips: Searching a File for Multiple Strings

July 24, 2023 — Gregg Szumowski

In Vim you can perform a search like this: /string1/;/string2

This will will combine two searches. It tells Vim:

  1. Search for string1, then
  2. From there search for string2 on any line that also contains string1.

Tags: cli, vim, search, motd

Edit a Remote File Using Vim

May 28, 2023 — Gregg Szumowski

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.

Tags: cli, vim, scp, motd