Chuyển đến nội dung

Bài 14 · Editing and Text Objects

Vim Case, Join and Increment: Small Transformations

Change case with ~ gu gU g~, join lines with J and gJ, increment and decrement numbers with Ctrl-a and Ctrl-x, sort, align and reformat quick edits in Vim.

Beyond big edits, Vim has a set of small transformation commands that save you from retyping: switch case, glue lines together, bump a number. They are quick to learn and you will use them every day. This lesson also shows how to combine them with counts and Visual mode.

Changing case

Command Effect
~ toggle the case of the character under the cursor and move right
g~w toggle case of a word
gUiw uppercase the word
guiw lowercase the word
gUU uppercase the line (also gUgU)
guu lowercase the line
g~~ toggle case of the line
U / u in Visual mode: upper / lower
~ in Visual mode: toggle

Hold a count before ~ to flip several characters: 5~ toggles five. Case operators are normal operators, so everything from the previous lessons applies: gUap uppercases a paragraph, gu$ lowercases to the end of the line, gUi" uppercases the string inside quotes.

For a constant-style name, gUiw on max_size gives MAX_SIZE. To capitalize only the first letter of a word, go to the word start (b) and press ~.

JS Case operators (stand-in)

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Joining lines

J joins the current line with the next and puts one space between them, removing the next line's leading whitespace. gJ joins without touching spaces. A count joins more: 3J joins three lines into one. In Visual mode, select lines and press J.

TEXT
const a = [
  1,
  2,
];

Put the cursor on the first line and 4J collapses it to const a = [ 1, 2, ];. Vim is smart about trailing periods and closing parentheses when joining: for example it will not add a space before ). The joinspaces and formatoptions options tune this.

The reverse, splitting a line, is r<Enter> on a space, or i<Enter> in Insert mode.

Incrementing numbers

Ctrl-a adds one to the number at or after the cursor on the current line; Ctrl-x subtracts one. With a count the step is bigger: 10 Ctrl-a adds ten. They find the next number on the line if the cursor is not on one, which is very convenient. They also understand:

  • Hex numbers (0x1f becomes 0x20)
  • Binary (0b101), with nrformats set to include bin
  • Leading zeros: 007 increments as decimal in recent defaults, check :help nrformats
  • Negative numbers via a - sign in front
  • Dates and words with plugins, or alpha in nrformats for single letters

In Visual mode Ctrl-a adds to every number in the selection, and g Ctrl-a adds an increasing amount on each line: 1, 2, 3, and so on. This is the standard way to number a list.

Numbering a list

Type 0 on five lines in a column (o0<Esc> then . four times, or yy and 4p). Select the column with Ctrl-v and jjjj, then press g Ctrl-a. The lines become 1, 2, 3, 4, 5.

JS Ctrl-a and g Ctrl-a (stand-in)

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The last call does what g Ctrl-a does: one more on each successive line.

Sorting and aligning

Select lines with V and type :sort to order them. Useful flags: :sort u removes duplicates, :sort n sorts numerically, :sort! reverses, and :sort /pattern/ sorts by what comes after a match. For alignment, :%!column -t (on Unix with column) aligns text into columns. In Neovim and Vim you can also use :%normal for repeated commands, discussed in chapter 4.

Indent and re-wrap

> and < shift; = re-indents; gq wraps. They combine with case and join: gqip reflows a paragraph, and gUap shouts it. Try your own combination on a scratch buffer, and press u whenever it surprises you.

Common edits as recipes

Goal Keys
Capitalize a word b~ or vU
Make a constant gUiw
Collapse a list to one line V + range + J
Add 5 to a version number 5 Ctrl-a on that line
Number items 1..n Ctrl-v column, g Ctrl-a
Remove trailing spaces :%s/\s\+$//e (see chapter 4)

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. What does gUiw do?

    gU is the uppercase operator and iw is inner word.
  2. 2. What is the difference between J and gJ?

    gJ joins without inserting or removing whitespace.
  3. 3. What does g followed by Ctrl-a do on a Visual selection?

    g Ctrl-a gives 1, 2, 3 and so on across the selected lines; plain Ctrl-a adds one to all.
  4. 4. How do you add ten to the number on the current line?

    A count before Ctrl-a is the amount to add.

Summary

  • ~ gU gu g~ change case; operators combine with motions and text objects.
  • J and gJ join lines; r<Enter> splits.
  • Ctrl-a and Ctrl-x bump numbers, with counts; g Ctrl-a numbers a list.
  • :sort with flags sorts selections; gq and = reflow and re-indent.
  • Small transformations are cheap, so avoid retyping.

Next: Registers: Vim's Many Clipboards

Theo dõi tiến độ của bạn

Tạo tài khoản miễn phí để đánh dấu bài đã học và tiếp tục từ chỗ bạn dừng lại trên mọi thiết bị.