Does Sublime have code narrowing like emacs or vim (https://github.com/chrisbra/NrrwRgn). Is there any plugin enabling this?
Does Sublime Text have code narrowing?
As far as I know, Sublime does not have this and I don’t know of a package that does - you would be able to apply most commands on a selection of code, but it’s not able to open a new buffer.
Why not?
Copy from current buffer the selected region.
Use .run_command(“new_file”) in your plugin to open a new buffer.
Than paste and save with a temporary file name.
Now you can edit it.
In the second step select the edited code and replace selection in the source file with your edited code.
Than delete the temporary file.
[size=150]Edit[/size]
Now I’ve made a very simple code narrowing package. I’ve tried to attache it (only 2kB size) but the forum says: Quota is reached.
OK, so I’ll post all the stuff here.
Create a new folder (“Narrowing”) in your package path.
Create the following 2 files and store them in this folder.
"narrowing.py"
[code]import sublime
import sublime_plugin
import os
import re
SETTINGS_FILE = “narrowing.sublime-settings”
extensions = None
regions = None
src_file = None
tmp_file = None
class NarrowCommand(sublime_plugin.TextCommand):
window = None
# param “action” = ‘edit’ / ‘replace’
def run(self, edit, action=‘edit’):
global regions
global src_file
global tmp_file
self.window = sublime.active_window()
if action == ‘edit’:
sel = self.view.sel()
if not sel:return
regions = ]
string_insert = ‘’
src_file = self.view.file_name()
name, ext = os.path.splitext(src_file)
tmp_file = ‘%s%s%s’ % (name, ‘TEMP’, ext)
ext = ext[1:]
comment_char = extensions[ext.lower()]
if not comment_char:comment_char = “#”,""]
delimiter_line = “%s ===== DELIMITER LINE ===== %s\n” % (comment_char[0],comment_char[1])
n = 0
for region in sel:
n += 1
text = self.view.substr(region)
regions.append([region.a, region.b])
sdelim = delimiter_line if n > 1 else ‘’
string_insert = ‘%s%s%s\n’ % (string_insert, sdelim, text)
with open(tmp_file, ‘w’) as f:
f.write(string_insert)
self.window.open_file(tmp_file)
elif action == 'replace':
sdelim = '===== DELIMITER LINE ====='
# be sure, that TMP file is active
view_tmp = self.window.find_open_file(tmp_file)
self.window.focus_view(view_tmp)
# read all text
region = sublime.Region(0, self.view.size())
text = self.view.substr(region)
# if text has delimiter --> split to parts
if sdelim in text:
# replace delimiter line with one character and split by this
patt = re.compile('\n(.+===== DELIMITER LINE =====.+)\n')
text = re.sub(patt, chr(7), text)
replace = re.findall('(^\a]+)', text)
else:
replace = [text]
# close TMP file
self.window.run_command("save")
self.window.run_command("close")
# activate the source buffer
view_src = self.window.find_open_file(src_file)
self.window.focus_view(view_src)
selection = view_src.sel()
selection.clear()
# replace from end to start
for i in range(len(replace)-1, -1, -1):
region = sublime.Region(regions*[0],regions*[1])
view_src.replace(edit, region, replace*)
# delete TMP file
os.remove(tmp_file)
regions = None
src_file = None
tmp_file = None
def plugin_loaded():
global extensions
settings = sublime.load_settings(SETTINGS_FILE)
extensions = settings.get(“extensions”)
[/code]
"narrowing.sublime-settings"**
{
"extensions":
// Insert for each of your file type the starting and ending
// comment characters for line comments.
// It's required to insert delimiter lines if you have more than
// one region selected.
{
"py": "#", ""],
"css": "/*", "*/"],
"htm": "<!--", "-->"],
"html": "<!--", "-->"],
"au3": ";", ""]
}
}
Now you need a key binding to run the 2 actions. Thats from my keymap:
{ "keys": "shift+f12"], "command": "narrow" },
{ "keys": "ctrl+shift+f12"], "command": "narrow", "args": {"action": "replace"} },
The first command opens a new file with selected code. This file has the name “path/FileNameSource__TEMP__.extension”.
If more than one region is selected, this parts are delimited with a line, that uses line comment characters from this filetyp. Never delete this line!!
Now you can edit this code.
With second command your selection(s) in the source file will replaced with the changed code. The temporary file will deleted.*
I know this thread is old, but I came across it while searching for something else on Google. If anyone else finds this thread and is still looking for this functionality, I (in a funny coincidence) have created another plugin for it called FeaturePresentation, available on Package Control.
FeaturePresentation