📜 ⬆️ ⬇️

Emacs Utility and tricks

This is my first post on Habré and blogs in general, so do not judge strictly. emacs is a miracle constructor, you can make almost anything from it (or maybe). and the more you work with him, the more you feel his boundlessness and power. here you’ll have a mail client, a jabber, and a shell, a universal development environment, a file manager, games ... this list goes on and on.

The first launch of emacs' will not show you anything special, but as soon as you have a problem or inconvenience, you will necessarily find a solution (if of course you want). In this post I will show you some things that have helped me and, I hope, will help you. I warn you in advance that some of the functions that I will list below are not written by me and express my gratitude to their authors for what they have done.

tested and working on emacs cvs.

VIEW.
')
1) I'll start with the elementary. interface. I do not use buttons, menus, scroll bar, because I consider them ineffective and useless. remove them.

;; scroll bar
(scroll-bar-mode -1)

;; tool bar
(tool-bar-mode -1)

;; menu bar
(menu-bar-mode -1)


2) I do not like the colors that go by default. decided to replace them with a darker and more pleasant looking (I have LCD).

(set-background-color "#333333")
(set-foreground-color "#ffffff")

if they do not suit you, you can view the list of all colors of 'Mx list-color-display' and change to your favorite. cursor color - (set-cursor-color <color>)

3) fonts. here you can argue endlessly. as many know in emacs, xft support has emerged from relatively recently. Someone may like it, but for programming I prefer bitmap fonts. the only negative is utf8, not everyone is holding it. for this reason, I chose terminus. and 0 from about in it you distinguish, and this is an important point. I change fonts through .Xdefaults. in windows this is done through the registry, google will help.

Emacs.font: -*-terminus-medium-*-*-*-16-*-*-*-*-*-*-*


4) utf8. to display utf8 correctly in the shell and dired.

(setq file-name-coding-system 'utf-8)


HOT KEYS AND USEFUL THINGS

5) change capslock to ctrl. very important thing. This, I think, is the most important thing for effectively using emacs. There are a lot of controversies about this, too, but it seems to me quite comfortable. add in .xmodmaprc (if you don’t have it, create) the following:

keycode 66 = Control_L
clear Lock
add Control = Control_L


add the line to .xinitrc or .xsession, depending on how you load the x.

xmodmap .xmodmaprc


6) convenient scrolling and selection of the current line. by default, emacs scrolls almost half the screen, which is not very convenient. fix it. and it also happens that you lose the place of the cursor, for this we turn on the highlighting of the current line.

(setq scroll-step 1)
(global-hl-line-mode 1)


7) we know, 'Cx o' switches the focus to the next window. and if windows 5 or 10? make navigation easier.

(windmove-default-keybindings 'meta)


Now the navigation will take place as follows: alt + \ arrow keys \.

8) resize the window, by default, you can mouse ... for this you need to remove your hand from the keyboard. is it convenient? I think so too. add the following to .emacs.

(defun win-resize-top-or-bot ()
"Figure out if the current window is on top, bottom or in the
middle"
(let* ((win-edges (window-edges))
(this-window-y-min (nth 1 win-edges))
(this-window-y-max (nth 3 win-edges))
(fr-height (frame-height)))
(cond
((eq 0 this-window-y-min) "top")
((eq (- fr-height 1) this-window-y-max) "bot")
(t "mid"))))

(defun win-resize-left-or-right ()
"Figure out if the current window is to the left, right or in the
middle"
(let* ((win-edges (window-edges))
(this-window-x-min (nth 0 win-edges))
(this-window-x-max (nth 2 win-edges))
(fr-width (frame-width)))
(cond
((eq 0 this-window-x-min) "left")
((eq (+ fr-width 4) this-window-x-max) "right")
(t "mid"))))

(defun win-resize-enlarge-horiz ()
(interactive)
(cond
((equal "top" (win-resize-top-or-bot)) (enlarge-window -1))
((equal "bot" (win-resize-top-or-bot)) (enlarge-window 1))
((equal "mid" (win-resize-top-or-bot)) (enlarge-window -1))
(t (message "nil"))))

(defun win-resize-minimize-horiz ()
(interactive)
(cond
((equal "top" (win-resize-top-or-bot)) (enlarge-window 1))
((equal "bot" (win-resize-top-or-bot)) (enlarge-window -1))
((equal "mid" (win-resize-top-or-bot)) (enlarge-window 1))
(t (message "nil"))))

(defun win-resize-enlarge-vert ()
(interactive)
(cond
((equal "left" (win-resize-left-or-right)) (enlarge-window-horizontally -1))
((equal "right" (win-resize-left-or-right)) (enlarge-window-horizontally 1))
((equal "mid" (win-resize-left-or-right)) (enlarge-window-horizontally -1))))

(defun win-resize-minimize-vert ()
(interactive)
(cond
((equal "left" (win-resize-left-or-right)) (enlarge-window-horizontally 1))
((equal "right" (win-resize-left-or-right)) (enlarge-window-horizontally -1))
((equal "mid" (win-resize-left-or-right)) (enlarge-window-horizontally 1))))

(global-set-key [CM-down] 'win-resize-mi2nimize-vert)
(global-set-key [CM-up] 'win-resize-enlarge-vert)
(global-set-key [CM-left] 'win-resize-minimize-horiz)
(global-set-key [CM-right] 'win-resize-enlarge-horiz)
(global-set-key [CM-up] 'win-resize-enlarge-horiz)
(global-set-key [CM-down] 'win-resize-minimize-horiz)
(global-set-key [CM-left] 'win-resize-enlarge-vert)
(global-set-key [CM-right] 'win-resize-minimize-vert)


As you can see from the keymap 'om,' ctrl + alt + \ arrow keys \ 'window resize.

9) I think that it is not only annoying to me that you should always type yes or no when you close the buffer or leave emacs. not easier to type y or n?

(fset 'yes-or-no-p 'y-or-np)


it will save from one to two extra clicks.

10) when working with a large number of buffers, I felt uncomfortable when switching between them, for a long time I could not find what I needed. It turns out that emacs had a mode for it.

(iswitchb-mode 1)


By the way, this is the old way to switch between buffers. I do not understand why he was removed. now, by pressing 'Cx b', iswitchb-buffer will be called. it automatically discards the unnecessary, and when the only option left is to press the tab and you will switch to the required buffer. the only drawback I noticed is that if there are buffers with the names: in and main, then by typing in, it chooses main, and it is impossible to turn to in. Maybe there is a key or command on it, but I did not find it.

and I also decided to add more convenient keys to switch to the next / previous buffer.

(global-set-key [?\C-,] 'previous-buffer)
(global-set-key [?\C-.] 'next-buffer)


11) there are times when you forget to hold down ctrl for navigation through 'Cn', 'Cp', etc. and get unnecessary sequences of the type: nnnnnnnnnnnn ppppppppppp ffffffff. do not delete all the backspace 'om ... you can make a hotkey for bacward-kill-word.

(global-set-key "\Cw" 'backward-kill-word)
(global-set-key "\Cx\Ck" 'kill-region)
(global-set-key "\Cc\Ck" 'kill-region)


Now, to “kill” the selected text will have to press the 'Cx C-k', because we assigned bacward-kill-region to 'cw'.

12) Macros are a powerful thing both in programming and in emacs. they are worthy to give them comfortable cubebing.

(global-set-key [f2] 'kmacro-call-macro)
(global-set-key [f3] 'kmacro-start-macro-or-insert-counter)
(global-set-key [f4] 'kmacro-end-or-call-macro)


f2 - call macro
f3 - start macro
f4 - end macro

13) it often happens when you need to switch to the beginning of the buffer, and then back to the place where you were previously. There are bookmarks for this. for convenience, I added hotkeys to call them.

(global-set-key [f5] 'bookmark-set)
(global-set-key [f6] 'bookmark-jump)


f5, enter the name of the bookmark, do what you need, f6, enter the name of the bookmark, and you are again in the same place.

14) use workspaces. it gives a lot of new features. for example, you can read mail with gnus at the first workspace, debug the program at the second, edit the code at the third, and at the fifth shell. damn convenient, right?

here is the link to workspaces.el.

copy it to your boot directory. if you do not have it. then create an elisp directory (or whatever you like) in your home directory (or whatever you like) and add to .emacs:

(add-to-list 'load-path "~/elisp") ;; ,
(require 'workspaces)
(global-set-key [?\Cq] 'workspace-goto)


I decided to choose 'C-q'. 'Cq' \ 0-9 \ - switches to the desired workspace. As you have probably understood their maximum number is 9.

15) if you are working with a large number of files / buffers and decide to close emacs, and then decide to resume work. do not open them again ... ask emacs to save the session before exiting.

(desktop-save-mode t)


END

that's all. if you are interested in my .emacs, contact me, I have a few more records about programming and debugging. I will be glad to hear your criticism. Sincerely, wzrd.

Source: https://habr.com/ru/post/39564/


All Articles