当前位置: 首页 > Linux

Linux——vim与文件操作

时间:2023-04-06 22:57:01 Linux

vim编辑文件内容——三种模式切换最后一行模式:q退出编辑:w保存文本:wq保存退出:q!强制退出:setnu显示行号:sethlserch高亮:setnohlserch取消高亮/字符串全文搜索,从上到下?字符串全文搜索,从下往上例子:/c全文搜索c$endoffiles:搜索和替换的含义:1、$s/oldstring/newstring从第一行到文件末尾,每行中的第一个旧字符串替换为新字符串:1,$s/oldstring/newstring/g从第一行到文件末尾,全文旧字符串替换为新字符串1,$num1,num2?????.,$fromthecurrenttotheendofthefile命令方式:所有命令不涉及当前行,只包括操作对象,不会改变当前行光标快速移动:gg移动到第一行textshift+g移动到最后一行numshift+g移动到指定行numshift+6移动到当前行行首shift+4移动到当前行行尾delete:dd删除当前行numddstart从光标开始,删除num行dnumshift+g,从num行号删除到光标undo/restoreatu撤销上次操作ctrl+r重做上次操作Copyyy复制光标所在行numyy开始从光标处,往下复制num行ynumshift+g,从第num行号复制到光标处,然后粘贴p粘贴复制或删除的内容SetyourvimsetTAB指定~目录下的空间,create.vimrcsetnusetautoindentsetcindentsettabstop=4setshiftwidth=4setsmartindentsetshowcmdsetmouse=asetbackground=lightsetclipboard+=unnamedsetfoldenablesetfoldmethod=indentsetfoldmethod=syntaxsetfoldmethod=syntax在文件内容操作视图上设置nofoldenable语法文件内容查看部分内容morefilenameq退出,回车查看显示的n%//退出后,屏幕上仍显示内容morepasswdlessfilenameq退出,上下查找(END结束)lesspasswd//退出后,screeniscleanhead-numfilename显示文件前num行的内容head-5passwd//前五行的内容tail-numfilename显示文件最后num行的内容tail-5passwdtoviewallcontentscatfilename//注意如果文件过大,屏幕会显示不全,因为有限制,cat主要不是用来查看文件内容的cat1.查看文件内容2.合并文件内容catnameanameb....namen>namec[root@localhost2020-3-6]#toucha.cppb.cpp[root@localhost2020-3-6]#lsa.cppb.cpp[root@localhost2020-3-6]#vima.cpp[root@localhost2020-3-6]#vimb.cpp[root@localhost2020-3-6]#morea.cppabcd[root@localhost2020-3-6]#moreb.cppefjh[root@localhost2020-3-6]#cata.cppb.cpp>C。cpp[root@localhost2020-3-6]#lsa.cppb.cppc.cpp[root@localhost2020-3-6]#morec.cppabcdefjh3。重定向:这里指写一些内容cat>filename//注意,Ctrl+c必须另起一行[root@localhost2020-3-6]#cat>chong.chellomanhellowomen^C[root@localhost2020-3-6]#morechong.chellomanhellowomen[root@localhost2020-3-6]#cat>rechong.chello^C[root@localhost2020-3-6]#morerechong.c[root@localhost2020-3-6]#搜索文件内容find目录名-搜索方法目标文件名[root@localhost2020-3-6]#find/-namebin/sys/kernel/debug/tracing/options/bin/bin/usr/share/locale/bin/usr/bin/usr/lib/debug/bin/usr/lib/debug/usr/bin/usr/local/bintextwc-l的统计信息filename//统计文件名的行数wc-wfilename//统计字数wc-cfilename//统计字节数文件压缩和解压最常见的两种类型:.tarzcftarname.tarfile1file2//一键压缩tarzxftarname.tar//一键解压tar选项:z使tar命令同时具有压缩和解压功能(GUN版后)一键打包和压缩过程[root@localhost2020-3-6]#lsa.cppb.cppc.cppchong.crechong.c[root@localhost2020-3-6]#tar-zcfnewtar.tgz*.cpp*.c[root@localhost2020-3-6]#lsa.cppb.cppc.cppchong.cnewtar.tgzrechong.c一键解压解包过程[root@localhost2020-3-6]#lsa.cppb.cppc.cppchong.cnewtar.tgzrechong.c[root@localhost2020-3-6]#rm*.cpp[root@localhost2020-3-6]#rm*.c[root@localhost2020-3-6]#lsnewtar.tgz[root@localhost2020-3-6]#tarzxfnewtar.tgz[root@localhost2020-3-6]#lsa.cppb.cppc.cppchong.c新的tar.tgzrechong.c.tar.gz文件打包压缩解压Unpacking?压缩后的文件分两步:1.打包tar:把需要的文件打成一个包2.Compressgzip:压缩这个包小解包解压文件分为两步:1.解压gzip-dzipname.tar.gz2.解压tarxftarname.tar文件打包ctaroptioncompressionpackagename.tarfile1file2tarcfname.tarfile1file2//打包文件1,和将文件2打成压缩包tar选项:c创建文件f指定目标为文件(不是设备)v显示过程x释放包文件t显示包文件内容[root@localhost2020-3-6]#lsa.cppb.cppc.cppchong.crechong.c[root@localhost2020-3-6]#tarcvfnewtar.tar*.cpp*.ca.cppb.cppc.cppchong.crechong。c[root@localhost2020-3-6]#lsa.cppb.cppc.cppchong.cnewtar.tarrechong.c文件压缩gzipnewtar.tar压缩.tar文件,成为.tar.gz压缩文件[root@localhost2020-3-6]#lsa.cppb.cppc.cppchong.cnewtar.tarrechong.c[root@localhost2020-3-6]#gzipnewtar.tar[root@localhost2020-3-6]#lsa.cppc.cppnewtar.tarrechong.cb.cppchong.cnewtar.tar.gz-rw-r--r--1rootroot10240Mar713:14newtar.tar-rw-r--r--1rootroot221Mar712:53解压gzipofnewtar.tar.gz文件-dzipname.tar.gz[root@localhost2020-3-6]#lsnewtar.tar.gz[root@localhost2020-3-6]#gzip-dnewtar.tar.gz[root@localhost2020-3-6]#解压lsnewtar.tar文件xtartftarname.tar//查看包文件[root@localhost2020-3-6]#tartfnewtar.tara.cppb.cppc.cppchong.crechong.ctarxftarname.tar//解包[root@localhost2020-3-6]#lsnewtar.tar[root@localhost2020-3-6]#tarxfnewtar.tar[root@localhost2020-3-6]#lsa.cppb.cppc.cppchong.cnewtar.tarrechong.ctaroptions:xreleasepackagefilef指定targeta文件(不是设备)t显示包文件内容v显示进程c创建文件