Fandom

Vim Tips Wiki

Use Return and Delete keys in normal mode like in insert mode

1,624pages on
this wiki
Add New Page
Talk0 Share

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.

Tip 1265 Printable Monobook Previous Next

created June 19, 2006 · complexity basic · author pulp · version n/a


function! Delete_key(...)
  let line=getline (".")
  if line=~'^\s*$'
    execute "normal dd"
    return
  endif
  let column = col(".")
  let line_len = strlen (line)
  let first_or_end=0
  if column == 1
    let first_or_end=1
  else
    if column == line_len
      let first_or_end=1
    endif
  endif
  execute "normal i\<DEL>\<Esc>"
  if first_or_end == 0
    execute "normal l"
  endif
endfunction
nnoremap <silent> <DEL> :call Delete_key()<CR>
nnoremap <silent> <CR> i<CR><Esc>

With this tip the keys "Return" and "Delete" will work the same in normal mode and insert mode. For example, a blank line will be deleted. Pressing Return will insert a new line.

Very useful before inserting some text via copy/paste. Or to clean up your code.

CommentsEdit

I made a real script from this: cr-bs-del-space-tab.vim script#1579


Also on Fandom

Random Wiki