vi delete cr
March 3, 2011 8:09 AM   Subscribe

vi: Deleting a carriage return and backing up to the last character of the previous line.

When entering text, I hit return then change my mind. In a GUI editor I'd hit backspace. What's the easiest thing to do in vi?
posted by Paquda to Computers & Internet (13 answers total) 1 user marked this as a favorite
Use vim instead, whose default behaviour will do exactly this.
posted by devbrain at 8:17 AM on March 3, 2011

In vim, hitting backspace will in fact return you to the previous line. In other versions of vi, you're kind of SOL. There isn't anything much faster than hitting <esc> dd shift-A.
posted by 1970s Antihero at 8:18 AM on March 3, 2011

Best answer: (nitpick: dd k shift-A)
posted by devbrain at 8:19 AM on March 3, 2011

Does vi have undo? In vim I would just hit esc and then u.
posted by hominid211 at 8:19 AM on March 3, 2011

(nitpick on the nitpick: but only if not on the last line of the file)
posted by devbrain at 8:20 AM on March 3, 2011

hominid211, hitting <esc> u will undo everything you typed since the last time you entered insert mode, i.e. all the text that came before you hit CR.
posted by 1970s Antihero at 8:22 AM on March 3, 2011

1970s Antihero, good point. This is one of those rare cases I would actually use the backspace key in vim, so I didn't really think about that.
posted by hominid211 at 8:27 AM on March 3, 2011

If using Vim or another Vi based editor (like nvi) that supports backspacing over lines (see :he bs in Vim for documentation) it isn't an option... I'd probably hit <esc>kJa (so, leave insert mode, go up one line, join the empty line from below and enter insert made after the space). Or leave off "a" if you want to do something else.
posted by skynxnex at 8:38 AM on March 3, 2011 [1 favorite]

Best answer: I do <esc> k shift-J

k moves to the previous line, and J joins to the next line, removing the carriage return.
posted by smackfu at 8:38 AM on March 3, 2011 [1 favorite]

CTRL-h also works as a backspace in insert mode, so if you're cool and have your capslock key remapped to ctrl, you can go backwards quick.

But yeah, the underlying answer is that vi is ancient. Use vim.
posted by cschneid at 8:40 AM on March 3, 2011

Response by poster: Oh well. At least I didn't turn out to have been missing something obvious, which is what I thought would happen when I posted.
posted by Paquda at 11:28 AM on March 3, 2011

Best answer: Are you really using 'vi' as opposed to 'vim'? If you're using vim, and backspace isn't working to backspace over a newline like you expect, there is a 'backspace' option that controls that. I.e. vim can do it, but it may not be turned on in your standard setup. I have to have a 'set backspace=indent,eol,start' in my ~/.vimrc to enable it.

                                                        *'backspace'* *'bs'*
'backspace' 'bs'        string  (default "")
                        {not in Vi}
        Influences the working of <bs>, <del>, CTRL-W and CTRL-U in Insert
        mode.  This is a list of items, separated by commas.  Each item allows
        a way to backspace over something:
        value   effect  ~
        indent  allow backspacing over autoindent
        eol     allow backspacing over line breaks (join lines)
        start   allow backspacing over the start of insert; CTRL-W and CTRL-U
                stop once at the start of insert.

        When the value is empty, Vi compatible backspacing is used.

        For backwards compatibility with version 5.4 and earlier:
        value   effect  ~
          0     same as ":set backspace=" (Vi compatible)
          1     same as ":set backspace=indent,eol"
          2     same as ":set backspace=indent,eol,start"

        See |:fixdel| if your <bs> or <del> key does not do what you want.
        NOTE: This option is set to "" when 'compatible' is set.

posted by zengargoyle at 6:27 PM on March 3, 2011

Response by poster: Thank you, zengargoyle.
posted by Paquda at 5:27 AM on March 4, 2011

« Older Help me find this todo webapp?   |   What's this Japanese TV office satire? Newer »
This thread is closed to new comments.