1. 程式人生 > >Linux檔案編輯管理常用命令

Linux檔案編輯管理常用命令

  • 檔案常用命令less、more、tail、head、cat
  • vim編輯器使用

  • 常用命令

    • cat、less、more
      command file
      more和less可以分屏檢視檔案,more翻屏至尾部後自動退出,less可以往回翻屏檢視
    • more
    • tail:檢視檔案的後幾行,預設檢視後十行
      tail [options] file
      -n # :檢視後幾行
      -#
      -f :檢視檔案尾部內容結束後不退出,跟隨顯示新增的行
      1.檢視重新整理的日誌
      2.日誌排查
      tail -f /var/log/messages | grep -i error

    • head:檢視檔案的前幾行,預設前10行
      head [options] file
      常用opts:
      -n # :檢視前幾行
      -#
  • vim編輯器使用
    vi:visual interface,vim :vi improved增強版
    基本模式:命令模式、輸入模式、末行模式
    vim file 進入預設為命令模式,三者間切換:
    Linux檔案編輯管理常用命令
    i:insert,在游標所在處插入
    a: append,在游標所在處後插入
    o:在游標所在處的下方新增一個新行
    I: 在游標所在行的行首插入
    A:在游標所在行的行尾插入
    O:在游標所在處的上方新增一個新行

    • 開啟檔案
      vim [options] file...
    • 關閉檔案
      ZZ 命令列模式,儲存退出
      :q 退出
      :q! 強制退出
      :wq 儲存並退出
      :x 等同於:wq
      :w /path/to/somefile儲存至給定檔案

    • 游標跳轉
      字元跳轉方向鍵
      單詞間跳轉:
      w:下一個單詞詞首
      e:當前或後一個單詞詞尾
      b:當前或前一個單詞詞首
      行首行尾行間跳轉:
      ^:跳至行首第一個非空白字元
      0:跳至行首
      $:跳至行尾
      #G:跳至#指定行
      1G,gg:第一行
      G:跳到最後一行
      翻屏操作:
      Ctrl+f:向檔案尾翻一屏
      Ctrl+b:向檔案首部翻一屏
      Ctrl+d:向檔案尾部翻半屏
      Ctrl+u:向檔案首部翻半屏
      Enter:按行向後翻

    • vim命令模式
      
      字元編輯:
              x:刪除游標所在處的字元;
              #x:刪除游標所在處起始的#個字元;                              
      替換命令(replace):
              r:替換游標所在處的字元;
              rCHAR
      刪除命令:
                  d:刪除命令,可結合游標跳轉字元,實現範圍刪除;
                  d$:刪除游標所在處至行尾的所有字元
                  d^: 刪除游標所在處至行首的所有字元             
                  dw:向後刪除游標所在處的單詞,但游標所在的字元不會刪除
                  de:向後刪除游標所在處的單詞
                  db:向前刪除游標所在處的單詞
                  dd:刪除游標所在處的行;
                  #dd:刪除游標所處的行起始的共#行;         
      貼上命令:
              p:緩衝區中的內容如果為整行,則貼上在當前游標所在行的下方;否則,則貼上至當前游標所在處的後方;
              P:緩衝區中的內容如果為整行,則貼上在當前游標所在行的上方;否則,則貼上至當前游標所在處的前方;            
      複製命令:y 複製,工作行為相似於d命令;
                  y$
                  y^
                  y0                  
                  ye
                  yw
                  yb                      
                  yy:複製一整行
                  #yy:複製游標處起#行                        
      其它編輯操作:         
              視覺化模式:
              v:按字元選定
              V:按行選定                  
              結合編輯命令使用:d, c, y實現批量操作

    撤銷操作:
    u:撤銷此前的操作;
    #u:撤銷此前的#個操作;
    恢復此前的撤銷:
    Ctrl+r

    
    
    -  vim末行模式
    地址定界
            :start_pos[,end_pos]
                #:指定第#行
                .:當前行;
                $:最後一行;
                #,#:指定行範圍,左側為起始行,右側為結束行;
                #,+#:指定行範圍,左側為超始行絕對編號,右側為相對左側行號的偏移量;例如:5,+2
                .,$-1:當前行至倒數第一行
                1,$:全文
                %:全文 
                /pattern/:從游標所在處起始向檔案尾部第一次被模式所匹配到的行;
                /first/,$
                /pat1/,/pat2/:從游標所在處起始,第一次由pat1匹配到的行開始,至第一次由pat2匹配到的行結束之間的所有行;
                可同編輯命令一同使用,實現編輯操作:d、y、c         
    查詢:
            /PATTERN:從當前游標所在處向檔案的尾部查詢能夠被當前模式匹配到的所有字串;
            ?PATTERN:從當前游標所在處向檔案的首部查詢能夠被當前模式匹配到的所有字串;
                n:下一個
                N:上一個
    查詢並替換:
            s:末行模式的命令;使用格式:s/ ../ ../
                s/old_content/new_content/修飾符
                    old_content:可使用正則表示式;
                    new_content:不可以使用下則表示式,但可以引用;                       
                    修飾符:
                        i:忽略大小寫;
                        g:全域性替換
            注:分隔符替換為其它非常用字元
                    [email protected]@@
                    s###
  • vim其他使用功能
    編輯多個檔案:
    vim [-o|-O] file1 file2 ...
    -o:水平分割視窗
    -O:垂直分割
    視窗間切換:Ctrl+w,方向鍵切換即可
    注:單個檔案也可以分屏:Ctrl+w,s/v #s表示水平分割,v表示垂直分割

  • vim的配置檔案:可以定製vim的工作特性
    注意:在末行模式下的設定,僅對當前vim程序有效;永久有效:
    全域性:/etc/vimrc
    使用者個人:~/.vimrc
    可以使用幫助:help
if v:lang =~ "utf8$" || v:lang =~ "UTF-8$"
   set fileencodings=ucs-bom,utf-8,latin1
endif
set nocompatible    " Use Vim defaults (much better!)
set bs=indent,eol,start     " allow backspacing over everything in insert mode
set ai          " always set autoindenting on
set sw=4
set si          "smart suo jin
"set backup     " keep a backup file
set viminfo='20,\"50    " read/write a .viminfo file, don't store more
            " than 50 lines of registers
set history=50      " keep 50 lines of command line history
set ruler       " show the cursor position all the time
set tabstop=4 
" Only do this part when compiled with support for autocommands
if has("autocmd")
  augroup redhat
  autocmd!
  " In text files, always limit the width of text to 78 characters
  " autocmd BufRead *.txt set tw=78
  " When editing a file, always jump to the last cursor position
  autocmd BufReadPost *
  \ if line("'\"") > 0 && line ("'\"") <= line("$") |
  \   exe "normal! g'\"" |
  \ endif
  " don't write swapfile on most commonly used directories for NFS mounts or USB sticks
  autocmd BufNewFile,BufReadPre /media/*,/run/media/*,/mnt/* set directory=~/tmp,/var/tmp,/tmp
  " start with spec file template
  autocmd BufNewFile *.spec 0r /usr/share/vim/vimfiles/template.spec
  augroup END
endif
if has("cscope") && filereadable("/usr/bin/cscope")
   set csprg=/usr/bin/cscope
   set csto=0
   set cst
   set nocsverb
   " add any database in current directory
   if filereadable("cscope.out")
      cs add $PWD/cscope.out
   " else add database pointed to by environment
   elseif $CSCOPE_DB != ""
      cs add $CSCOPE_DB
   endif
   set csverb
endif

" Switch syntax highlighting on, when the terminal has colors
" Also switch on highlighting the last used search pattern.
if &t_Co > 2 || has("gui_running")
  syntax on
  set hlsearch
endif

filetype plugin on

if &term=="xterm"
     set t_Co=8
     set t_Sb=[4%dm
     set t_Sf=[3%dm
endif

" Don't wake up system with blinking cursor:
" http://www.linuxpowertop.org/known.php
let &guicursor = &guicursor . ",a:blinkon0"

"set auto add shellscript title #設定指令碼xxx.sh預設配置
autocmd BufNewFile *.py,*.cc,*.sh,*java exec ":call SetTitle()"
func SetTitle()
    if expand("%:e") == "sh"
        call setline(1, "#!/bin/bash")
        call setline(2, "#Author:tong")
        call setline(3, "#Blog:http://www.xuetong.51cto.com")
        call setline(4, "#Time:".strftime("%F %T"))
        call setline(5, "#Name:".expand("%"))
        call setline(6, "#Version:v1.0")
        call setline(7, "#Description:this is a test script")
    endif
endfunc