-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvim.sh
224 lines (185 loc) · 5.61 KB
/
vim.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#!/bin/bash
# This script installs and configures Vim with Vundle plugin manager
# and a customized .vimrc configuration
# Exit on any error
set -e
###########################################
# Functions
###########################################
check_dependencies() {
if ! command -v git >/dev/null 2>&1; then
echo "Installing git..."
sudo apt-get update
sudo apt-get install -y git
fi
}
install_vundle() {
local vundle_path="$HOME/.vim/bundle/Vundle.vim"
if [ ! -d "$vundle_path" ]; then
echo "Installing Vundle..."
git clone "https://github.com/VundleVim/Vundle.vim.git" "$vundle_path"
else
echo "Vundle already installed"
fi
}
configure_vimrc() {
local vimrc="$HOME/.vimrc"
local timestamp=$(date +%Y%m%d_%H%M%S)
local temp_conf="${vimrc}.tmp"
echo "Configuring .vimrc..."
# Create new file if it doesn't exist
if [ ! -f "$vimrc" ]; then
touch "$vimrc"
else
# Backup existing .vimrc
cp "$vimrc" "${vimrc}.bak_${timestamp}"
fi
# Function to check if a setting exists
setting_exists() {
local pattern="$1"
grep -q "^[[:space:]]*$pattern" "$vimrc"
}
# Function to add a setting if it doesn't exist
add_setting() {
local setting="$1"
local comment="$2"
if ! setting_exists "$setting"; then
if [ -n "$comment" ]; then
echo "\" $comment" >> "$temp_conf"
fi
echo "$setting" >> "$temp_conf"
fi
}
# Copy existing content to temp file
cp "$vimrc" "$temp_conf"
# Add basic settings if they don't exist
{
# Basic settings section
if ! setting_exists "\" Basic settings"; then
echo -e "\n\" Basic settings" >> "$temp_conf"
fi
add_setting "set hlsearch" "highlight all search results"
add_setting "set ignorecase" "do case insensitive search"
add_setting "set incsearch" "show incremental search results as you type"
add_setting "set number" "display line number"
add_setting "set noswapfile" "disable swap file"
add_setting "set exrc"
add_setting "set secure"
# Vundle configuration
if ! grep -q "call vundle#begin()" "$vimrc"; then
cat >> "$temp_conf" << 'EOF'
" Vundle configuration
set nocompatible
filetype off
" Set up Vundle
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
" Plugins
Plugin 'VundleVim/Vundle.vim'
Plugin 'scrooloose/nerdtree'
Plugin 'Tagbar'
Plugin 'c.vim'
Plugin 'Syntastic'
call vundle#end()
filetype plugin indent on
EOF
fi
# Tagbar configuration if not exists
if ! grep -q "tagbar_type_ruby" "$vimrc"; then
cat >> "$temp_conf" << 'EOF'
" Tagbar configuration
nmap <C-t> :TagbarToggle<CR>
let g:tagbar_autofocus = 1
let g:tagbar_type_ruby = {
\ 'kinds' : [
\ 'm:modules',
\ 'c:classes',
\ 'd:describes',
\ 'C:contexts',
\ 'f:methods',
\ 'F:singleton methods'
\ ]
\ }
EOF
fi
# Encoding settings
if ! setting_exists "\" Encoding settings"; then
echo -e "\n\" Encoding settings" >> "$temp_conf"
fi
add_setting "set fileencodings=utf-8,gb2312,gb18030,gbk,ucs-bom,cp936,latin1"
add_setting "set enc=utf8"
add_setting "set fencs=utf8,gbk,gb2312,gb18030"
# Editor settings
if ! setting_exists "\" Editor settings"; then
echo -e "\n\" Editor settings" >> "$temp_conf"
fi
add_setting "syntax on"
add_setting "set paste"
add_setting "set tabstop=4"
add_setting "set shiftwidth=4"
add_setting "set softtabstop=4"
add_setting "set expandtab"
# File type specific settings
if ! grep -q "autocmd.*BufRead.*\.h,\*\.c" "$vimrc"; then
cat >> "$temp_conf" << 'EOF'
" File type specific settings
augroup project
autocmd!
autocmd BufRead,BufNewFile *.h,*.c set filetype=c.doxygen
augroup END
EOF
fi
}
# Replace original with updated config
mv "$temp_conf" "$vimrc"
echo "Vim configuration updated while preserving existing settings"
}
install_plugins() {
echo "Installing Vim plugins..."
vim -E -s -c "source ~/.vimrc" -c "PluginInstall" -c "qa"
}
show_help() {
cat << EOF
Usage: $(basename "$0") [-h|--help]
Install and configure Vim with Vundle plugin manager and custom configuration.
Options:
-h, --help Show this help message
The script will:
1. Install required dependencies (git)
2. Install Vundle plugin manager
3. Configure .vimrc with custom settings
4. Install configured plugins
Note:
- DO NOT run this script as root! Vim configuration should be done as regular user.
- Existing .vimrc will be backed up before modification.
- Some commands may ask for sudo password to install dependencies.
EOF
exit 0
}
check_user() {
if [ "$(id -u)" -eq 0 ]; then
echo "Warning: This script should NOT be run as root!"
echo "Vim configuration should be done as a regular user."
echo "Please run without sudo: './vim.sh'"
exit 1
fi
}
###########################################
# Main Script
###########################################
main() {
# Show help if requested
if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
show_help
fi
echo "Starting Vim setup..."
check_user
check_dependencies
install_vundle
configure_vimrc
install_plugins
echo "Vim setup completed successfully!"
echo "Note: Some plugins might require additional configuration."
}
# Run main function
main "$@"