I'm looking for a command wich would be the opposite of the <shift>J
command.
For example, I want to break the sentence below in 2 parts and place
the
second part on the next line:
This is a sentence I want to break in two parts
Once I placed the cursor on the desired word ("break" for ex), I have
to:
- type i (go in the insert mode)
- type <enter> (place the rest of the sentence on the next line)
- type <esc> (return in the command mode)
I'd like to know if there a quickest way to do this.
Thanks
The J (or shift-j) command will join [count] lines (default 2) starting at the
cursor line, regardless of the cursor position. To do the opposite, you have
to tell Vim where the line breaks must come.
- If you just joined some lines, then change your mind, you can use u (undo).
- To break the line immediately before the cursor, you can do what you wrote
above, or, if you use it often, you can assign it to a key (let's say F3):
:map <F3> i<CR><Esc>
- To insert a line break and a space just before the cursor, you can also use
"="\n "^MP
(where ^M means "hit Enter" or "hit Ctrl-M", your choice -- these keystrokes
are synonymous to Vim). The above decomposes as
"= use the expression register
"\n " a linefeed and a space
^M end the expression
P put before cursor
This method cannot, however, be used to insert a naked linefeed, because if
the last character in the expression is a linefeed, the expression register
will be regarded as linewise and the break will not appear before the cursor
_character_ but before the cursor _line_ .
- To insert a line break after every period followed by whitespace in the
whole document (or in a given range of lines), you can use search-and-replace:
:1,$s/\.\s\+/\.\r/g
which means
: start an ex-command
1,$ from line 1 to the last line
s substitute
/ replace what?
\. full stop
\s\+ one or more spaces or tabs (as many as possible)
/ replace by what?
\. full stop
\r line break
/ end of "replace by"
g as many times per line as possible
Best regards,
Tony.
--
You think Oedipus had a problem -- Adam was Eve's mother.
> I'm looking for a command wich would be the opposite of
> the <shift>J command.
Here's what I use. E.g., \. will split the line and the
first tab or space after the cursor, respecting
'autoindent', and will clean up trailing whitespace.
map @[-trim] :-s/[ ^I]*$//^M+
map \, ?[ ^I]^M/[^ ^I]^Mi^M^[@[-trim]
map \. /[ ^I]^M/[^ ^I]^Mi^M^[@[-trim]
Note however that I have this in my exrc and is
vi-compatible; the ^I, ^M and ^[ represent literal tab,
Ctrl-M and escape. So you'll probably want to replace those
control characters with <Tab>, <CR> and <Esc> respectively.
You could also use Vim-only regexp features such as \s and
\S. Another consequence is that it uses :map rather than
:nnoremap, and it will also affect your search history. If
you never use a real vi and like the mappings then you
probably should adapt the maps to fix those issues. --Antony
In addition to inserting a newline, you can wrap a paragraph or
line using the current textwidth (tw).
Examples:
:set tw=20
gqap
gqq
John
I move the cursor over the space where I want to break the line and
type
r<Enter>
HTH,
Gary
I put this in my vimrc, which lets me format paragraphs with an uppercase Q:
nnoremap Q gqap
vnoremap Q gq
--Ted
--
Ted Pavlic <t...@tedpavlic.com>