I want to use beginneing-of-visual-line and end-of-visual-line.
So I wrote in .emacs like following.
(global-set-key "\C-a" 'beginning-of-visual-line)
(global-set-key "\C-e" 'end-of-visual-line)
beginning-of-visual-line works as I hoped.
But if I use C-e in visually multiple lines, cursor go to the beginning of next line.
Why is this happens? And how can I fix it?
I'm using Emacs 24.3 (9.0) in Mac OS X 10.7.5.
I think in general, end-of-line will position the point on the next character after the end of the line.
I find this helpful when coding in lisp, because it places the point in the perfect place to evaluate the previous s-exp easily.
However, in the case of end-of-visual line, the first character after the end of the line is actually on the next visual line. If you want the point to be on the last character of the current visual line, you could just go back one character, like this:
(global-set-key (kbd "C-e") '(lambda ()
(interactive)
(end-of-visual-line)
(backward-char)))
Edit
A much better way of achieving the same thing - which will fix the point in your comment, is just to enable visual-line-mode:
(global-visual-line-mode 1)
This will automatically make your C-a and C-e work with visual lines.
The only problem with visual-line mode is that by default, there are no indicators showing where the line is wrapped. To fix this, you can use:
(setq visual-line-fringe-indicators '(left-curly-arrow right-curly-arrow))
You'll need to put the visual-line-fringe-indicators assignment before the global-visual-line-mode call in your .emacs for it to work.
Source for the above: http://www.emacswiki.org/emacs/VisualLineMode
Related
In Terminal Emacs (no mouse), I'm using split windows to work with multiple buffers at the same time. I'm finding moving between the split windows much more painful than how I do it in Vim. Reading the documentation it looks like I'm doing it correctly (C-x o), but that just cycles around the windows in a clockwise direction. If I move to an adjacent window to make a quick edit, I need to hit C-x o a few times before I'm back where I was. Sometimes I accidentally press it too many times in a hurry and have to cycle all the way back through the windows again.
Far from install yet-another-external-package, is there any way I can just either move directly to a window (by, say, a number), or at least cycle around the windows in the opposite direction?
In Vim C-w C-w is like C-x o in Emacs, but there's also C-w ARROW to move in a specified direction... something like that? :)
Add this to your init file:
(windmove-default-keybindings)
Then you can use SHIFT+arrow to move to the next adjacent window in the specified direction.
You can specify a different modifier if you prefer, by passing an argument (defaults to 'shift).
Or just bind whatever you like to these functions:
windmove-up
windmove-down
windmove-left
windmove-right
You can also add FrameMove to the mix, to make this work transparently across multiple frames.
For numbered window navigation, there's switch-window.el.
Add this to your init file (e.g. ~/.emacs):
(windmove-default-keybindings)
Then do SHIFT+arrow to move to the window in that direction.
You can give a prefix argument to C-x o like this C-u -1 C-x o. This way you can go any number of windows forward or backward. Personally I think it's easier to create a special key for moving back one window. I have this in my .emacs:
(defun other-window-backward ()
"Goto previous window"
(interactive)
(other-window -1))
(global-set-key (kbd "\C-x p") 'other-window-backward)
I use the following to navigate to the next (same as C-x o), previous, first, and last window:
(defun my-previous-window ()
"Previous window"
(interactive)
(other-window -1))
(global-set-key "\C-xp" 'my-previous-window)
(global-set-key "\C-xn" 'other-window)
(defun my-select-first-window ()
(interactive)
(select-window (frame-first-window)))
(defun my-select-last-window ()
(interactive)
(select-window (previous-window (frame-first-window))))
(global-set-key "\C-x<" 'my-select-first-window)
(global-set-key "\C-x>" 'my-select-last-window)
Use window-jump, e.g.:
;; C-x <direction> to switch windows
(use-package window-jump
:bind (("C-x <up>" . window-jump-up)
("C-x <down>" . window-jump-down)
("C-x <left>" . window-jump-left)
("C-x <right>" . window-jump-right)))
For help with use-package, see https://github.com/jwiegley/use-package/blob/master/README.md.
For the sake of completion, there is window-numbering and ace-window too
I wrote an elisp module a while back to expand on windmove to make it a bit more useful: http://samograd.ca/stumpwm.el. You can bind stumpwm-move-window-[left/right/up/down] to whatever keys you want and the windows will move in the correct direction, even into another another frame (tested with 2 frames). There's also an stumpwm-interactive-resize-window for handy interactive window resizing using C-n/C-p/C-f/C-b with Return to end resizing.
I've noticed that C-e <END> or M-x move-end-of-line doesn't always move the cursor to the end of the line.
Specifically this happens lines wider than the current window, it appears to move to some arbitrary point midway along the line.
Does anyone know if this is expected and more importantly, how to switch it off and make move-end-of-line, really move to the END of the line?
(Note: this is also happening in regular non-macro use.)
Emacs version in this example is GNU Emacs 23.1.97.1 (i386-mingw-nt6.1.7601)
Update.
The cursor is moving to the char that is on the edge of the window, (the display then re-centers around the cursor.)
Make sure visual-line-mode is off for the buffer.
Agree with #Slomojo here (it seems I cannot comment yet).
To add, here is the quote from the Emacs manual:
In Visual Line mode, some editing commands work on screen lines instead of logical lines: C-a (beginning-of-visual-line) moves to the beginning of the screen line, C-e (end-of-visual-line) moves to the end of the screen line, and C-k (kill-visual-line) kills text to the end of the screen line.
C-e is mapped to end-of-visual-line, the best solution isn't to deactivate visual-line-mode (don't do that especially if you're coding) but to remap C-e to end-of-line in your init file like this:
(global-set-key (kbd "C-e") 'end-of-line)
Of course I advise you to do the same for C-a and to remap it to beginning-of-line.
If you think you need to use end-of-visual-line and beginning-of-visual-line they're still mapped to end and home buttons respectively.
I'm using Emacs 23.1.1 on Ubuntu with Emacs starter kit. I primarily work in the lua-mode.
Is there a way to stop Emacs being so smart about indentation? I'm used to the dumb editors, and press all the required keys manually.
I want to use two spaces per indent, tabs-to-spaces.
When I press RETURN, the new line indentation must match the previous line.
When I press TAB on the leading whitespace, the line contents must be indented by one indentation unit.
When I press TAB on the beginning of empty line, the cursor must move one indentation unit to the right.
Oh, and I'd like to get soft word wrap on 80th column and trim-trailing-spaces on save as well.
Update:
(Would put this in a comment, but it needs formatting)
If I use Thomas's solution, auto-indent on RETURN is "fixed", but TAB still indents weirdly:
local run = function(...)
x
"x" marks the spot where cursor appears after I type the first line and hit RETURN, TAB.
Emacs has a concept of modes, which means that depending on what type of file you're editing it provides special functionality that is useful for that file. Every buffer has one major mode associated and optionally a number of minor modes.
Indentation is one of the things that is typically mode-dependent. That is, you may have to configure indentation separately for every major-mode, because otherwise when you load a new file, its associated major mode may override your indentation settings. It's possible though to write a function that configures indentation and set up Emacs in a way that the function is invoked whenever a new major-mode is started.
In order to realize the settings you want, you'll need to run a few lines of elisp code. (Unfortunately your description of what should happen when you hit TAB leaves out some details, I've implemented the simplest version I could think of below -- if it's not what you want, that can be changed, of course.)
Put the following code in the file named .emacs in your home directory (~):
(setq-default indent-tabs-mode nil) ; use spaces for indentation
(defvar my-indentation-width 2
"The number of spaces I prefer for line indentation.")
(defun my-enter ()
"Inserts a newline character then indents the new line just
like the previous line"
(interactive)
(newline)
(indent-relative-maybe))
(defun my-indent ()
"When point is on leading white-space of a non-empty line, the
line is indented `my-indentation-width' spaces. If point is at
the beginning of an empty line, inserts `my-indentation-width'
spaces."
(interactive)
(insert (make-string my-indentation-width ? )))
(defun my-indentation-setup ()
"Binds RETURN to the function `my-enter' and TAB to call
`my-indent'"
(local-set-key "\r" 'my-enter)
(setq indent-line-function 'my-indent))
(defun delete-trailing-whitespace-and-blank-lines ()
"Deletes all whitespace at the end of a buffer (or, rather, a
buffer's accessible portion, see `Narrowing'), including blank
lines."
(interactive)
(let ((point (point)))
(delete-trailing-whitespace)
(goto-char (point-max))
(delete-blank-lines)
(goto-char (min point (point-max)))))
;; make sure trailing whitespace is removed every time a buffer is saved.
(add-hook 'before-save-hook 'delete-trailing-whitespace-and-blank-lines)
;; globally install my indentation setup
(global-set-key "\r" 'my-enter)
(setq indent-line-function 'my-indent)
;; also override key setting of major-modes, if any
(add-hook 'after-change-major-mode-hook 'my-indentation-setup)
This works for me in Emacs 23, although I may have missed some edge cases. However, these changes are so fundamental that I predict you will run into incompatibilities sooner or later with some major-modes that expect indentation to work they set it up. If you really want to get into Emacs it's worthwhile adapting the habits you inherited from other editors to the way Emacs does things.
For soft word-wrap there is a minor-mode called "longlines" which you can download from here: http://www.emacswiki.org/cgi-bin/emacs/download/longlines.el I haven't used it so I can't tell you how well it works.
Fixing TAB and RETURN:
(global-set-key "\t" 'self-insert-command)
(global-set-key "\r" 'newline-and-indent)
Fill column (haven't tried): say ESC x customize-var, enter fill-column, set to 80.
This is probably basic but I really tried to find the answer. "C-k" deletes from the cursor to the end of the line, but is there an analogous shortcut to delete a line backwards from the cursor point?
Best
Try C-u 0 C-k - i.e. C-k with the prefix 0 kills from point to the start of the line. See the documentation for C-k (kill-line) for more information.
As other answers suggest, C-0C-k kills from point to the start of the line. See the documentation for C-k (kill-line) for more information. You may also want to kill the whole line before and after point with C-S-backspace which evals kill-whole-line.
Another convenience for killing is to rebind kill-region which defaults to C-w and instead bind to that key the function backward-kill-word which will then mimic the behavior of readline's C-w (unix-word-rubout). I rebind kill-region to C-q after moving quoted-insert to A-q. Yes, this requires moving a number of keys around, but if you give it a try I think you'll find it is convenient.
Here's asjo's answer bound to a key:
(global-set-key "\M-k" '(lambda () (interactive) (kill-line 0)) ) ;M-k kills to the left
I tried C-u with a negative number and C-k as arguments. It worked.
Example to delete 4 lines before cursor, try C-u -4 C-k
That's not exactly to the beginning of a line, but you might want to check out the hungry-delete package. Install, then add the following to the init file:
(add-hook 'after-init-hook (lambda ()
(require 'hungry-delete)
(global-hungry-delete-mode)))
After that DEL would delete up to the next non-whitespace character, and so would Backspace, but in the opposite direction.
M-0C-k (because C-0C-k doesn't work on my computer).
To delete all blank spaces in the beginning of a line:
M-mM-0C-k.
How do I configure Emacs so that line wrapping does not break in the middle of a word?
If you want to emulate the behavior of an editor like Notepad, you might want to turn on visual line mode. While setting word-wrap will cause line wrapping at word boundaries, any action you take on a line (e.g., moving up/down or killing) will still respect the newline character. Visual line mode will treat each display line as though it had a newline at the end.
(visual-line-mode t)
Line to add in .emacs file:
(global-visual-line-mode t)
M-x toggle-truncate-lines disable allows you to disable visually line breaking.
M-x auto-fill-mode + M-q allows you to word wrap for real a pre-existing paragraph.
Add this to your init file:
(setq-default word-wrap t)
Alternatively, press C-h vword-wrap in Emacs and follow the "customize" link near the end.
I discovered longlines-mode only recently (I think I was spelunking through the Emacs Info documentation). It wraps as you would expect in other UI editors' word-wrap feature. It's especially useful when I'm reading or writing free text with no newlines (a la Microsoft Word) without the ugly mid-word wrapping that happens when you use M-x toggle-word-wrap.
See LongLines.
My configuration:
(setq longlines-wrap-follows-window-size t)
(global-set-key [(control meta l)] 'longlines-mode)