vim - Append next line to end of current [duplicate] - vim

This question already has answers here:
How do I join two lines in vi?
(9 answers)
Closed 9 years ago.
I'm sure this has been asked before, but I haven't had much luck with a search. Is there an easy way to append the next line to the end of the current line?
Example:
this.thing = that
.getThing();
I am looking for a one-stroke command that turns it into:
this.thing = that.getThing();

Shift-J joins the next line with the current one. It removes indentation but usually adds a space, unfortunately.
These commands, except "gJ", insert one space in place of the <EOL> unless
there is trailing white space or the next line starts with a ')'. These
commands, except "gJ", delete any leading white space on the next line.

Use J which is short for join.
Vim doc copied below
*J*
J Join [count] lines, with a minimum of two lines.
Remove the indent and insert up to two spaces

Adding to the other answers, if you want to keep the cursor position you can use a mark:
:nnoremap J mzJ`z
mz - add mark to register z
J - join as usual
`z - go to the mark set

Related

Remove until end of the line after the same character from different locations in multiple lines [duplicate]

This question already has answers here:
Delete all characters after "." in each line
(5 answers)
Closed 17 days ago.
I want to remove anything from : to the end of line in all of the following lines in Vim:
key1: A
key2_long: B
key3_longerrr: C
key: D
So the result would be:
key1
key2_long
key3_longerrr
key
For a single line I use f:d$. What are better ways to do this for all lines, wihtout repeating f:d$ for all lines?
:%s/:.*//g
It searches and replaces what matches the regex between the first and second / with what is between the second and third in all the file.
you can find more on this in the documentation, look for substitute
If you want to stick with f:d$ (which should be f:D), you can use :normal for a one-off macro:
:,+3normal f:D
or:
vjjj:normal f:D
See :help :range and :help :normal.

Completely de-indent visual block [duplicate]

This question already has answers here:
Remove all arbitary spaces before a line in Vim
(9 answers)
Closed 8 years ago.
How can I map a key to completely de-indent a visual block?
The best method I know for doing this "manually" is de-indenting once with < and then repeatedly pressing ., so the mapping is:
vmap <leader>d <.................
This does work but I'd like to know the "right" way to do this. The above also only works for however many .s there are.
use the steps in this page to completely trim white spaces from the left for a range
Step by step:
Select your block ( shift-v)
:left or :le
<CR>
That should do it
You can add a count to <; I usually do 50<, which will de-indent it 50 times, which should almost always be enough.
From :help <:
{Visual}[count]< Shift the highlighted lines [count] 'shiftwidth'
leftwards (for {Visual} see Visual-mode). {not in
Vi}
Instead of using the < key to de-indent you can simply delete all the whitespace in each line before the first character. You can do so by adding:
let #y = '0d^j'
In order to run this on every line of the selected block, enter command mode by typing : and complete the command to:
:'<,'>normal #y
Then again you can record another macro to do so:
let #q = ':normal #y'
Using this approach, you will eventually be able to de-indent a visual block by typing #q, which calls the macro #y in the background.
Explanation of the first macro:
0 will move the cursor to the beginning of the line,
d^ will delete all whitespace until the first character and
j will move the cursor to the line below

How to delete to the last blank characters on the line [duplicate]

This question already has answers here:
How can you automatically remove trailing whitespace in vim
(14 answers)
Closed 9 years ago.
I have:
int x = 1;______
(underscores means spaces)
and I would like to get:
int x = 1;
My naive solution is $bld$, is there a quickest way?
In Emacs I use M-\ (delete-horizontal-space)
For the current line:
:s/\s\+$
For all lines:
:%s/\s\+$
The substitution text can be omitted if blank, so we don't need to write s/\s\+$//.
I do this with a search and replace mapping:
map <leader>W :%s/\s\+$//<CR>:let #/=''<CR>
:%s/\s\+$// deletes all trailing white space and then :let #/='' clears the search register.
:%s/\s\+$//
What it does is it searches for white spaces at the end of the line and replace them by nothing.
Source: http://vim.wikia.com/wiki/Remove_unwanted_spaces

Vim: delete empty lines around cursor

Suppose I'm editing the following document (* = cursor):
Lions
Tigers
Kittens
Puppies
*
Humans
What sequence can I use to delete the surrounding white space so that I'm left with:
Lions
Tigers
Kittens
Puppies
*
Humans
Note: I'm looking for an answer that handles any number of empty lines, not just this exact case.
EDIT 1: Line numbers are unknown and I only want to effect the span my cursor is in.
EDIT 2: Edited example to show I need to preserve leading whitespace on edges
Thanks
Easy. In normal mode, dipO<Esc> should do it.
Explanation:
dip on a blank line deletes it and all adjacent blank lines.
O<Esc> opens a new empty line, then goes back to normal mode.
Even more concise, cip<Esc> would roll these two steps into one, as suggested by #Lorkenpeist.
A possible solution is to use the :join command with a range:
:?.?+1,/./-1join!
Explanation:
[range]join! will join together a [range] of lines. The ! means with out inserting any extra space.
The starting point is to search backwards to the first character then down 1 line, ?.?+1
As the 1 in +1 can be assumed this can be abbreviated ?.?+
The ending point is to search forwards to the next character then up 1 line, /./-1
Same as before the 1 can be assumed so, /./-
As we are using the same pattern only searching forward the pattern can be omitted. //-
The command :join can be shorted to just :j
Final shortened command:
:?.?+,//-j!
Here are some related commands that might be handy:
1) to delete all empty lines:
:g/^$/d
:v/./d
2) Squeeze all empty lines into just 1 empty line:
:v/./,//-j
For more help see:
:h :j
:h [range]
:h :g
:h :v
Short Answer: ()V)kc<esc>
In normal mode, if you type () your cursor will move to the first blank line. ( moves the cursor to the beginning of the previous block of non-blank lines, and ) moves the cursor to the end (specifically, to the first blank line after said block). Then a simple d) will delete all text until the beginning of the next non-blank line. So the complete sequence is ()d).
EDIT: You're right, that deletes the whitespace at the beginning of the next non-blank line. Instead of d) try V)kd. V puts you in visual line mode, ) jumps to the first non-blank line (skipping the whitespace at the beginning of the line), k moves the cursor up one line. At this point you've selected all the blank lines, so d deletes the selection.
Finally, type O (capital O) followed by escape to crate a new blank line to replace the ones you deleted. Alternatively, replacing dO<Escape> with c<Escape> does the same thing with one less keystroke, so the entire sequence would be ()V)kc<Esc>.
These answers are irrelevant after the updated question:
This may not be the answer you want to hear, but I would make use of ranges. Take a look at the line number for the first empty line (let's say 55 for example) and the second to last empty line (perhaps 67). Then just do :55,67d.
Or, perhaps you only want there to ever be one empty line in your whole file. In that case you can match any occurrence of one or more empty lines and replace them with one empty line.
:%s/\(^$\n\)\+/\r/
This answer works:
If you just want to use normal mode you could search for the last line with something on it. For instance,
/.<Enter>kkVNjd
I didn't test so much, but it should work for your examples. There maybe more elegant solutions.
function! DelWrapLines()
while match(getline('.'),'^\s*$')>=0
exe 'normal kJ'
endwhile
exe 'silent +|+,/./-1d|noh'
exe 'normal k'
endfunction
source it and try :call DelWrapLines()
I know this question has already been resolved, but I just found a great solution in "sed & awk, 2nd Ed." (O'Reilly) that I thought was worth sharing. It does not use vim at all, but instead uses sed. This script will replace all instances of one or more blank lines (assuming there is no whitespace in those lines) with a single blank line. On the command line:
sed '/ˆ$/{
N
/ˆ\n$/D
}' myfile
Keep in mind that sed does not actually edit the file, but instead prints the edited lines to standard output. You can redirect this input to a file:
sed '/ˆ$/{
N
/ˆ\n$/D
}' myfile > tempfile
Be careful though, if you try to write it directly to myfile, it will just delete the entire contents of the file, which is clearly not what you want! After you write the output to tempfile, you can just mv tempfile myfile and tada! All instances of multiple blank lines are replaced by a single blank line.
Even better:
cat -s myfile > temp
mv temp myfile
cat is awesome, yes?
Bestest:
If you want to do it inside vim, you can replace all instances of multiple blank lines with a single blank line by using vim's handy feature of executing shell commands on a range of lines within vim.
:%!cat -s
That's all it takes, and your entire file is reformatted all nice!

What is the best way to refactor a Ruby ‘if’ statement into one-line shorthand form in Vim?

I have the following Ruby code:
if some_cond && another
foo_bar
end
and I want to change it to:
foo_bar if some_cond && another
What are the most idiomatic ways to do that in Vim?
Assuming that the cursor is located at the if-line prior to
refactoring (not necessarily at the beginning of that line),
I would use the following sequence of Normal-mode commands:
ddjVpkJ<<
or this chain of Ex commands:
:m+|-j|<|+d
Here the if-line is first moved down one line by the :move + command.
The :move command cuts a given range of lines (the current line, if
not specified) and pastes it below the line addressed by the argument.
The + address is a shorthand for .+1 referring to the next line
(see :help {address}).
Second, the line containing the body of the conditional statement is
joined with the just moved if-line. The :join command concatenates
a given range of lines into a single line. The - range is a shortened
form of the .-1 address referring to the line just above the cursor
(see :help {address}).
Third, the newly joined line is unindented by one shiftwidth using
the :< command.
Finally, the remaining end-line, which can now be addressed as +,
is removed by the :delete command.
I see few (probably non-optimal) solutions:
cursor in first character in first line:
D - remove if condition but leave cursor in same position (don't delete line)
J - join next line to current
A <Space> <ESC> - append space and exit to Normal mode
p - paste if condition
and then remove remaining end with jdd
cursor in first character in first line, as previously:
j - move to next line
dd - remove this line
k - move back to if condition
P - paste removed line before actual line, cursor should be placed to pasted line
J - join next line to current
== or << - unindent current line
and then remove remaining end with jdd
another solution:
j - move to second line
JD - join line with next, remove what was joined
dd - remove current line
k - step to previous line
PJ<< - paste, join and unshift
It's probably not optimal, but I do it without thinking, because most of this commands are in my muscle memory (you don't think how to move around you, how to yank/delete and paste most of the time, and joining line is also helpful to remember).
If you have virtualedit enabled in config, instead of A <Space> <Esc> you can $ <Space>, but I find $ harder to use than A followed by Ctrl-[ (it's simmilar to ESC).
As an advice: if you use some upper letter commands, try to chain them if it's possible, so you only need to keep Shift pressed and then execute some commands, instead of mixing upper and lower letter commands and pressing two keys at a time (upper letter is 2 key press, one is Shift). Once I found combo helpful for restarting server in console Ctrl+cpj, which sends Ctrl+c, Ctrl+p (previous command) and Ctrl+j (Enter key) with single Ctrl press. Since then I try to find simmilar time-saving combination in Vim too mostly with Shift, as Ctrl is not much used in Vim.
Yet another way:
ddpkJjdd
ddp swap the two lines
kJ move up and join the lines
== re-indent the line
jdd move down and delete the last line
There are probably 30 ways to do this.
Here is one, assuming you are starting from the end of the word end in normal mode:
dd (delete last line)
"aD (delete and copy foo_bar to buffer a)
dd (delete now-empty line 2)
"aP (paste from buffer a before caret)
aSpaceEsc (insert space and return to normal mode)
Again, "properly" rarely applies in Vim because there are so many ways to accomplish something. This is so small a change that even re-typing foo_bar could be justifiable.

Resources