Using recent Visual Studio Code, if I have something like this:
(defn add [x y]
(+ x y)
and then type a closing parens as the last character on the second line, the editor outdents the second line to the left margin. I mainly run into this sort of thing editing Clojure, but I tried the above in a Plain Text file to try to rule out extension problems, and I got the same outdenting behavior.
Does anyone know a setting I can change to disable this auto-outdenting?
Install Parinfer for the following indentation behavior:
CTRL-p (Linux and Windows) or CMD-p (macOS) for quick edit and ext install vscode-parinfer
Related
I'm writing a C++ program in Emacs, and I've encountered an irritating feature whereby when I type one of a set of seemingly random characters, it will indent the line I'm typing on.
For example:
cout<<"Case #"<<case<<": ";
Each time I typed the << operator, the line would be indented by two spaces, which I would then have to go back and remove. The same thing would occur when typing (. After a while, you start to get bored of this. Any idea why this might be happening?
The only style point I've changed from default is to set the 'style' variable to 'linux', and all I've got in my ~/.emacs is:
(setq backup-directory-alist (("." . "~/.saves")))
(setq tab-width 4)
You can toggle electric indentation on/off with c-toggle-electric-state (eg M-x ...). When turned off you shouldn't get an automatic indentation after typing << or (.
If you always want it off, it can be turned off in your c++-mode-hook, eg
(defun my-c++-mode-hook ()
;; ...
(c-toggle-electric-state -1))
(add-hook 'c++-mode-hook 'my-c++-mode-hook)
I'm completely new to linux/emacs (Using GNU Emacs 24.4.1 on debian) and I'm finding it incredibly frustrating to use. I'm trying to program in C++, however I'm finding the tab spacing is very small, it's not even a tab it's literally just a single space how do I fix this? I've tried adding this to my .emacs file in home but it didn't fix anything:
(setq-default indent-tabs-mode nil)
(setq-default tab-width 4)
(setq indent-line-function 'insert-tab)
Is it also possible to change copy/cut/paste/undo to the original
You can skip the config file for now and just use the GUI:
Options -> Customize Emacs -> Top-level Customization Group -> Editing -> Indent
You might also consider checking out "smart-tabs" if you want tab to insert spaces instead of tab characters: Smart Tabs (tabs for indentation, spaces for alignment) http://www.emacswiki.org/emacs/SmartTabs
As for changing the cut/copy/paste commands, try CuaMode (should be installed by default on Emacs 22.1.1 and later):
M-x cua-mode
That will change many of your editing hotkeys around to the more "standard" versions. See more info here: http://www.emacswiki.org/emacs/CuaMode
(I would, however, recommend against that, as it will delay the Emacs learning process. The standard commands become second-nature in a short while, and learning them will guarantee that you aren't stuck using nano or gedit if you have to shell into someplace with an older version of Emacs).
To change the indentation of c-like modes (c, c++, java, etc.) use
(setq c-basic-offset 4)
I love using emacs to compile my C++ project using compilation mode and next-error to jump to the warnings and errors in the source. However, I find it highly annoying that next-error brings me to every #include for the lines "In file included from" in the compilation output. I know you can use compilation-skip-threshold to skip warnings, but I don't want to skip warnings, and these include lines show up as warnings.
To me this seems to be a bug in compilation mode (these aren't warnings), but this bug was closed as "not a bug"
Specifically, for an output that looks like this:
In file included from /path/to/file1.h:linenum1:
In file included from /path/to/file2.h:linenum2:
In file included from /path/to/file3.h:linenum3:
/path/to/file4.h:linenum4:columnnum4: warning: you are bad at c++
I want next-error to take me right to file4.h, instead of stopping in files 1 through 3 on the way.
Thanks!
I tried it for myself. We seem to have different gcc versions, because my output looks like this:
g++ test.cc
In file included from file3.h:1:0,
from file2.h:1,
from file1.h:2,
from test.cc:2:
file4.h:1:2: warning: #warning "you are bad at c++" [-Wcpp]
But I still see the problem. Apparently, it's the 'gcc-include regexp that breaks things. In my situation, all those "from" lines match correctly but the last one. The problem is that it ends in a colon and this somehow makes it a warning. I'm a bit lazy now to check what possible gcc output message does such matching target (there should be a reason for it, huh?), so I'll just answer the question:
;; This element is what controls the matching behaviour: according to
;; `compilation-error-regexp-alist` doc, it means if subexpression 4 of the
;; regexp matches, it's a warning, if subexpression 5 matches, it's an info.
(nth 5 (assoc 'gcc-include compilation-error-regexp-alist-alist))
(4 . 5)
;; We could try and tinker with the regexp, but it's simpler to just set it as
;; "always match as info".
(setf (nth 5 (assoc 'gcc-include compilation-error-regexp-alist-alist)) 0)
This snippet stopped compilation mode from highlighting last "from" line as a warning for me.
Configure compilation-skip-threshold.
When entering code Emacs transiently highlights the matching brace or bracket. With existing code however is there a way to ask it to highlight a matching brace or bracket if I highlight its twin?
I am often trying to do a sanity check when dealing with compiler errors and warnings. I do enter both braces usually when coding before inserting the code in between, but have on occasion unintentionally commented out one brace when commenting out code while debugging.
Any advice with dealing with brace and bracket matching with Emacs?
OS is mostly Linux/Unix, but I do use it also on OS X and Windows.
If you're dealing with a language that supports it, give ParEdit a serious look. If you're not using with a Lisp dialect, it's not nearly as useful though.
For general brace/bracket/paren highlighting, look into highlight-parentheses mode (which color codes multiple levels of braces whenever point is inside them). You can also turn on show-paren-mode through customizations (that is M-x customize-variable show-paren-mode); that one strongly highlights the brace/bracket/paren matching one at point (if the one at point doesn't match anything, you get a different color).
my .emacs currently contains (among other things)
(require 'highlight-parentheses)
(define-globalized-minor-mode global-highlight-parentheses-mode highlight-parentheses-mode
(lambda nil (highlight-parentheses-mode t)))
(global-highlight-parentheses-mode t)
as well as that show-paren-mode customization, which serves me well (of course, I also use paredit when lisping, but these are still marginally useful).
Apart from the answer straight from the manual or wiki, also have a look at autopair.
tried on emacs 26
(show-paren-mode 1)
(setq show-paren-style 'mixed)
enable showing parentheses
set the showing in such as highlit the braces char., or if either one invisible higlight what they enclose
for toggling the cursor position / point between both, put this script in .emacs
(defun swcbrace ()(interactive)
(if (looking-at "(")(forward-list)
(backward-char)
(cond
((looking-at ")")(forward-char)(backward-list))
((looking-at ".)")(forward-char 2)(backward-list))
)))
(global-set-key (kbd "<C-next>") 'swcbrace)
it works toggling by press Control-Pgdn
BTW, for the immediate question: M-x blink-matching-open will "re-blink" for an existing close paren, as if you had just inserted it. Another way to see the matching paren is to use M-C-b and M-C-f (which jump over matched pairs of parens), which are also very useful navigation commands.
I second ParEdit. it is very good atleast for lisp development.
FWIW I use this function often to go to matching paren (back and forth).
;; goto-matching-paren
;; -------------------
;; If point is sitting on a parenthetic character, jump to its match.
;; This matches the standard parenthesis highlighting for determining which
;; one it is sitting on.
;;
(defun goto-matching-paren ()
"If point is sitting on a parenthetic character, jump to its match."
(interactive)
(cond ((looking-at "\\s\(") (forward-list 1))
((progn
(backward-char 1)
(looking-at "\\s\)")) (forward-char 1) (backward-list 1))))
(define-key global-map [(control ?c) ?p] 'goto-matching-paren) ; Bind to C-c p
Declaimer: I am NOT the author of this function, copied from internet.
If you just want to check the balanced delimiters, be them parentheses, square brackets or curly braces, you can use backward-sexp (bound to CtrlAltB) and forward-sexp (bound to CtrlAltF) to skip backward and forward to the corresponding delimiter. These commands are very handy to navigate through source files, skipping structures and function definitions, without any buffer modifications.
You can set the below in your init.el:
(setq show-paren-delay 0)
(show-paren-mode 1)
to ensure matching parenthesis are highlighted.
Note that (setq show-paren-delay 0) needs to be set before (show-paren-mode 1) so that there's no delay in highlighting, as per the wiki.
If you want to do a quick check to see whether brackets in the current file are balanced:
M-x check-parens
Both options tested on Emacs 27.1
So I have a tool lints python changes I've made and produces errors and warnings. I would like this to be usable in compile mode with Emacs, but I have an issue. The file name is output only once at the beginning, and then only line numbers appear with the errors and warnings. Here's an example:
Linting file.py
E0602: 37: Undefined variable 'foo'
C6003: 42: Unnecessary parens after 'print' keyword
2 new errors, 2 total errors in file.py.
It's very similar to pylint, but there's no output-format=parseable option. I checked the documentation for compilation-error-regexp-alist, and found something promising:
If FILE, LINE or COLUMN are nil or that index didn't match, that
information is not present on the matched line. In that case the
file name is assumed to be the same as the previous one in the
buffer, line number defaults to 1 and column defaults to
beginning of line's indentation.
So I tried writing a regexp that would optionally match the file line and pull it out in a group, and then the rest would match the other lines. I assumed that it would first match
Linting file.py
E0602: 37: Undefined variable 'foo'
and be fine. Then it would continue and match
C6003: 42: Unnecessary parens after 'print' keyword
with no file. Since there was no file, it should use the file name from the previous match right? Here's the regexp I'm using:
(add-to-list 'compilation-error-regexp-alist 'special-lint)
(add-to-list 'compilation-error-regexp-alist-alist
'(special-lint
"\\(Linting \\(.*\\)\\n\\)?\\([[:upper:]][[:digit:]]+:\\s-+\\([[:digit:]]\\)+\\).*"
2 4 nil nil 3))
I've checked it with re-builder and manually in the scratch buffer. It behaves as expected. the 2nd group is the file name, the 4th is the line number, and the 3rd is what I want highlighted. Whenever I try this, I get the error:
signal(error ("No match 2 in highlight (2 compilation-error-face)"))
I have a workaround for this that involves transforming the output before the compile module looks at it, but I'd prefer to get rid of that and have a "pure" solution. I would appreciate any advice or pointing out any dumb mistakes I may have made.
EDIT
Thomas' pseudo code below worked quite well. He mentioned that doing a backwards re search could mess up the match data, and it did. But that was solved by adding the save-match-data special form before save-excursion.
FILE can also have the form (FILE
FORMAT...), where the FORMATs (e.g.
"%s.c") will be applied in turn to the
recognized file name, until a file of
that name is found. Or FILE can also
be a function that returns (FILENAME)
or (RELATIVE-FILENAME . DIRNAME). In
the former case, FILENAME may be
relative or absolute.
You could try to write a regex that doesn't match the file name at all, only the column. Then for the file, write a function that searches backwards for the file. Perhaps not as efficient, but it should have the advantage that you can move upwards through the error messages and it will still identify the correct file when you cross file boundaries.
I don't have the necessary stuff installed to try this out, but take the following pseudo-code as an inspiration:
(add-to-list 'compilation-error-regexp-alist-alist
'(special-lint
"^\\S-+\\s-+\\([0-9]+\\):.*" ;; is .* necessary?
'special-lint-backward-search-filename 1))
(defun special-lint-backward-search-filename ()
(save-excursion
(when (re-search-backward "^Linting \\(.*\\)$" (point-min) t)
(list (match-string 1)))))
(It could be that using a search function inside special-lint-backward-search-filename will screw up the sub-group matching of the compilation-error-regexp, which would suck.)
I don't think you can make compilation do what you want here, because it won't assume that a subsequent error relates to a previously-seen filename. But here's an alternative; write a flymake plugin. Flymake always operates on the current file, so you only need to tell it how to find line (and, optionally, column) numbers.
Try hacking something like this, and you'll likely be pleasantly surprised.