From RootdevWiki
Contents |
Helpful commands
Vim Cookbook - good bits
Set dos/unix/mac modes
:set ff=unix (or dos|mac)
See Vim Wiki for more details.
Cut text
Command Explanation 1. Move the cursor to the top of the paragraph you want to move. 2. ma Place a mark named "a" at this location. 3. Move the cursor to the bottom of the paragraph to be moved. 4. d'a Delete to mark "a". This puts the deleted text in a cut buffer. 5. Move the cursor to line where the text is to go. 6. p Paste the text in below the cursor.
Alternatively at step 5:
:sn newfile.txt p
Ctrl-K and Ctrl-J enable you to switch b/w windows.
Rock on.
Remove ^M (from Windows users)
:%s/\r//g
How to Escape escape in vim scripts
To insert a text snippet into a bunch of scripts or files at a specific point, say line 17, you can add the snippet to a file and call it like so:
vi +17 -s scriptname <filename>
Vi[m] will read the text in from scriptname as though you had typed it.
That's all well and good, but how do you then escape from insert mode to save the file?
You need to escape the escape character by pressing CTRL-v and pressing the [esc] key, and you get the following:
iText goes here, immediately after pressing 'i' to begin 'insert' mode, unless you want a space at the beginning of the line, and keeps going as long as you need to input it. ^[ :wq
NB You will not be able to see the ^[ if you use any kind of pager to view the script, you will just see a blank line.
To confirm it's there open the script again in vi.
Lowercase HTML Tags
This only works for tags with no attributes:
%s:<\(/\=\u\+\)>:<\L\1>:g
I could work out how to do this for tags with attributes, I just haven't got round to it yet.
Remove lines from file
To remove blank lines (or any other regex) from within a file:
%g/^$/d
Comment out all lines
From here to the end of the file, if they're not already commented out:
.,$s/\(^.*[^#]\)/#\1/g
Change quoted multidimensional array assignment to array references
.,$s/"\(@[abcde]\)"/\\\1/gc