tags:

views:

105

answers:

3

In Vim, how do I break a line of text with a newline?

For example, I'm in INSERT mode and I'd like to get from here...

$('#toggle_contact_search_mode').click([CURSOR IS HERE]);

To here...

$('#toggle_contact_search_mode').click(
  [CURSOR IS HERE]
);

The result I'm getting is that when I'm in insert mode and I have the cursor in the first position above and I hit return...

  • Vim goes to normal mode.
  • The line with the cursor scrolls to the bottom of the screen.
  • Code throughout the file is reformatted incorrectly. For example, in another part of the file the code is reformatted like this:


$('.edit_phone_number').ajaxForm({
success: function(response) {
$('input, select, textarea', '.edit_phone_number').removeClass('updating');
}   
});

$('input, select, textarea', '.edit_phone_number').focus(function(event) { 
    $(event.target).removeClass('updating').addClass('focussed');
    });

I don't want any reformatting at all. I just want Vim to just enter a newline and remain in insert mode.


UPDATE

When I temporarily remove my .vimrc file, the Vim behaves as expected. My .vimrc:

" ==================================================================================
" Global stuff
" ==================================================================================

" Prevent Vim from emulating vi bugs and limitations
:set nocompatible

" Custom status line based on one from here:
" http://www.linux.com/archive/feature/120126
:set statusline=\ \ %f\ \ [FORMAT=%{&ff}]\ \ [TYPE=%Y]\ \ [POS=%04l,%04v][%p%%]\ \ [LEN=%L]

" Set status line colors
highlight StatusLine term=reverse ctermfg=DarkBlue ctermbg=Grey

" Set status line to be shown above the command buffer.
:set laststatus=2

" Enable syntax highlighting
:syntax on

" Enable line numbering, taking up 6 spaces
:set number

" Allow <BkSpc> to delete line breaks, beyond the start of the current
" insertion, and over indentations
" set backspace=eol,start,indent

" ==================================================================================
" Searching
" ==================================================================================

" Highlight matches as you type in the search string
:set incsearch
:set showmatch

" When searching ignore case (except explicit caps)
:set ignorecase
:set smartcase

" ==================================================================================
" Indenting
" ==================================================================================

" Copy indent from current line when starting a new line
" :set autoindent
"
" :set smartindent

" :filetype plugin on

" indent depends on filetype
" :filetype indent on

:set tabstop=2
:set softtabstop=2
:set shiftwidth=2
:set expandtab

" ==================================================================================
" Mappings
" ==================================================================================

" ESC
imap <C-k> <ESC>

" Go to the next tab
nmap <C-j> gt
imap <C-j> <ESC>gt

" Auto indent entire file.
" Sets a mark ("mt""), moves across the whole file to indent it ("gg=G"), 
" then returns to the mark ("'t").
nmap <C-m> mtgg=G't
imap <C-m> <ESC><C-m>
+1  A: 

Seems like the imap lines you have in your .vimrc are messing you up. It seems odd to make a mapping to work in insert mode just to make it do what the normal mode mapping does. Are you sure you actually want that to happen? I'd suggest considering removing the last two imap lines (c-j and c-m), and see if you even notice they're gone.

zigdon
+6  A: 

Your problem is the imap <C-m> mapping -- control-M is logically equivalent to enter on the terminal.

hobbs
Entering ^J is also equivalent to pushing enter on the terminal. Mapping either of those isn't, in general, a great idea.
janm
Haha, that answers an age old weirdness I was experiencing. Having mapped C-m to "make" and wondering why sometimes a make would occur when I hit enter.
Benj
A: 

I don't know VIM but Ctrl+M works in Notepad -- you might try it.

gerard