Skip to content

Instantly share code, notes, and snippets.

@amoeba
Last active January 17, 2025 02:44
Show Gist options
  • Save amoeba/e1172ede2e40dc5a25efbe3893d90242 to your computer and use it in GitHub Desktop.
Save amoeba/e1172ede2e40dc5a25efbe3893d90242 to your computer and use it in GitHub Desktop.
Emacs C++, clangd, lsp-mode config
;; Initialize package system
(require 'package)
(add-to-list 'package-archives '("melpa" . "https://melpa.org/packages/") t)
(package-initialize)
;; Install required packages if not present
(unless package-archive-contents
(package-refresh-contents))
(setq package-selected-packages
'(lsp-mode
lsp-ui
company
company-box
which-key
yasnippet
flycheck
clang-format))
(package-install-selected-packages)
;; Basic UI improvements
(menu-bar-mode -1)
(tool-bar-mode -1)
(scroll-bar-mode -1)
(global-display-line-numbers-mode 1)
;; LSP configuration
(require 'lsp-mode)
(setq lsp-prefer-flymake nil)
(setq lsp-keymap-prefix "C-c l")
;; Enable LSP for C/C++ files
(add-hook 'c-mode-hook 'lsp)
(add-hook 'c++-mode-hook 'lsp)
;; LSP UI configurations
(require 'lsp-ui)
(setq lsp-ui-doc-enable t)
(setq lsp-ui-doc-position 'top)
(setq lsp-ui-sideline-enable t)
;; Company mode configuration
(require 'company)
(add-hook 'after-init-hook 'global-company-mode)
(setq company-minimum-prefix-length 1)
(setq company-idle-delay 0.0)
(setq company-selection-wrap-around t)
;; Company box for better looking completions
(require 'company-box)
(add-hook 'company-mode-hook 'company-box-mode)
;; Which-key helps you remember keybindings
(require 'which-key)
(which-key-mode)
;; YASnippet for code snippets
(require 'yasnippet)
(yas-global-mode 1)
;; Clang-format configuration
(require 'clang-format)
(setq clang-format-style "google")
(global-set-key (kbd "C-c f") 'clang-format-buffer)
;; Custom keybindings
(global-set-key (kbd "C-c C-c") 'compile)
(global-set-key (kbd "C-c d") 'lsp-find-definition)
(global-set-key (kbd "C-c r") 'lsp-find-references)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment