【问题标题】:Have VIM add a final new line on save让 VIM 在保存时添加最后一个新行
【发布时间】:2016-07-05 15:51:23
【问题描述】:

我希望 VIM 在我保存文本文件时添加最后一个新行。奇怪的是,搜索它我只找到有关如何禁用此行为的说明,但我明确想要它。

这是我的 VIM 安装:

$ vim --version

VIM - Vi IMproved 7.4 (2013 Aug 10, compiled Mar 18 2016 01:00:23)
MacOS X (unix) version
Included patches: 1-1525
Compiled by Homebrew
Huge version without GUI.  Features included (+) or not (-):
+acl             +farsi           +mouse_netterm   +tag_binary
+arabic          +file_in_path    +mouse_sgr       +tag_old_static
+autocmd         +find_in_path    -mouse_sysmouse  -tag_any_white
-balloon_eval    +float           +mouse_urxvt     -tcl
-browse          +folding         +mouse_xterm     +terminfo
++builtin_terms  -footer          +multi_byte      +termresponse
+byte_offset     +fork()          +multi_lang      +textobjects
+channel         -gettext         -mzscheme        +title
+cindent         -hangul_input    +netbeans_intg   -toolbar
-clientserver    +iconv           +packages        +user_commands
+clipboard       +insert_expand   +path_extra      +vertsplit
+cmdline_compl   +job             +perl            +virtualedit
+cmdline_hist    +jumplist        +persistent_undo +visual
+cmdline_info    +keymap          +postscript      +visualextra
+comments        +langmap         +printer         +viminfo
+conceal         +libcall         +profile         +vreplace
+cryptv          +linebreak       +python          +wildignore
+cscope          +lispindent      -python3         +wildmenu
+cursorbind      +listcmds        +quickfix        +windows
+cursorshape     +localmap        +reltime         +writebackup
+dialog_con      -lua             +rightleft       -X11
+diff            +menu            +ruby            -xfontset
+digraphs        +mksession       +scrollbind      -xim
-dnd             +modify_fname    +signs           -xsmp
-ebcdic          +mouse           +smartindent     -xterm_clipboard
+emacs_tags      -mouseshape      +startuptime     -xterm_save
+eval            +mouse_dec       +statusline      -xpm
+ex_extra        -mouse_gpm       -sun_workshop
+extra_search    -mouse_jsbterm   +syntax
   system vimrc file: "$VIM/vimrc"
     user vimrc file: "$HOME/.vimrc"
 2nd user vimrc file: "~/.vim/vimrc"
      user exrc file: "$HOME/.exrc"
  fall-back for $VIM: "/usr/local/share/vim"
Compilation: /usr/bin/clang -c -I. -Iproto -DHAVE_CONFIG_H   -DMACOS_X_UNIX  -Os -w -pipe -march=native -mmacosx-version-min=10.11 -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=1
Linking: /usr/bin/clang   -L. -L/usr/local/lib -L/usr/local/lib -Wl,-headerpad_max_install_names -o vim        -lm  -lncurses -liconv -framework Cocoa   -fstack-protector  -L/System/Library/Perl/5.18/darwin-thread-multi-2level/CORE -lperl -framework Python   -lruby.2.0.0 -lobjc

我的.vimrc:

autocmd BufWritePost .vimrc source %

set nocompatible
filetype off

" Set the runtime path to include Vundle and initialize
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()

Plugin 'altercation/vim-colors-solarized'
Plugin 'ctrlpvim/ctrlp.vim'
Plugin 'vim-airline/vim-airline'

call vundle#end()
filetype plugin indent on

syntax on
colorscheme solarized

set backspace=2
set textwidth=80
set colorcolumn=+1
set laststatus=2
set tabstop=4
set shiftwidth=4
set expandtab
set number
set hidden
set hlsearch
set incsearch
set mouse=a

" Use ag over grep
set grepprg=ag\ --nogroup\ --nocolor

" Use ag in CtrlP for listing files. Lightning fast and respects .gitignore
let g:ctrlp_user_command = 'ag %s -l --nocolor -g ""'

" ag is fast enough that CtrlP doesn't need to cache
let g:ctrlp_use_caching = 0

for dir in ['backup', 'swap', 'undo']
    let path = $HOME . '/.vim/' . dir
    if !isdirectory(path)
        call mkdir(path, 'p')
    endif
endfor

set backupdir=~/.vim/backup//
set directory=~/.vim/swap//
set undodir=~/.vim/undo//

let g:airline_powerline_fonts = 1

let mapleader=","
imap jj <Esc>
nnoremap ; :

" Yank text to the OS X clipboard
noremap <leader>y "*y
noremap <leader>yy "*Y

" Preserve indentation while pasting text from the OS X clipboard
noremap <leader>p :set paste<CR>:put  *<CR>:set nopaste<CR>

" Disable arrow keys
map <up> <nop>
map <down> <nop>
map <left> <nop>
map <right> <nop>

" Easy window navigation
map <C-h> <C-w>h
map <C-j> <C-w>j
map <C-k> <C-w>k
map <C-l> <C-w>l

" Clear search
nmap <silent> ,/ :nohlsearch<CR>

【问题讨论】:

  • 这可能是binary 选项。如果设置,vim 不会在最后一行添加换行符。 Vim 可以根据识别的文件类型自动设置binary 选项。您可以使用:set binary? 进行测试。
  • 谢谢,但很遗憾nobinary
  • 您想在最后一行之后添加一个实际的空行还是在最后一行的末尾添加一个“换行符”字符?前者没有意义,但它可能可以通过自动命令来完成。 Vim 默认已经做到了后者。
  • 谢谢,这很有帮助! VIM 确实在编写我想要的换行符,只是没有像其他编辑器那样显示它。在不同的编辑器中打开它,我实际上可以看到最后一个空白行,VIM 似乎隐藏了它。

标签: linux unix vim text gnu


【解决方案1】:

正如romainl 发现的那样,VIM 确实插入了 EOL 字符,但与我习惯的其他编辑器不同,它没有将其显示为新行。

见:Why do I need vim in binary mode for 'noeol' to work?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-12
    • 1970-01-01
    相关资源
    最近更新 更多