Linux C programming-emacs uses two

Source: Internet
Author: User
Tags autoload

Emacs Save As another file after opening the file:

C-x C-w then enter the file path and filename of the Save As

The shell in Emacs uses: (Quote original address: http://www.ibm.com/developerworks/cn/linux/l-cn-emacs-shell/)

First intro

GNU Emacs is a very powerful editor that can be used not only to write articles, write programs, but more importantly, to work with some seemingly unrelated applications to create new "magical" applications. For example, you can run your Shell inside GNU Emacs.

In general, when people work on Linux or Unix, whether they work on the computer or the remote machine that logs on to the other side of the globe, they run the Shell using a variety of terminals or terminal simulators. The most common examples are XTERM,RXVT, and terminal emulators such as Putty. In response, GNU Emacs also has its own terminal emulator, such as Ansi-term,multi-term and so on. These terminal modes allow you to work as you would in other terminals, and even run Vim inside the Emacs terminal.

But what we're going to share with you today is another way to use--shell mode. This is a completely different way of working. One of the biggest differences between this and the usual way of working is that there is no terminal at all. The user is actually working in an Emacs text buffer and does not interact directly with the Shell. All command input is written to the text buffer, which is comint.el read from the buffer and then forwarded to the background Shell process. The output generated by the Shell is then comint.el collected and then written to the buffer used by the user. This buffer is called the shell buffer in Emacs.

The process of starting a shell buffer and entering the shell mode is straightforward. Just press the combo key in Emacs Meta-x (usually the Alt-x key on the current keyboard), then enter the command shell and return, Emacs launches a shell process and opens a shell buffer associated with it. The name of the Shell buffer will usually be *shell* . The specific startup Shell process is specified by a variable in the Emacs configuration file shell-file-name , or by the user's environment variable SHELL EMACSSHEL . The usual notation is

(setq shell-file-name "/bin/bash")

Or

Export Emacsshell=/usr/bin/zsh

In addition, if you want to use a Shell process that supports ANSI color, it is best to add the following two lines to your Emacs configuration file so that the ls – color=auto color information that is output when the command is executed can be parsed correctly by Emacs.

(AutoLoad ' ansi-color-for-comint-mode-on "Ansi-color" nil T)  (Add-hook ' Shell-mode-hook ' ansi-color-for-comint-mode-on t)

With so much to say, what good would this way of working be? Why should I leave the familiar Xterm and move my Shell into Emacs?

Back to top of page

Second back to the Shell mode--window

Now let's talk about the benefits. In fact, it is not only a benefit, but a considerable degree of even irreplaceable.

The first obvious benefit is a multi-window working mode.

Often, in people's jobs, multiple terminals are opened and several jobs are performed. These terminal windows need to be arranged and managed at this time (assuming you are working in a graphical environment). It is often necessary to use the mouse frequently to switch the focus between different windows. To avoid hiding windows from each other, you might want to .Xdefaults make two or four terminal windows neatly tiled across the screen through carefully edited files. But still need to use the mouse to switch between different windows, copy and paste information between different windows ... The work that these windows maintain is very heavy when the task is busy. And if you need more than 4 windows at this time, or you need to do additional text editing work ... The final window will either be overwritten or squeezed into another virtual desktop.

It's best to try GNU Emacs at this time. GNU Emacs is inherently well-equipped for window management and does not depend on X window at all. This is because GNU Emacs was born much earlier than the history of X Window. In GNU Emacs you only have to press the Ctrl-x 2 key combination to cut the current window into two equal

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

Press the Ctrl-x 3 key combination and the current window can be cut into the left and right two equal to the window. These shards can go on all the time.

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

Enter Ctrl-x 0 to close the window where the current cursor is located.

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

Enter a Ctrl-x 1 key combination to close all other windows and make the window with the current cursor the largest.

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

When you use two or more windows, you can use the Ctrl-x o (note is the lowercase letter O) key combinations in each window to move. Ctrl-x o Ctrl-u 3 Ctr-x o Meta-3 Ctrl-x o You can quickly move between multiple windows by adding a numeric prefix to the key combination, for example, or more succinctly.

Of course, when you start too many buffers, you're going to have to cover some of them out. Because it is really meaningful to make sure that the work window has enough visible area. At such times, it is Ctrl-x b convenient to switch between all buffers by combining keys. or Ctrl-x Ctrl- b get a list of all buffers by combining keys.

The biggest difference between switching between this buffer and the X window or virtual desktop is that if you have any two or more buffers you need to cross-reference (which is very common), even if you copy and paste the information, At this time the relevant work window should preferably be distributed on the same screen. In GNU Emacs you will easily switch the buffer areas that need to be referenced to the same screen window. In the way the graphics terminal works, the windows that need to be referenced often either happen to be covered by each other or happen to be in different virtual desktops, and frequent drag-and-drop movements can become cumbersome.

There is also a situation where the reason for the work happens to be a contextual reference to the content of the same Shell process ... Typically, most terminals do not provide this functionality. But in Emacs, the same buffer is displayed in two separate windows, which is not a problem at all.

Plus, if you like how multiple virtual desktops work, you can usemake-frame命令生成多个 frame( 也许可以叫做“窗框”),把他们放到多个虚拟桌面当中去。而且即使是在这种情况下,仍然可以使用Ctrl-x b 组合键在任何一个frame 中的任何一个窗口中切换到任何一个被遮盖的缓冲区。不需要进行任何 X Window 当中的窗口移动和桌面切换,包括进行上下文参照。

技巧一

如何在 GNU Emacs 当中启动多个 Shell 进程及其对应的 Shell 缓冲区?

我在上文当中提到了那么多的窗口,但是如果你在 minibuffer 当中第二次输入 Meta-x shell 命令,GNU Emacs 会把你带到已经存在的那个名叫*shell* 的 Shell 缓冲区,而不是创建一个新的。解决的方法非常简单——你只需要使用rename-buffer 命令为现有的 Shell 缓冲区重新安排一个名字,然后再执行 shell 命令,GNU Emacs 就会为你创建一个新的名叫*shell* 的 Shell 缓冲区了。因为这两个命令在我的工作中用的非常频繁,所以我把它们绑定到了两个快捷键上面

 (global-set-key (kbd "C-c z") ‘shell)  (global-set-key (kbd "<f10>") ‘rename-buffer)
技巧二

如何 undo 到我刚刚离开的窗口设置?

上文提到过,Ctrl-x 0, Ctrl-x 1, Ctrl-x 2, Ctrl-x 3 能够快速的更改 GNU Emacs 的窗口设置,但是如果我在用过 Ctrl-x 1 之后希望能够快速“退回”到“刚才”使用过的窗口设置,而不是把它再做一遍,有没有办法做呢?GNU Emacs 有一个叫做 winner-mode 的 minor mode 可以帮你完成这个愿望。

只需要在你的 Emacs 配置文件里面加入下面几行

 (when (fboundp ‘winner-mode)   (winner-mode)   (windmove-default-keybindings)) 然后就可以使用 Ctrl-c ← (对,就是向左的箭头键)组合键,退回你的上一个窗口设置。

回页首

第三回 甜蜜约会 -- buffer 篇

上文描述了在 GNU Emacs 里面通过简单的窗口管理优化 Shell 工作的方法,是不是开始对 Shell 从终端里面搬到 Emacs 里面开始有了一点点的心动了呢? 别着急,这还只是个开始,目前你看到的都还只是外表。接下来让我们和 Emacs 来一个甜蜜的约会吧。

输入

我在开头的引子部分曾说过,在 Shell mode 中工作的时候,用户实际上接触的是一个文本缓冲区,实际上并没有直接的跟 Shell 进程打任何交道。这也是和通常的终端模式的工作方法的一个非常大的区别。虽然这个区别看起来似乎不是那么显著(那是因为这个 Shell 缓冲区被设计成了看起来很像一个图形终端的样子),但是实际上这点区别将会带来一些不可替代的优势。让我们来先看一个简单的例子:

让我们在 Shell 提示符前输入这样一行命令

 2 : 2037 : 13:04:40 : ~  [email protected]$  cd /usr/share/emacs  2 : 2038 : 13:05:05 : /usr/share/emacs  [email protected]$ ls -1  23.1  site-lisp  site-lisp.tar   2 : 2039 : 13:05:09 : /usr/share/emacs  [email protected]$

这个时候让我们把光标移动到 23.1 的前面,输入 ls -1加空格,

 2 : 2037 : 13:04:40 : ~  [email protected]$  cd /usr/share/emacs  2 : 2038 : 13:05:05 : /usr/share/emacs  [email protected]$ ls -1  ls -1 23.1  site-lisp  site-lisp.tar   2 : 2039 : 13:05:09 : /usr/share/emacs  [email protected]$

然后回车。接下来就会看到这样的输出结果出现在缓冲区里面。

 2 : 2040 : 13:08:55 : /usr/share/emacs  [email protected]$ ls -1 23.1  etc  leim  lisp  site-lisp   2 : 2041 : 13:09:06 : /usr/share/emacs  [email protected]$

这是一件很有意思的事情,因为我们并没有像在终端当中常见的那样在 Shell 提示符的后面进行命令输入,而是在一个看起来非常随意的地方。神奇的是他居然被正确地执行了。事情的真相其实很简单。

因为现在我们是在一个被称作 Shell 缓冲区的文本缓冲区里面。这就是一个很普通的文本缓冲区,它具有所有其他文本缓冲区所具有的一切特性。你可以在任何时候,任何位置,对这个缓冲区里的任何文本内容进行任何编辑。因为他就是文本。直到某一刻,你在其中一个文本行上面按下了回车,这时comint.el 就会负责把当前光标所在行的内容提取出来,发送给 Shell 去执行,然后将 Shell 执行的结果以及 一个提示符(这个提示符实际上也是由 Shell 输出给comint.el 的)以文本的形式添加到这个缓冲区的末尾。

这个例子并不仅仅是列一个目录那么简单,事实上他提供了一个更加强大的工作方式 —— 曾经只能用来阅读的命令输出现在也可以被用来构造新的命令了。让我们再来看一个新的例子,在这个例子中我们将把这种能力与 Bash 的历史命令引用的能力结合起来

 2 : 2044 : 15:16:17 : /usr/share/emacs/23.1  [email protected]$ ls -1  etc  leim  lisp  cd ../site-lisp && !!  2 : 2045 : 15:16:49 : /usr/share/emacs/23.1  [email protected]$ cd ../site-lisp && !!  cd ../site-lisp && ls -1  auctex  auctex.el  autoconf  autoconf-mode.el  autotest-mode.el  bashdb.el  bashdb.elc  bbdb

如果这时候我需要列出 auctex.el 文件的内容,我只需要在在各文件名前面输入 head,然后回车就行了

 2 : 2045 : 15:16:49 : /usr/share/emacs/23.1  [email protected]$ cd ../site-lisp && !!  cd ../site-lisp && ls -1  auctex  head auctex.el  autoconf  autoconf-mode.el  autotest-mode.el  bashdb.el  bashdb.elc  bbdb  2 : 2046 : 15:17:16 : /usr/share/emacs/site-lisp  [email protected]$ head auctex.el  ;;; auctex.el  ;;  ;; This can be used for starting up AUCTeX.  The following somewhat  ;; strange trick causes tex-site.el to be loaded in a way that can be  ;; safely undone using (unload-feature ‘tex-site).  ;;  (autoload ‘TeX-load-hack   (expand-file-name "tex-site.el" (file-name-directory load-file-name)))  (TeX-load-hack)  2 : 2047 : 15:23:53 : /usr/share/emacs/site-lisp  [email protected]$

这看起来已经不太像是在运行 Shell 了,倒象是在与某人合作编写一篇巨大的文章了,而 Shell 就是你的合作者。如果你真这么想的话,那就非常好了。至少你已经从枯燥乏味的日常工作当中找到些许的乐趣了。

小技巧

如果你登录在一台远程机器上工作,cat 一个文件后,需要把这个文件的内容保存到本地来,那么完全不需要启动一个 FTP session 去下载这个文件。你只需要选中缓冲区里面的文件内容,按下Meta-x 组合键,输入write-region 命令就可以把选中的内容保存在本地文件当中。


Linux C programming-emacs uses two

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.