📜 ⬆️ ⬇️

Emacs for beginners: windows management, dedicated-mode

This article is for novice Emacs users, talks about managing windows in this great editor.

Many users working under Linux, as well as under other Unix-s, and some even under Windows, use Emacs as an editor.

The editor is not just powerful, but super-powerful, but as every powerful thing, it requires some effort to master. I will tell about management of windows.

A few words about terminology. In Emacs, it is somewhat unusual. What we usually call a window is called a frame in Emacs. Window (window) is called some part of the frame. Usually the window takes up the entire frame, but you can divide the frame into several windows, which is very, very convenient.
')
For example, the Cx 2 command divides the current window horizontally into two other windows, the Cx 3 command does the same, but vertically, Cx 0 destroys the current window. To navigate through the windows there are several ways. The first is to simply click the desired window with the mouse. Second, use the "go to the next window" key combination, Cx o, command / function (other-window). Well, the third way is to insert a line into .emacs:

(windmove-default-keybindings)

Now you can move through the windows using the key combination Shift- <arrow> or emacs, <S-up>, <S-down>, <S-left>, <S-right>.

The same can be done from scripts.

Thus, for example, by defining a function in our .emacs file:

 (defun my-make-three-windows () 
   "Make three windows"
   (interactive)
   (split-window-horizontally)
   (split-window-vertically)	
   )

and calling it to run, we get the following window configuration:
 | ----------- + ----------- |
 |  |  |
 |  |  |
 | ----------- + |
 |  |  |
 |  |  |
 | ----------- + ----------- |

Personally, in my organizer, on the basis of org-mode I use the following combination:

 + ---------- + --------------- + --------------- +
 |  |  |  |
 |  |  |  |
 |  |  |  |
 |  + --------------- + --------------- +
 |  |  |
 |  |  |
 |  |  |
 + ---------- + ------------------------------- +

Interesting, but what is the use of all this? Well, for example, we can open the file of interest in each of the windows. Modifying the function:

 (defun my-make-three-windows () 
   "Make three windows"
   (interactive)
   (find-file "~ / file_1")
   (split-window-horizontally)
   (find-file "~ / file_2")
   (split-window-vertically)	
   (find-file "~ / file_3"))

So, well, but if we already have space divided into windows, our function divides the current window into three parts, and we want our windows to occupy the entire frame. No questions. Add the first line to the destruction of other windows:

 (defun my-make-three-windows () 
   "Make three windows"
   (interactive)
   (delete-other-windows)
   (find-file "~ / file_1")
   (split-window-horizontally)
   (find-file "~ / file_2")
   (split-window-vertically)	
   (find-file "~ / file_3"))

Now we want the right buffer to always have the same buffer, with the file open, and Emacs wants it, and can easily put an unwanted buffer in our window. Let's apply dedicated-mode. In Ubuntu, it is in the emacs-goodies package. So, install emacs-goodies. and add the following command to our function:

 (defun my-make-three-windows () 
   "Make three windows"
   (interactive)
   (delete-other-windows)
   (find-file "~ / file_1")
   (split-window-horizontally)
   (find-file "~ / file_2")
   (split-window-vertically)	
   (find-file "~ / file_3")
   (other-window 1)
   (other-window 1)
   (dedicated-mode))

Pay attention to the line (other-window 1), we overtook the cursor in the window we need, and only then gave the command (dedicated-mode). Now our right window will always be associated with the file buffer “file_1”.

The same command can be given online, it is tied to the keys ESC Md.

Keyboard / function label:
 | --------- + --------------------------- + ----------- ---------------------------- |
 |  Keys |  Function |  Action |
 | --------- + --------------------------- + ----------- ---------------------------- |
 |  Cx 1 |  delete-other-windows |  Delete other windows, leave current |
 |  C-x 2 |  split-window-vertically |  Vertical divides |
 |  Cx 3 |  split-window-horizontally |  Horizontal Divides |
 |  Cx 0 |  delete-window |  Destroys the current window |
 |  ESC Md |  dedicated-mode |  On / Off Assigned Mode |
 |  Cx o |  other-window |  Go to the next window |
 | --------- + --------------------------- + ----------- ---------------------------- |

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


All Articles