Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div class="content">
	<p class="mb-0">CodeMirror: Minimum Lines</p>
	<textarea class="editor" style="display: none;">var str = "Hello, World!"; 
function doSomething (str) {
	console.log(str);			
}</textarea>
	<a href="https://codepen.io/Souleste/pen/QWpGBva" target="_blank">CodeMirror Maximum Number of Lines</a>
</div>
              
            
!

CSS

              
                .content {
	width: 450px;
	position: absolute;
	top: 50%;
	left: 50%;
	transform: translate(-50%, -50%);
}
.CodeMirror {
	height: auto !important;
	border-radius: 5px;
}
.CodeMirror-sizer {
	padding-bottom: 10px !important;
}
.CodeMirror-scroll {
	min-height: 200px;
}

              
            
!

JS

              
                // set minimum number of lines CodeMirror instance is allowed to have
(function (mod) {
	mod(CodeMirror);
})(function (CodeMirror) {
	var fill = function (cm, start, n) {
		while (start < n) {
			let count = cm.lineCount();
			cm.replaceRange("\n", { line: count - 1 }), start++;
			// remove new line change from history (otherwise user could ctrl+z to remove line)
			let history = cm.getHistory();
			history.done.pop(), history.done.pop();
			cm.setHistory(history);
			if (start == n) break;
		}
	};
	var pushLines = function (cm, selection, n) {
		// push lines to last change so that "undo" doesn't add lines back
		var line = cm.lineCount() - 1;
		var history = cm.getHistory();
		history.done[history.done.length - 2].changes.push({
			from: {
				line: line - n,
				ch: cm.getLine(line - n).length,
				sticky: null
			},
			text: [""],
			to: { line: line, ch: 0, sticky: null }
		});
		cm.setHistory(history);
		cm.setCursor({ line: selection.start.line, ch: selection.start.ch });
	};

	var keyMap = {
		Backspace: function (cm) {
			var cursor = cm.getCursor();
			var selection = {
				start: cm.getCursor(true),
				end: cm.getCursor(false)
			};

			// selection
			if (selection.start.line !== selection.end.line) {
				let func = function (e) {
					var count = cm.lineCount(); // current number of lines
					var n = cm.options.minLines - count; // lines needed
					if (e.key == "Backspace" || e.code == "Backspace" || e.which == 8) {
						fill(cm, 0, n);
						if (count <= cm.options.minLines) pushLines(cm, selection, n);
					}
					cm.display.wrapper.removeEventListener("keydown", func);
				};
				cm.display.wrapper.addEventListener("keydown", func); // fires after CodeMirror.Pass

				return CodeMirror.Pass;
			} else if (selection.start.ch !== selection.end.ch) return CodeMirror.Pass;

			// cursor
			var line = cm.getLine(cursor.line);
			var prev = cm.getLine(cursor.line - 1);
			if (
				cm.lineCount() == cm.options.minLines &&
				prev !== undefined &&
				cursor.ch == 0
			) {
				if (line.length) {
					// add a line because this line will be attached to previous line per default behaviour
					cm.replaceRange("\n", { line: cm.lineCount() - 1 });
					return CodeMirror.Pass;
				} else cm.setCursor(cursor.line - 1, prev.length); // set cursor at end of previous line
			}
			if (cm.lineCount() > cm.options.minLines || cursor.ch > 0)
				return CodeMirror.Pass;
		},
		Delete: function (cm) {
			var cursor = cm.getCursor();
			var selection = {
				start: cm.getCursor(true),
				end: cm.getCursor(false)
			};

			// selection
			if (selection.start.line !== selection.end.line) {
				let func = function (e) {
					var count = cm.lineCount(); // current number of lines
					var n = cm.options.minLines - count; // lines needed
					if (e.key == "Delete" || e.code == "Delete" || e.which == 46) {
						fill(cm, 0, n);
						if (count <= cm.options.minLines) pushLines(cm, selection, n);
					}
					cm.display.wrapper.removeEventListener("keydown", func);
				};
				cm.display.wrapper.addEventListener("keydown", func); // fires after CodeMirror.Pass

				return CodeMirror.Pass;
			} else if (selection.start.ch !== selection.end.ch) return CodeMirror.Pass;

			// cursor
			var line = cm.getLine(cursor.line);
			if (cm.lineCount() == cm.options.minLines) {
				if (
					cursor.ch == 0 &&
					(line.length !== 0 || cursor.line == cm.lineCount() - 1)
				)
					return CodeMirror.Pass;
				if (cursor.ch == line.length && cursor.line + 1 < cm.lineCount()) {
					// add a line because next line will be attached to this line per default behaviour
					cm.replaceRange("\n", { line: cm.lineCount() - 1 });
					return CodeMirror.Pass;
				} else if (cursor.ch > 0) return CodeMirror.Pass;
			} else return CodeMirror.Pass;
		}
	};

	var onCut = function (cm) {
		var selection = {
			start: cm.getCursor(true),
			end: cm.getCursor(false)
		};
		setTimeout(function () {
			// wait until after cut is complete
			var count = cm.lineCount(); // current number of lines
			var n = cm.options.minLines - count; // lines needed
			fill(fm, 0, n);
			if (count <= cm.options.minLines) pushLines(cm, selection, n);
		});
	};

	var start = function (cm) {
		// set minimum number of lines on init
		var count = cm.lineCount(); // current number of lines
		cm.setCursor(count); // set the cursor at the end of existing content
		fill(cm, 0, cm.options.minLines - count);

		cm.addKeyMap(keyMap);

		// bind events
		cm.display.wrapper.addEventListener("cut", onCut, true);
	};
	var end = function (cm) {
		cm.removeKeyMap(keyMap);

		// unbind events
		cm.display.wrapper.removeEventListener("cut", onCut, true);
	};

	CodeMirror.defineOption("minLines", undefined, function (cm, val, old) {
		if (val !== undefined && val > 0) start(cm);
		else end(cm);
	});
});

const $ = function (selector, bind) {
	var bind = bind === undefined ? document : bind;
	let nodes = bind.querySelectorAll.bind(bind)(selector);
	return nodes.length == 1 ? nodes[0] : nodes;
};
var textarea = $(".editor");
var editor = CodeMirror.fromTextArea(textarea, {
	theme: "berry-dark",
	autoRefresh: true,
	firstLineNumber: 1,
	lineNumbers: true,
	smartIndent: true,
	lineWrapping: true,
	indentWithTabs: true,
	refresh: true,
	minLines: 10
});

              
            
!
999px

Console