79

I can't figure out how to configure notepad++ to display regions for user defined keywords.

I have a big trace file which shows the start and the end of a procedure. The trace file looks like this:

Beginn abc.def;
   ...
   Beginn ghi.jkl;
   ...
   Ende ghi.jkl;
   ...
Ende abc.def;

I would like to fold those regions like this:

[+] Beginn abc.def;

or

[-] Beginn abc.def;
       ...
[+]    Beginn ghi.jkl;
       ...
    Ende abc.def;

How do I configure my notepad++ to display this. Is it possible? Do you have any other suggestions?

Stephan Schielke
  • 2,744
  • 7
  • 34
  • 40
  • http://stackoverflow.com/questions/9251858/hide-comments-in-notepad Duplicate (2010 is the earliest I could find). – Wolfpack'08 Feb 27 '16 at 20:53

7 Answers7

62

For version 6.5.5 and above:

Under the menu "Language" there is a menuitem called "Define your language..."

enter image description here

In the tab "Folder & Default" is a group called "Folding in code" where you can enter an "Open"- and a "Close"-Keyword.

Folding in code

For versions older than 6.5.5:

Under the menu "View" there is a menuitem called "User-Defined Dialog..."

View Define your language

In the tab "Folder & Default" you can enter a "Folder Open Keyword" and a "Folder Close Keyword"

Folder Open Close Keyword

Stephan Schielke
  • 2,744
  • 7
  • 34
  • 40
  • 4
    Would there be any way to add these to an existing language definition? Specifically, could I add in comments to create folding sections e.g: /**/ ... [lots of code] ... /**/ – RSG Jan 20 '11 at 08:45
  • As you can see in the picture there is a way to imort and export the language files. Notepad++ stores the existing language configs. Maybe you can import them to the user defined ones and edit them. Good luck. – Stephan Schielke Jan 21 '11 at 07:59
  • 6
    In version 6.5.5 this dialog is under the "Language" menu. I just added #region end #endregion and folding works great! – adg Mar 19 '14 at 23:32
  • 1
    @adg: I'd like to add `// #region` folding to Javascript too. How did you do it and to which language did you add it to? – Robert Koritnik Feb 28 '15 at 14:00
  • @StephanSchielke, It's under "Language >> Define your Language" now – Pacerier Dec 29 '15 at 09:56
  • @adg and Pacerier Changed the description and pics. – Stephan Schielke Jul 01 '16 at 09:34
  • Notepad++ only allows you to specify a few Folding Keyword styles. Is there a way to appoint more keyword opener/closers to be used to close code? I'm using a language that uses very arcane keywords for blocking structures (Basic variant: Sub()/End, Function()/EndFunction, Table()/EndTable, etc.) – Minh Tran Oct 31 '17 at 18:46
58

Another simple way is just add "comment command" and then " { " to begin and later "comment command" and then " } ". In C/C++, Java, Javascript, etc. it would be just like this :

// {

// }
Marcelo Ruggeri
  • 1,939
  • 1
  • 17
  • 10
  • 2
    As of NPP v6.4.3 the quick-and-easy user defined dialogue seems to be gone (or rolled into user defined language syntax). So that makes it non-trivial to simply add a collapsible region syntax to a built-in language. But this solution from Marcelo is very quick and easy and works in NPP 6.4.3. – Todd Aug 19 '13 at 14:18
  • 7
    Thanks! Now I do: `//{ #region LOADING` and `//} #endregion LOADING` in Javascript – Szymon Toda Nov 24 '13 at 11:47
  • 1
    How is "add 'comment command'" performed ? I have the same code as shown but no folding option. I suppose the block must be selected and hidden with Alt+h ? – Goufalite Dec 30 '14 at 13:43
  • 3
    I'm using `Notepad++ 6.7.9.2`. In this version you need to remove the space between the beginning of the comment and the bracket. So `//{` works but `// {` does not. – jeremysawesome Jul 29 '15 at 04:16
16

I have a similar problem. I want to add a custom tag like #region / #endregion to create arbitrary folding points in languages that don't support it. Specifically, I am trying to do this for php.

After researching for an hour or two, it seems that modifying an existing language is quite difficult due to the underlying scintilla lexer, and writing a plugin may be the only way to accomplish this.

I did discover however a decent workaround:

Wrap the code you wish to fold in comments like:

#{ 
...
#}

Then move your cursor before the open brace and press CTRL+ALT+b to highlight the entire block, followed by ALT+h to hide it.

It's a different operation than folding, but it works in a pinch.

Kevin Jhangiani
  • 1,597
  • 2
  • 13
  • 23
  • What do these shortcuts do exactally? They aren't working for me, maube it's a plugin you have installed? – Hikari Oct 06 '13 at 20:38
  • 2
    Alt+H worked, but it seems to be temporary only. I just need to expand it for it to disappear – Hikari Oct 06 '13 at 20:39
  • Hi! At least, you could have mentioned that you copied my answer, just like "user1881282" did, but I´m not thaaat much worried about it. Have a nice day ! – Marcelo Ruggeri Jul 02 '14 at 19:10
  • 1
    CTRL + ALT + b to highlight block. (I disagree this is just copied from Marcelo's answer - that makes no mention of using block select and hide instead of folding, which is a key point in this answer) – Nick Rice Oct 18 '14 at 09:56
  • Thanks for the correction on the keystroke @Nick Rice, I have corrected the answer to be CTRL+ALT+B to highlight the block, which does appear to work (cursor must be between # and { characters) – Kevin Jhangiani Nov 26 '14 at 11:21
4

I used Marcelo's answer to solve this for myself (in Perl), with one change...
If I included a space between the comment symbol and the bracket then it wouldn't work. It had to be placed immediately after:

#START example
################{

print "Hi there! ";
print "How are you?\n";

#}END example

Note that if I do:

#END example }

with the bracket after the text it won't work either

user1881282
  • 152
  • 2
  • 7
0

!! Disclaimer: Total noob at PHP !!

Assuming that you are wrapping your PHP in some HTML, you can define custom sections using <?php and?>.

Ex:

<html>
<head></head>
<body>
<?php 
$cities[1] = "Phoenix";$cities[2] = "Tucson";$cities[3] = "Flagstaff";  
$capitals['CA'] = "Sacramento";$capitals['TX'] = "Austin";$capitals['OR'] = "Salem";    
$cities[] = "Phoenix";$cities[] = "Tucson";$cities[] = "Flagstaff"; 
echo "Here are cities: $cities[1]";echo nl2br("\n");
?>
</br>
<?php
$cities = array( "Phoenix","Tucson","Flagstaff");   
$capitals = array( "CA" => "Sacramento","TX" => "Austin","OR" => "Salem",);
echo $capitals['TX'];echo nl2br("\n");  
?>
<body>
</html>

Notepad++ will then allow you to collapse the sections arbitrarily and you can label the sections with "#". Just learning PHP, so my code might not be awesome; It's still an ongoing process.

ITGuyOU
  • 29
  • 5
-1

If it's SQL, then encapsulating your code with BEGIN and END works well. The BEGIN statement is shown, plus any comments you add on the same line.

e.g:

BEGIN --creating temp table with eligible users

   ...code

END
Rahil Wazir
  • 10,007
  • 11
  • 42
  • 64
jeminar
  • 27
-2

STEP ONE: Add a unique key with open and close (i.e.,

#1
{{{
#2
{{{
#2
}}}
##
}}}

Use indentation and/or comments to indicate nest-level.

Step 2: {CTRL}-H when you're finished. Replace all '{{{' and '}}}'.

STEP 3: Comment strip (app).

Wolfpack'08
  • 3,982
  • 11
  • 46
  • 78