useful simple vi commands for DBAs

This page has been moved to http://www.andrewfraserdba.com/?p=6

Advertisements

13 Responses to “useful simple vi commands for DBAs”

  1. Al Says:

    :1,$s/data/index/g replace all occurrencies of “data” with “index”

    You only need use that “1,$” format if you want to specify which lines you want the search/replace to occur on. To replace all simply use:
    :s/original/replacement/

    The “g” on the end is not required any more either, but can still be used for legacy purposes.

    “:e!” reopens the existing file for editing – the bang forces it to without saving. Can be very useful!

    I also like “cw” and associated commands (eg. dw) which acts on a “word” from the cursor to the next punctuation mark. (cw stands for “change word” – deletes the word and changes the mode to insert)

    In general a number before any of the commands works, so eg “10yy” will yank 10 lines and “yG” or “dG” will yank/delete to the end of file.

    “r” changes current character to , “5r” changes next 5 chars to

  2. Al Says:

    Sorry, lost a lot of that in the html. Here’s the full text:
    :1,$s/data/index/g replace all occurrences of “data” with “index”

    You only need use that “1,$” format if you want to specify which lines you want the search/replace to occur on. To replace all simply use:
    :s/original/replacement/

    The “g” on the end is not required any more either, but can still be used for legacy purposes.

    “:e!” reopens the existing file for editing – the bang forces it to without saving. Can be very useful!

    I also like “cw” and associated commands (eg. dw) which acts on a “word” from the cursor to the next punctuation mark. (cw stands for “change word” – deletes the word and changes the mode to insert)

    In general a number before any of the commands works, so eg “10yy” will yank 10 lines and “yG” or “dG” will yank/delete to the end of file.

    “r[char]” changes current character to [char], “5r[char]” changes next 5 chars to [char]

  3. Andy Todd Says:

    Here are a couple of corkers I’ve got bookmarked;


    http://tnerual.eriogerg.free.fr/vimqrc.pdf

  4. Andy Todd Says:

    Oh, and the ultimate list of vi/vim links;

    http://thomer.com/vi/vi.html

  5. matt Says:

    A couple of other useful-sh things:

    % (i.e. shift-5) – takes you to the matching bracket, if you’re on a bracket

    qx,q,@x – to start recording, stop recording and execute a macro (called x). Probably vim only

  6. d Says:

    shift z is not equivalent to :wq in that it does not save (write) the file if there are no unsaved changes.

  7. Andrew Fraser Says:

    Thanks d., I’ve updated original post with that. Means I think that it is better practice to use shift-z-z.

  8. Kiran Says:

    Hi,

    Thanks a ton for this which i really need these commands.

  9. SteveC Says:

    Andrew

    Many thanks for this page. It’s the only place on the web where I could find the commands I needed quickly.

    Star!

  10. kedarm Says:

    Hey,

    How does one insert a new line?

    For instance, if my file is –
    test 1
    test 2
    test 3

    and I want to make it,
    test 1

    test 2

    test 3

    Will a simple ‘:s/test/\n test’ work?

  11. Andrew Fraser Says:

    Hi kedarm.
    :%s/$/ctrl+v ctrl+m
    Does that. That’s how you type it, but it appears as
    :%s/$/^M
    On the vi screen.
    Works because $ is the end of line character in vi. ^ is the equivalent beginning of line character, I sometimes use that instead.
    Andrew.

  12. kedarm Says:

    I’ve got some doubts –

    1) Difference between :%s and :$s
    :%s/$/ctrl+v ctrl+m – After every instance of $ (end line), insert ctrl+v ctrl+m (new line)?
    :$s/$/ctrl+v ctrl+m – Replace every instance of $ (end line) with ctrl+v ctrl+m (new line)
    Is the above correct?

    2) Why use ctrl+v ctrl+m
    Why doesn’t :%s/$/$/g work? This should mean that append after every $ (new line) another $ (new line)?
    Why should ctrl+v ctrl+m and not another $ – eg, after every instance of an end-line, put another end-line, something analogous to
    if (s[i]==’\n’) s[i+1]=’\n’;

    3) Why not use /g?
    Why don’t we use the /g after :%s/$/ctrl+v ctrl+m? Doesn’t it tell the command to do it for every occurrence of $ (end-line)?

    4) Escape sequences
    If I want to replace the character $ (dollar) by c (cents), is there some escape sequence I can use so that vim doesn’t mistakenly hunt for end-of-line characters instead of the ‘$’ character?

    Thanks
    Kedar

  13. Andrew Fraser Says:

    Hi Kedar,

    1) :%s changes every line, :$s only changes the last line in the file.
    2) $ is end of line rather than new line. ctrl+v ctrl+m is carriage return/line break character.
    3) You can include /g if you want, makes no difference to this particular command, since there will be only one end of line marker in each line.
    4) Use the “\” character to escape out special vi characters. So this command would change all $ symbols to c:
    :%s/\$/c/g

    Andrew.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: