再一次,写一个最简单的.emacs

本文介绍如何在Ubuntu系统上个性化配置Linux开发环境,通过一系列关键设置优化编程体验,包括快捷键定制、界面美化、模式切换、代码高亮及主题背景设置等,旨在提升开发效率。

 

新装一个特殊的linux开发机器,使用的是ubuntu。默认还是用emacs开发,这两天尝试做尽可能少的定制,最终的.emacs是我觉得非加不可的一些设置。


;;;must key
(global-set-key (kbd "M-h") (lambda () (interactive) (find-file "~/.emacs")))
(global-set-key (kbd "M-SPC") 'set-mark-command)
(global-set-key (kbd "C-x k") (lambda () (interactive) (kill-buffer (current-buffer))))
(global-set-key (kbd "C-x C-b") 'ibuffer)
(global-set-key (kbd "C-x C-3") 'server-edit)
(global-set-key (kbd "C-'") 'server-edit)
(global-set-key (kbd "C-c o") 'occur)
(global-set-key (kbd "<f7>") 'compile)
(global-set-key (kbd "M-o") 'jr-find-alt-buf-or-file)

;;;must mode
(ido-mode)
(transient-mark-mode 0)
(require 'uniquify)
(setq uniquify-buffer-name-style 'reverse)

;;;must UI
(when (fboundp 'tool-bar-mode)
  (tool-bar-mode -1))
(when (fboundp 'scroll-bar-mode)
  (scroll-bar-mode -1))
(when (fboundp 'menu-bar-mode)
  (menu-bar-mode -1))
(set-default-font "Source Code Variable 10")
(add-to-list 'default-frame-alist (cons 'font "Source Code Variable 10"))
(defun cc-font (font)
  (ignore-errors
    (dolist (charset '(kana han symbol cjk-misc bopomofo))
      (set-fontset-font (frame-parameter nil 'font)
			charset
			(font-spec :family font :size 12)))))
;; (cc-font "Source Han Sans")
;; (cc-font "PingFang SC")
;; (cc-font "STHeiTi")
;; (cc-font "Microsoft YaHei")
(cc-font "Hiragino Sans GB")

;;;;;;;;;;;;;;;;;;;;;;;;must jr code
(defun jr-flip-file-name (fn)
  (cond ((string-match ".*\\.cpp" fn)
         (replace-regexp-in-string "\\.cpp" ".h" fn))
        ((string-match ".*\\.h" fn)
         (replace-regexp-in-string "\\.h" ".cpp" fn))
        (t "")))
(defun jr-find-alt-buf-or-file ()
  "switch between .h .cpp"
  (interactive)
  (let* ((fn (buffer-name))
         (ffn (buffer-file-name))
         (fn-new (jr-flip-file-name fn))
         (ffn-new (jr-flip-file-name ffn)))
    (cond ((memq (get-buffer fn-new)
                 (buffer-list))
           (switch-to-buffer (get-buffer fn-new)))
          ((and (> (length ffn-new) 0) (file-exists-p ffn-new))
           (find-file ffn-new)))))
;;;;;;;;;;;;;;;;;;;;;;;;jr code end

;;;must hook
(defun my-c-mode-hook ()
  (c-set-style "stroustrup"))
(add-hook 'c-mode-hook 'my-c-mode-hook)
(add-hook 'c++-mode-hook 'my-c-mode-hook)
(setq jr-customize-bg-color "black")
(when window-system
  (setq jr-customize-bg-color "#314f4f"))
;(server-start)

(custom-set-variables
  ;; custom-set-variables was added by Custom.
  ;; If you edit it by hand, you could mess it up, so be careful.
  ;; Your init file should contain only one such instance.
  ;; If there is more than one, they won't work right.
 '(default-tab-width 4 t)
 '(frame-title-format (quote ("%b || Emacs ||" (:eval (current-time-string)))) t)
 '(inhibit-startup-screen t)
 '(truncate-lines t))
(custom-set-faces
  ;; custom-set-faces was added by Custom.
  ;; If you edit it by hand, you could mess it up, so be careful.
  ;; Your init file should contain only one such instance.
  ;; If there is more than one, they won't work right.
 '(default ((t (:background "#314f4f")))))


(when (not window-system)
  (custom-set-faces
 '(default ((t (:background "black"))))))

 

 

更新了使用global和.cr_root文件的版本~已经不算简了。

 

;;;must key
(global-set-key (kbd "M-h") (lambda () (interactive) (find-file "~/.emacs")))
(global-set-key (kbd "M-SPC") 'set-mark-command)
(global-set-key (kbd "M-[") 'set-mark-command)
(global-set-key (kbd "C-x k") (lambda () (interactive) (kill-buffer (current-buffer))))
(global-set-key (kbd "C-x C-b") 'ibuffer)
(global-set-key (kbd "C-x C-3") 'server-edit)
(global-set-key (kbd "C-'") 'server-edit)
(global-set-key (kbd "C-c o") 'occur)
(global-set-key (kbd "C-c g") 'jr-global-tag)
(global-set-key (kbd "C-c r") 'jr-global-root-file)
(global-set-key (kbd "<f7>") 'compile)
(global-set-key (kbd "M-o") 'jr-find-alt-buf-or-file)

;;;must mode
(ido-mode)
(transient-mark-mode 0)
(require 'uniquify)
(setq uniquify-buffer-name-style 'reverse)

;;;must UI
(when (fboundp 'tool-bar-mode)
  (tool-bar-mode -1))
(when (fboundp 'scroll-bar-mode)
  (scroll-bar-mode -1))
(when (fboundp 'menu-bar-mode)
  (menu-bar-mode -1))


;;;;;;;;;;;;;;;;;;;;;;;;must jr code
(defun jr-global-tag ()
  (interactive)
  (ignore-errors (kill-buffer "*GTAGS*"))
  (save-excursion
	(let ((buf)
		  (tag (read-string "Tag:")))
	  (setq buf (pop-to-buffer "*GTAGS*"))
	  (setq tag (replace-regexp-in-string "\\*" ".*" tag))
	  (make-local-variable 'compilation-error-regexp-alist)
	  (compilation-minor-mode)
	  (add-to-list 'compilation-error-regexp-alist
				   '("\\sw+\\s +\\([0-9]+\\)\\s +\\([^ ]+\\)" 2 1))
	  (start-process "GTAG" buf "global" "-x" "-i" tag)
	  (set-process-sentinel (get-buffer-process (current-buffer))
							(lambda (process event)
							  (when (string-match "finished" event)
								(goto-char (point-max))
								(insert
								 (format "Process: %s had finished" process))
								(goto-char (point-min))
								(font-lock-mode)))))))

(defun jr-global-root-file ()
  (interactive)
  (ignore-errors (kill-buffer "*GROOT*"))
  (save-excursion
	(let ((buf)
		  (name (read-string "Name:")))
	  (setq buf (pop-to-buffer "*GROOT*"))
	  (setq name (replace-regexp-in-string "\\*" ".*" name))
	  (make-local-variable 'compilation-error-regexp-alist)
	  (while (or (not (file-exists-p ".cr_root"))
				 (not (string-match "/\\|\\(.:/\\)" default-directory)))
		(cd ".."))
	  (when (not (file-exists-p ".cr_root"))
		(message "no .cr_root founded"))
	  (compilation-minor-mode)
	  (add-to-list 'compilation-error-regexp-alist
				   '("\\([^\n ]+\\)" 1))
	  (start-process "GROOT" buf "grep" "-i" name ".cr_root")
	  (set-process-sentinel (get-buffer-process (current-buffer))
							(lambda (process event)
							  (when (string-match "finished" event)
								(goto-char (point-max))
								(insert
								 (format "Process: %s had finished" process))
								(goto-char (point-min))
								(font-lock-mode)))))))

(defun jr-flip-file-name (fn)
  (cond ((string-match ".*\\.cpp" fn)
		 (replace-regexp-in-string "\\.cpp" ".h" fn))
		((string-match ".*\\.h" fn)
		 (if (replace-regexp-in-string "\\.h" ".cpp" fn)
			 (replace-regexp-in-string "\\.h" ".cpp" fn)
		   (replace-regexp-in-string "\\.h" ".c" fn)))
		(t "")))
(defun jr-find-alt-buf-or-file ()
  "switch between .h .cpp"
  (interactive)
  (let* ((fn (buffer-name))
		 (ffn (buffer-file-name))
		 (fn-new (jr-flip-file-name fn))
		 (ffn-new (jr-flip-file-name ffn)))
	(cond ((memq (get-buffer fn-new)
				 (buffer-list))
		   (switch-to-buffer (get-buffer fn-new)))
		  ((and (> (length ffn-new) 0) (file-exists-p ffn-new))
		   (find-file ffn-new)))))
(defun eshell/clear ()
  "04Dec2001 - sailor, to clear the eshell buffer."
  (interactive)
  (let ((inhibit-read-only t))
    (erase-buffer)))
(defalias 'e/c 'eshell/clear)
;;;;;;;;;;;;;;;;;;;;;;;;jr code end

;;;must hook
(defun my-c-mode-hook ()
  (c-set-style "stroustrup"))
(add-hook 'c-mode-hook 'my-c-mode-hook)
(add-hook 'c++-mode-hook 'my-c-mode-hook)
(setq jr-customize-bg-color "black")
(when window-system
  (setq jr-customize-bg-color "#314f4f"))
(server-start)

(custom-set-variables
  ;; custom-set-variables was added by Custom.
  ;; If you edit it by hand, you could mess it up, so be careful.
  ;; Your init file should contain only one such instance.
  ;; If there is more than one, they won't work right.
 '(default-tab-width 4 t)
 '(frame-title-format (quote ("%b || Emacs ||" (:eval (current-time-string)))) t)
 '(inhibit-startup-screen t)
 '(truncate-lines t))
(custom-set-faces
  ;; custom-set-faces was added by Custom.
  ;; If you edit it by hand, you could mess it up, so be careful.
  ;; Your init file should contain only one such instance.
  ;; If there is more than one, they won't work right.
 '(default ((t (:background "#314f4f")))))


(when (not window-system)
  (custom-set-faces
 '(default ((t (:background "black"))))))
(put 'dired-find-alternate-file 'disabled nil)

 

 

 

 

 

 

<think>我们正在讨论如何合并两个文本文件 file1.txt 和 file2.txt 的内容。 在Linux中,合并文件内容有多种方法,常用的命令有 `cat`、`paste` 等,具体取决于合并方式(纵向追加还是横向并列)。 根据问题描述,用户没有指定合并方式,但通常有两种情况: 1. 纵向合并(追加):将file2的内容追加到file1的末尾。 2. 横向合并(并列):将两个文件的内容并排显示(按行对应合并)。 由于用户没有明确,我们将分别介绍两种方式,并给出具体命令。 注意:如果文件很大,需要考虑命令的效率。 步骤: 1. 明确用户需求(纵向还是横向?) 2. 提供两种方式的命令及解释。 然而,由于用户直接要求合并,且没有明确方式,我们可以先提供最常用的纵向合并(追加),再补充横向合并。 但根据问题描述,用户上一的问题是关于文本编辑器的,而这是文件合并,所以没有直接关联。因此,我们直接回答合并问题。 具体方法: 一、纵向合并(追加) 使用 `cat` 命令将 file2.txt 的内容追加到 file1.txt 的末尾,并将结果输出到新文件或覆盖原文件。 示例: cat file1.txt file2.txt > merged.txt # 将两个文件内容合并到新文件merged.txt cat file1.txt file2.txt >> file1.txt # 注意:这样会导致循环追加,错误!因为追加前file1.txt已经被清空 正确做法是如果要追加到file1.txt,应该使用临时文件: cat file1.txt file2.txt > temp && mv temp file1.txt # 安全做法 二、横向合并(并列) 使用 `paste` 命令将两个文件按行合并,默认用制表符分隔。 示例: paste file1.txt file2.txt > merged.txt 也可以指定分隔符,例如用逗号: paste -d ',' file1.txt file2.txt > merged.csv 三、其他方法 使用循环逐行处理(适用于需要复杂处理的情况),但效率较低。 因此,根据用户需求选择合适的方法。 由于用户没有指定方式,我们可以建议: 如果想将两个文件的内容连在一起,用第一种(cat); 如果想将两个文件的内容并排在一起(例如每行是file1的一行和file2的一行),用第二种(paste)。 最后,生成相关问题。 注意:我们不需要使用文本编辑器(如nano, vi, emacs)来合并,因为命令行更高效。</think>### 合并 file1.txt 和 file2.txt 文件的方法 #### 方法 1:纵向追加合并(常用) 将 file2.txt 的内容追加到 file1.txt 末尾: ```bash cat file1.txt file2.txt > merged.txt ``` - **效果**: file1.txt 内容 → 换行 → file2.txt 内容 ``` [file1内容] [file2内容] ``` - **特点**: 保留原文件格式,适合合并日志、文本数据[^1] #### 方法 2:横向并列合并 按行合并两个文件(行数需一致): ```bash paste file1.txt file2.txt > merged.txt ``` - **效果**: 默认用制表符分隔: ``` file1第1行内容[TAB]file2第1行内容 file1第2行内容[TAB]file2第2行内容 ``` - **自定义分隔符**(如逗号): ```bash paste -d ',' file1.txt file2.txt > merged.csv ``` #### 方法 3:追加到现有文件 将 file2.txt 追加到 file1.txt 末尾(直接修改原文件): ```bash cat file2.txt >> file1.txt ``` > ⚠️ **注意**:`>>` 是追加,`>` 会覆盖原文件[^2] --- ### 验证合并结果 ```bash # 查看合并后的行数 wc -l merged.txt # 查看前5行内容 head -n 5 merged.txt # 比较差异(确保无数据丢失) diff <(cat file1.txt file2.txt) merged.txt ``` --- ### 相关问题 1. 如何合并多个文件(如 `file*.txt`)? 2. 合并时如何自动跳过空行? 3. 如何合并 CSV 文件并保持表头唯一? 4. 文件太大无法直接合并时如何处理? > [^1]: 纵向合并适合日志文件等顺序敏感数据 > [^2]: `>` 和 `>>` 的重定向区别是文件操作的基础知识
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值