I use vim's block insert to comment blocks of code like this (think of the square brackets as the cursor):
[k]eyword {
variable declaration;
return_type function_or_something() {
for (a; b; c) {
do_something_funky();
}
}
Hit Ctrl+V 6jI// Space Esc
[/]/ keyword {
// variable declaration;
// return_type function_or_something() {
// for (a; b; c) {
// do_something_funky();
// }
// }
All nice and neat, but in this situation:
[k]eyword { // this is a block of code with a somewhat long comment
variable declaration;
return_type function_or_something() {
for (a; b; c) {
do_something_funky();
}
}
Hit Ctrl+V 6jI//, at this point the combination of textwidth=80 and formatoptions=caq1njw kick in, and since my // has pushed the line over the textwidth, it triggers the wrap, and once that happens, my visual block insert operation is toast, so only the first line actually ends up getting commented out, and as a result I let loose a string of expletives.
[/]/keyword { // this is a block of code with a somewhat long
//comment
variable declaration;
return_type function_or_something() {
for (a; b; c) {
do_something_funky();
}
}
Seems like a long shot, but I wonder if anyone knows of a workaround/fix... Maybe I can temporarily blow away formatoptions when I start a visual block selection? How to do that?
Note: I tried hitting o right before I to put the cursor at the bottom edge of the block selection, but it doesn't help, the way it works is once you hit I it will reposition the cursor to the top line, which subsequently screws you over as you turn the line into a comment.
Vim does not recognize code that is commented out and normal comments. I do not think such a thing exists. Your 'formatoptions' specify that a inserted text in comments should reflow. You are inserting a comment so the comment will reflow. You can avoid this by not doing an insert but something else instead, like a substitution.
:s#^#//
However by far the easiest solution would be to use one of the many comment plugins. There are many options: commentary (which I use), Nerd Commenter, EnhCommentify, tComment, ..., etc.
Related
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)
Assuming I have the following code:
bool myCopiedFunc() {
Some code that I've written;
The cursor is on this line; <<<<<<<<<<<<<<
if (something) {
bool aValue;
some of this is inside braces;
return aValue;
}
if (somethingElse) {
this is also inside braces;
bool anotherValue;
{
more braces;
}
return anotherValue;
}
return false;
}
I decide I want to rewrite the remainder of the function, from the line with the cursor on it.
To replace up to a char on the same line, I can use ct<char> e.g. ct;
To replace up to and including a char on the same line I can use cf<char> e.g. cf;
To replace up to a string across multiple lines, I can use c/<string> e.g. c/return false
To replace up to and including a string across multiple lines, I can use... ?? e.g. ??
I can't just search for a semicolon, as there are an unknown number of them between the cursor and the end of the function, and counting them would be slow.
I can't just search for a closing brace, as there are several blocks between the cursor and the end of the function, and counting all closing braces would be slow.
With the help of code highlighting, I can easily see that the unique string I can search for is return false.
Is there an elegant solution to delete or change up to and including a string pattern?
I've already looked at a couple of related questions.
Make Vim treat forward search as "up to and including" has an accepted answer which doesn't answer my question.
In my case, I settled for deleting up to the search string, then separately deleting up to the semicolon, but it felt inefficient, and like it would have been quicker to just reach for the mouse. #firstworldproblems
To replace up to and including a string across multiple lines, I can
use... ?? e.g. ??
The / supports offsets.
In your case, you are gonna need the e offset, that is, c/foo/e.
You may want to know more details about "search offset":
:h offset
If you'll replace up to the closing brace associated to your current scope, you have c]}.
If you're looking for the end of the function, even if it means crossing to the upper scope, you'll need a plugin if the function may not be 0-indented as it's the case in C++, Java... See the related Q/A on vi.SE
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.
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>]}
This question already has answers here:
Closed 13 years ago.
Duplicate:
How to indent code in vim editor in Windows?
Tabbing selected section in VIM
Sometimes I want to indent a block of C code in Vim. I usually ended up tabbing it line by line.
What is a better/faster way?
I'm surprised no one came up with =% yet.
Make sure you have :set cindent,
Place yourself on one of the {} of your block, and just hit:
=%
All code within this block will be correctly indented.
Use '>' to tab a block
Enter visual mode, select to the next matching bracket, indent:
V
%
>
While insert: C-d, C-t
While visual: >, <
While normal: >>, <<
In any of this modes use '.' to indent further.
Try
:set cindent
This will turn on C indenting magic in vim. So as soon as you open a brace, it will automatically tab until you close the brace.
If you have unindented code that looks like this...
if (foo)
{
/* line 1 */
/* line 2 */
/* line 3 */
}
...place your cursor on "line 1" in command mode and type 3==, where 3 is the number of lines to indent.
I think this will do it without any indent switches being set.
:startRange,stopRange s/^/^\t/g
should add a tab space at beginning of line between the line number range you provide
unindent with:
:startRange,stopRange s/^\t/^/g
In addition to what skinp said, if you have:
int foo()
{
/* line 1 */
/* line 2 */
/* line 3 */
/* line 4 */
}
and for whatever reason wish it to look like this (i.e. you want everything indented 4 spaces* from where they were previously, rather than indenting 'correctly' according to the cindent rules):
int foo()
{
/* line 1 */
/* line 2 */
/* line 3 */
/* line 4 */
}
anywhere within the block, do viB> (visualselection innerBlock indent)**
* or whatever your shiftwidth is set at
** vi} is the same as viB, and may be easier to remember since vi} selects within {}, vi) selects within (), vi] selects within [], and vi> selects within <>.
Also, va}, va), etc. select the {}, (), etc in addition to what's contained within the block.