Vim Multi-Line If-Statements into Single-Line If-Statements - vim

Is it possible to have a vim binding that turns multi-line if-statements into single-line if-statements and vice-versa?
turn this
if () {
statement_1;
statement_2;
} else if () {
statement_3; statement_4;
} else if () {
statement_5;
} else {
}
into this
if () { statement_1; statement_2 }
else if () { statement_3; statement_4; }
else if () { statement_5; }
else { }
or anything close to the above behavior? I was thinking of having the command execute upon visually selecting the block to convert and then use searches for else if and entering new lines, etc. But my problem was determining how many else if were in the code

Join all the lines to a single line by selecting them in visual mode V and pressing J; then add a newline before every else with :s/else/\relse/. You'll end up with:
if () { statement_1; statement_2; }
else if () { statement_3; statement_4; }
else if () { statement_5; }
else { }
The \r in the replacement pattern is a newline (you need to use \n and search and \r in replace; don't ask me why).
Next step is to put all the start braces in the same column. I'd use the tabular plugin for this, which makes that very easy:
:%Tabularize /{/
With % we operate on the entire buffer, in a "real" file you'll probably want to use a more restrictive range or visual mode. There are some other plugins which do something similar as well.
You should now have the output you want.
If you don't want to use a plugin, you can use the column command:
:%!column -ts{ -o{
If you want a "Vim-only" solution, then it's a bit more complex:
:let b:column = 10
:%s/^\(.\{-}\) {/\=submatch(1) . repeat(' ', b:column - len(submatch(1))) . ' {'/
Breaking that down:
I used the b:column variable to specify the column to align to. You don't need this but it make it a bit easier to edit this number later on.
^\(.\{-}\) { puts everything before { in a subgroup.
In the replace we used an expression (as indicated with \=). See :help sub-replace-\=.
First we put the if ... back with submatch(1)
Then we insert as many spaces as we need with repeat(' ', b:column - len(submatch(1)))
Finally we insert the literal { back.
I told you it was a bit more complex ;-) If you don't want tabular. Personally, I'd just start insert mode to insert spaces, which will be a lot faster than writing & debugging this (relevant xkcd).
Note that I didn't make some "magic" command which re-arranges all the text with just a stroke of a key. I don't think such a command would be a good idea. In practice there will be a lot of edge cases that such a command won't handle. Fully "parsing" a programming language with ad-hoc editing commands and/or regexps doesn't really work all that great.
Where Vim really shines is giving the user powerful text editing commands, which can be applied and combined with minimal effort, which is exactly what I did above. There are several other ways one can use to get the same effect.
But if you really want to, you can of course combine all of the above in a command:
fun! s:reformat(line1, line2)
" Remember number of lines for later
let l:before = line('$')
" Join the lines
execute 'normal! ' . (a:line2 - a:line1 + 1) . 'J'
" Put newline before else
:s/else/\relse/
" Run tabular; since the number of lines change we need to calculate the range.
" You could also use one of the other methods here, if you want.
let l:line2 = a:line2 - (l:before - line('$'))
execute a:line1 . ',' . l:line2 . 'Tabularize /{/'
endfun
command! -range Reformat :call s:reformat(<line1>, <line2>)

Related

How to take curly brace to end of previous line

I see many codes like following on the net:
public static void read()
{
using (StreamReader m_StreamReader = new StreamReader("C:\\myCsv.Csv"))
{
while (m_StreamReader.Peek >= 0)
{
string m_Str = m_StreamReader.ReadLine;
string[] m_Splitted = m_Str.Split(new char[] { "," });
Console.WriteLine(string.Format("{0}={1}", m_Splitted[0], m_Splitted[1]));
}
}
}
However, I want to convert above to following:
public static void read() {
using (StreamReader m_StreamReader = new StreamReader("C:\\myCsv.Csv")) {
while (m_StreamReader.Peek >= 0) {
string m_Str = m_StreamReader.ReadLine;
string[] m_Splitted = m_Str.Split(new char[] { "," });
Console.WriteLine(string.Format("{0}={1}", m_Splitted[0], m_Splitted[1]));
}
}
}
Hence starting curly brace is taken to the end of previous line. How can this be done programmatically in Vim? It tried but though I can pick up starting curly brace but could not manage to take it to end of previous line.
Note: above code is from here.
Joining the next line to the current line is done with :help J in normal mode or :help :join in command-line mode.
Joining the current line to the previous line is done in normal mode by moving the cursor to the previous line with - and then joining with J. In command-line mode, you would use -, short for .-1 ("current line number minus one"), as :h address for :join: :-j, which mirrors the normal mode method quite well.
To do this on the whole buffer, you need a way to execute a given command on every isolated opening brace. This is done with :help :g:
:g/^\s*{\s*$/-j
Breakdown:
:g/<pattern>/<command> executes <command> on every line matching <pattern>,
^\s*{\s*$ matches lines with a single opening braces and optional leading and trailing whitespace,
-j joins the current line with the line above.
But the result is not correctly indented anymore so you will need something like the following command to fix the mess:
gg=G
Breakdown:
gg moves the cursor to line 1,
=G re-indents every line from the cursor to the last line, see :help =.
That said, switching from one coding style to another seems like something that should be done with a dedicated tool rather than with general text editing.
The desired output looks strikingly similar to the ratliff style. If that's correct, astyle is one formatter that supports this particular style and this solution will be more robust than making changes manually.
You can wrap it in your own command as follows:
command! -buffer Fmt let winsaved = winsaveview() | execute '%! astyle --style=ratliff' | if v:shell_error > 0 | silent undo | endif | call winrestview(winsaved)

Way to remove two non-consecutive lines in vim

Is there a way to remove two non-consecutive lines with a single vim command (which could be undone in a single step).
I have file like this:
if (enabled) {
const a = 1;
const b = 2;
console.log(a + b);
}
And would like to end up with this:
const a = 1;
const b = 2;
console.log(a + b);
Use visual mode and paste over the whole block.
yiBvaBVp
Vimcasts episode: Pasting from Visual mode
For more help see:
:h iB
:h aB
:h v_p
If you select your example (with v$% for instance), then
:'<,'>g/{\|}/d
will give you what you want (if there are no nested { or }).
Another way would be to record a macro, something like
# qa to start recording, the final q to stop. ^O is <Ctrl-o>
qaf{%dd^Oddq
then use it with #a.
Edit: more general solution (still using visual mode though):
You could add a mapping:
:xnoremap <leader>d <esc>:'<d \| '>d<cr>
to delete the first and last line of the last visual selection.

Using printf and substitute changes visual selection

I've been working on implementing a simple utility into Vim which allows me to visually select some text, calculate the result, then replace the original text with the result. I've run into a problem, however: it appears that somehow using substitute in conjunction with printf changes my visual selection.
Here's the code:
vnoremap <leader>C y<ESC>:call setreg( '"', printf( '%f', eval( substitute( substitute( getreg( '"' ), '\n', '', 'g' ), '\(\d\+\.\=\d*\)\s*\^\s*\(\d\+\.\=\d*\)', 'pow( \1, \2 )', 'g' ) ) ), getregtype( '"' ) )<CR>gvp
Now, it's a bit lengthy, but I'm working on fixing that. Basically, it takes the visually selected text, yanks it, then sets the " register to contain the evaluated response, then re-selects the last visual selection, then pastes it, replacing the visual selection with the result.
As an example, say I have this line: a = 32 - 2. Visually select 32 - 2, then hit <leader>C. The line ends up being a = 30.0000002 - 2.
The problem is in the last bit of the last substitution: when the '\(\d\+\.\=\d*\)\s*\^\s*\(\d\+\.\=\d*\)'
bit doesn't have the last \.\=\d*, the code runs fine (given the exponent/index isn't a float). Without printf, it works fine given there are no floats. With printf and the last bit, only the first character is replaced, but the output is correct.
I need to use printf, though, because that allows me to use floats. I also need to do that substitution, because it allows me to use exponents.
What's happening, and how do I fix it? It doesn't make since that the visual area would be changed just by using that code inside of printf. Am I just missing something really obvious?
Apparently the unnamed register doesn't survive mode changes. However, according to the manual:
Writing to the "" register writes to register "0.
And indeed, changing gvp to gv"0p makes your macro work. However, I suggest using a slightly safer version instead:
function! s:expr_eval()
let old_expr = #"
let reg_type = getregtype('"')
let expr = substitute(old_expr, '\n', '', 'g')
let expr = substitute(expr, '\v(\d+%(\.\d*)?)\s*\^\s*(\d+%(\.\d*)?)', 'pow(\1, \2)', 'g')
try
let expr = printf('%f', eval(expr))
catch
let expr = old_expr
endtry
call setreg('"', expr, reg_type)
endfunction
vnoremap <silent> <leader>C y:<C-u>call <SID>expr_eval()<CR>gv"0p

Vim keyboard shortcut: Deleting code without deleting the if statement

This is probably a basic vim questions to all those vim gurus out there.
If I want to delete a particular if statement and its closing bracket, but without deleting the code inside the expression, how can I achieve this through some keyboard shortcuts?
eg:
if (a == 2) {
// do not delete this part.
for (int i = 1; i < 10; i++) {
// code
}
}
The result will be:
// do not delete this part.
for (int i = 1; i < 10; i++) {
// code
}
Thanks for any help.
Alternatively to Zachs solution, you could use this:
di{Vk]p
Explanation:
You start inside the { } block, and with di{ you delete inside the { } block.
Then, you select the the if statement, which you want to remove, which now is two lines. Vk selects both, and you paste the block you just deleted over it with p. The ] makes the pasted block be correctly indented (thanks, Kache)
There could be a more elegant solution to the pasting part, buth this was the first one that came to mind.
The anvantage over Zachs answer is that you don't have to move the cursor to the line with the if, you just need to be inside it (although you may run into trouble with nested {} blocks, which there usually are). In addition, you don't need to fix indentation.
Move your cursor anywhere on the line with the if statement. Here is the sequence of commands (will be explained after)
$%dd''.
Explanation:
$ goes to the end of the line (cursor is now on {).
% goes to the matching curly brace (or parentheses etc...). Cursor is now on } of the if statement
dd deletes the line the cursor is on (} is now deleted)
'' goes to the line of the last jump. The last jump was when we used %. (cursor is back on if statement line)
. Repeats the last command (dd). This deletes the line (if statement line is now deleted)
Result:
// do not delete this part.
for (int i = 1; i < 10; i++) {
// code
}
The indentation will be off so you can use gg=G (which correctly indents the file) to fix it.
If you do this alot you can make map it to a key in your .vimrc or you can make it into a macro
Making it into a mapping:
Example of a mapping in your .vimrc:
nnoremap <C-d> $%dd''.gg=G
Whenever you press control-d (you can choose any key, just replace <C-d>) Vim will run those commands, doing what I explained before followed by an entire file indentation this time. Make sure your cursor is on the if statement line when using this command otherwise there could be unintended results.

How do you select the entire PHP function definition?

In PHP, if I have a function such as:
function test($b) {
var $a = 0;
while ($a < b) {
$a += 3;
}
return $a;
}
and the cursor is on the $a += 3 line, is it possible to quickly select the entire function?
"v2aB" would select everything including the function braces but not the declaration function test($b)
Press V after the selection command you post, to convert the selection to line selection, and it will select the function declaration:
v2aBV
It's been a long time since this question was asked and answered, but I will add my own answer because it's the one I was looking for and none of the others work exactly like this one:
nnoremap vaf ?func.*\n*\s*{<cr>ma/{<cr>%mb`av`b
vmap af o<esc>kvaf
The first mapping, "Visual around function" or vaf, will jump back to the start of the function definition, regardless that the { is in the same line or the next one, and even if it's a lambda function, and visually select it characterwise to it's ending bracket. This works in PHP, Javascript and Go.
The user can then press V to turn to linewise select mode if she wants to.
The only problem that I found is that when I am in the body of a big function, but below a line that uses a lambda (let's say "small") function, this will stop searching at the beginning of the small function and select it's body instead of reaching the start of the big function and select all of its body.
function show_video_server(v_server) {
// this whole function should get selected
var something = function(){ /* this function gets selected */ };
// | the cursor is here when I type "vaf"
}
As a workaround I use the second mapping: vmap af o<esc>kvaf. It feels like a repetition or expansion of the selection. What it really does is abandon the selection and go to the line before it, and then try it agan. If the "big" function uses several lambda functions the user has to repeat the af several times to reach the big one.
Usually, vaf es enough. Sometimes vaf af or vaf af af is needed. Anyway, it's the closest I could get to what I wanted, so this is the version I'm using.
Here's a mapping that seems to work very well, no matter the nesting level.
:map t ? function <CR>f{vaBV
Here's another method that will work if you have function-level folding turned on: z c v
That closes the current fold and selects it, but it leaves it closed. If you want it to remain open: z c v $
If you have block-level folding turned on, you would have to close twice, since you're inside the while loop, so: 2 z c v
To enable PHP class/function folding: let php_folding = 1
simple way
nmap vaf va}V
I like this
nmap vaf [{?function<CR>:nohl<CR>vf{]}
if ‘{’ is in new line
nmap vaF [{?function<CR>:nohl<CR>v/{<CR>]}
Yet another way. This should select the entire function definition regardless of your cursor position within the definition, not just when you're at the $a += 3 line.
Use this in normal mode (<CR> means press enter)
?func<CR>V/{%
Explanation of each part:
?func search backward for the word "func" (the idea is to get to the first line of the function definition)
V go to visual line mode
/{ search forward for the opening brace (I didn't use f{ because the opening brace might be on a separate line)
% go to the matching brace
If you are using OOP programming this works (it looks for extra words before function[public, private, protected])
nmap vaf [{?\S* function<CR>:nohl<CR>v/{<CR>]}
As a bonus here is a wrapper around if
nmap vai [{?if<CR>:nohl<CR>v/{<CR>]}

Resources