当前位置: 代码迷 >> 综合 >> 第十章 Making big changes
  详细解决方案

第十章 Making big changes

热度:47   发布时间:2023-12-20 22:03:54.0

usr_09 Using the GUI

I don’t want to use gui

usr_10 Making big changes

10.1 Record and playback commands

The “.” command repeats the preceding change.

  • recording
    1. The “q{register}” command starts recording keystrokes into the register named {register}.
    2. Type your commands.
    3. To finish recording, press q.
  • execute the macro by the command “@{register}”, can be preceded by a count.
  • move and execute
@a
@@
  • using registers
"np
{edits}
0
"ny$
dd
  • appending to a register
"aY
"AY
10.2 Substitution
  • to perform string replacements.
:[range]substitution/from/to/[flags]
:%substitute/Professor/Teacher/
  • to change every occurrence on the line.
%s/Professor/Teacher/g
  • flags
  1. global
  2. print
  3. confirmation
:s/^the/these/
:s:one/two:one
10.3 Command ranges
  • The simple form of a range is {number},{number}.
1,5s/this/that/g
  • address one specified line:
:54s/President/Fool/
  • write only the current line into a file:
:.write otherfile
  • to substitution in the lines from the cursor to the end:
:.,$s/yes/no/
  • from the first to the last line:
% is a short way to say "1, $"
  • Using A Pattern In a range
:?^Chapter?,/^Chapter/s=grey=gray=g
= is delimiter 
  • add and substract
  • To search for a pattern and then use the line above it:
/Chapter/-1
  • To address the second line below the match:
/Chapter/+2
  • The offsets can also be used with the other items in a range:
:.+3,$-5
  • using marks
  • use mt ot mark the top of an area and mb to mark the bottom:
:'t,'b
  • visual mode and ranges
  • you press : to start a colon command, you will see this:
:'<,'>
The '< and '> are actually marks, placed at the start and end of the Visual selection.
  • you can use the '< command to jump to position where the visual area started:
:'>,$
  • a number of lines
you can type the number and then ":". if you type "5:", you will get:
:.,.+5
10.4 The global command
  • find a match for a pattern and execute a command there.
:[range]global/{pattern}/{command}
:g+//+s/foobar/barfoo/g
10.5 Visual block mode
  • ctrl-v selection of a rectangular area of text.
  • inserting text
the command I{string}<Esc> inserts the text {string} in each line.
  • changing text
  • Other commands that change the characters in the block:
        ~       swap case       (a -> A and A -> a)U       make uppercase  (a -> A and A -> A)u       make lowercase  (a -> a and A -> a)
  • filling with a character
  • shifting
  • the command “>” shifts the selected text to the right one shift amount, inserting whitespace.
:set shiftwidth=4
  • joining lines
  • the “J” command joins all selected lines together into one line.
10.6 Reading and writing part of a file
  • include another file.
:read {filename}
  • appends the file “patch” at the end of the file.
:$r patch
  • read the file above the first line.
:0read patch
  • writing a range of lines
:.,$write tempo
  • if you know what you are doing and want to overwrite the file:
:.,$write! tempo
  • appending to a file
  • write the first line with this command:
:.write collection
:.write >>collection
10.7 Formatting text
  • type plain text, each line is automatically trimmed to fit in the window:
:set textwidth=72
  • to check the current value of textwidth:
:set textwidth
  • to tell vim to format the current paragraph
gqap
  • format the whole file:
gggqG
10.8 Changing case
                         gUwsection header      ---->      SECTION header
  • to make an operator work on lines you double it.
                            g~~ Some GIRLS have Fun    ---->   sOME girls HAVE fUN ~
Using an external program
  • The command “!{motion}{program}” takes a block of text and filters it through an external program.
  • sort lines 1 through 5 of a file.
!5Gsort<Enter>
  • !! command filters the current line through a filter.
!!date<Enter>
  • when it donesn’t work
        'shell'         specifies the program that Vim uses to executeexternal programs.'shellcmdflag'  argument to pass a command to the shell'shellquote'    quote to be used around the command'shellxquote'   quote to be used around the command and redirection'shelltype'     kind of shell (only for the Amiga)'shellslash'    use forward slashes in the command (only forMS-Windows and alikes)'shellredir'    string used to write the command output into a file
  • reading command output
  • to read the contents of the current directory into the file:
:read !ls
  • a range can be used to where vim should put the lines:
0read !date -u
  • writing text to a command
  • to count the words in the current file:
:write !wc
  • redrawing the screen
ctrl-l