Why not login to Qiita and try out its useful features?

We'll deliver articles that match you.

You can read useful information later.

0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

C言語をvimで始めてみよう - プラグインとセットアップ

Last updated at Posted at 2025-01-27

環境

  • OS: macOS / Linux
  • Vimのバージョン: 8.2

Vimのインストール

だいたいどんな環境でもデフォルトで入っているVimですが、もし消してしまっていたり無かったりした場合は下記のコマンドでインストールしてください。

Linux

sudo apt update && sudo apt install vim

MacOS

brew install vim

プラグインマネージャー「vim-plug」の導入

Linux/macOS

curl -fLo ~/.vim/autoload/plug.vim --create-dirs \
https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim

coc.nvimのためにnode.jsをインストール

sudo apt install nodejs npm

COC.nvimのためにNode.jsをインストール

以下のコマンドでNode.jsとnpmをインストールします。COC.nvimはNode.jsを前提とするので下記のコマンドを使って必要なものをインストールしてください。

Linux

sudo apt install nodejs npm

Mac

sudo brew install node npm

coc.nvimのセットアップ

coc.nvim を使用するために、COC Extensionsをインストールし、C言語開発に特化した設定を行います。

COC Extensionsのインストール

以下のコマンドをVimのメニューバーに打ち込むことで、C言語用の拡張機能をインストールします。

:CocInstall coc-clangd

clangdのインストール

Linux

sudo apt install clangd

macOS

brew install llvm

macOSでは、だいたいパスが通っていないのでパスを通してください。

export PATH="/usr/local/opt/llvm/bin:$PATH"

最後に下記を~/.vimrcに記述してください。(vim ~/.vimrc)

" ベース設定
set nocompatible
syntax on
set number
set tabstop=4
set shiftwidth=4
set noexpandtab
set autoindent
set smartindent
set showcmd
set laststatus=2
set noshowmode

" 検索設定
set ignorecase
set smartcase
set incsearch
set hlsearch

" 表示とファイル設定
set nowrap
set ruler
set showmode
set nobackup
set nowritebackup
set noswapfile

" C言語特化のローカル設定
autocmd FileType c setlocal cinoptions=(0
autocmd FileType c setlocal formatoptions=croql
autocmd FileType c setlocal comments=s1:/*,mb:*,ex:*/,://

" その他の便利な設定
set wildmenu
set backspace=indent,eol,start

" 選択部分の強調色変更
highlight Search ctermbg=none guibg=#33467c guifg=#c0caf5

" 現在行の背景色変更
highlight CursorLine ctermbg=none guibg=#1e202e


" プラグイン管理(vim-plug利用)
call plug#begin('~/.vim/plugged')

" プラグイン一覧
Plug 'wakatime/vim-wakatime'            " 開発時間計測
Plug 'preservim/nerdtree'               " ファイルツリー
Plug 'itchyny/lightline.vim'            " ステータスライン
Plug 'scrooloose/nerdcommenter'         " コメント管理
Plug 'jiangmiao/auto-pairs'             " 自動括弧補完
Plug 'honza/vim-snippets'               " スニペット
Plug 'tpope/vim-fugitive'               " Git連携
Plug 'ryanoasis/vim-devicons'           " アイコン
Plug 'ghifarit53/tokyonight-vim'
Plug 'puremourning/vimspector'
Plug 'neoclide/coc.nvim', {'branch': 'release'}

call plug#end()

set termguicolors
let g:tokyonight_style = 'night'
let g:tokyonight_enable_italic = 1
colorscheme tokyonight
highlight Comment guifg=#a9b1d6 ctermfg=lightblue

" auto-pairs
let g:auto_pairs = 1

" cocの設定
inoremap <silent><expr> <TAB> pumvisible() ? "\<C-n>" : "\<TAB>"
inoremap <silent><expr> <S-TAB> pumvisible() ? "\<C-p>" : "\<C-h>"

" エラーリストの移動
nmap <silent> [g <Plug>(coc-diagnostic-prev)
nmap <silent> ]g <Plug>(coc-diagnostic-next)

" 定義や参照
nmap <silent> gd <Plug>(coc-definition)
nmap <silent> gr <Plug>(coc-references)

" lightlineの設定
let g:lightline = {
      \ 'colorscheme': 'tokyonight',
      \ 'active': {
      \   'left': [ [ 'mode', 'paste' ],
      \             [ 'gitbranch', 'readonly', 'filename', 'modified' ] ],
      \   'right': [ [ 'lineinfo' ],
      \              [ 'percent' ],
      \              [ 'fileformat', 'fileencoding', 'filetype' ] ]
      \ },
      \ 'component_function': {
      \   'gitbranch': 'FugitiveHead',
      \   'readonly': 'LightlineReadonly',
      \   'filename': 'LightlineFilename',
      \ },
      \ 'mode_map': {
      \   'n': 'N',
      \   'i': 'I',
      \   'R': 'R',
      \   'v': 'V',
      \   'V': 'VL',
      \   "\<C-v>": 'VB',
      \   'c': 'C',
      \   't': 'T',
      \ }
      \ }

" カスタム関数: 読み取り専用表示
function! LightlineReadonly()
  return &readonly ? 'RO' : ''
endfunction

" カスタム関数: ファイル名表示(空の場合は [No Name])
function! LightlineFilename()
  return expand('%:t') !=# '' ? expand('%:t') : '[No Name]'
endfunction

" Gitブランチ表示のためのvim-fugitive設定
" FugitiveHead関数はvim-fugitiveかr
if !exists('*FugitiveHead')
  function! FugitiveHead()
    return ''
  endfunction
endif

最後にvimのメニューバーで下記のコマンドを入力することでプラグインのインストールが終わります。

PlugInstall

以上の設定を完了することで、C言語を効率的に開発できるVim環境が整います。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up

Qiita Conference 2025 will be held!: 4/23(wed) - 4/25(Fri)

Qiita Conference is the largest tech conference in Qiita!

Keynote Speaker

ymrl、Masanobu Naruse, Takeshi Kano, Junichi Ito, uhyo, Hiroshi Tokumaru, MinoDriven, Minorun, Hiroyuki Sakuraba, tenntenn, drken, konifar

View event details
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?