{"id":189,"date":"2020-08-18T19:23:47","date_gmt":"2020-08-18T20:23:47","guid":{"rendered":"http:\/\/www.linux-tutorial.info\/?page_id=77"},"modified":"2020-08-22T19:25:59","modified_gmt":"2020-08-22T20:25:59","slug":"this-is-the-page-title-toplevel-23","status":"publish","type":"page","link":"http:\/\/www.linux-tutorial.info\/?page_id=189","title":{"rendered":"Command Line Editing"},"content":{"rendered":"\n<title>Command Line Editing<\/title>\n<p>\nWhen I first started working in tech support, I was given a csh and once I\nfigured out all it could do, I enjoyed using it. I found the editing to be\ncumbersome from time to time, but it was better than retyping everything.\n<\/p>\n<p>\nOne of my co-workers, Kamal (of IguanaCam fame), was an avid proponent of the\nKorn Shell. Every time he wanted to show me something on my <glossary>terminal<\/glossary>,\nhe would grumble when he forgot that I wasn&#8217;t using ksh. Many times he tried to convert\nme, but learning a new <glossary>shell<\/glossary>\nwasn&#8217;t high on my list of priorities.\n<\/p>\n<p>\nI often complained to Kamal how cumbersome <command>vi<\/command> was (at least I\nthought so at the time). One day I asked him for some pointers on vi, because every time I saw him\ndo something in vi, it looked like magic. He agreed with the one condition that I at\nleast try the <command>ksh<\/command>. All he wanted to do was to show me one thing and if after\nthat I still wanted to use the <command>csh<\/command>, that was my own decision. Not that he would\nstop grumbling, just that it was my own choice.\n<\/p>\n<p>\nThe one thing that Kamal showed me convinced me of the errors of my ways. Within a week, I had requested\nthe system <glossary>administrator<\/glossary>\nto change my <glossary>login<\/glossary>\n<glossary>shell<\/glossary> to <command>ksh<\/command>.\n<\/p>\n<p>\nWhat was that one thing? Kamal showed me how to configure the <command>ksh<\/command> to edit previous commands\nusing the same syntax as the <command>vi<\/command> editor. I felt like the <command>csh<\/command> editing mechanism was like using a sledge-hammer to pound in a nail. It does what you want, but it is more work than you need.\n<\/p>\n<question id=\"\" type=\"mc\" text=\"What variable defines where your shell history is stored.\" \/>\n<question id=\"\" type=\"mc\" text=\"By default in which file is you bash history stored?\" \/>\n<p>\nMany different shells have a history mechanism. The history mechanism\nof both the <command>ksh<\/command> and <command>bash<\/command> have two major\nadvantages over that of the <command>csh<\/command>. First,\nthe information is actually saved to a file. This is either defined by the\nHISTFILE <glossary>environment<\/glossary>\n<glossary>variable<\/glossary>\n<em>before<\/em> the <glossary>shell<\/glossary>\nis invoked, or it defaults to <file>.bash_history<\/file> (for the bash) in your\n<glossary>home directory<\/glossary>. At any point\nyou can edit this file and make changes to what the <command>ksh<\/command> perceives as your\ncommand history.\n<\/p>\n<p>\nThis could be useful if you knew you\nwere going to be issuing the same commands every time you logged in and you\ndidn&#8217;t want to create aliases or functions. If you copied a saved version of this\nfile (or any other text file) and named it <file type=\"\">.sh_history<\/file>, you would immediately\nhave access to this new history. (Rewriting history? I shudder at the ramifications.)\n<\/p>\n<question id=\"53\" text=\"How to you activate command-line editing in vi-mode?\" \/>\n<concept id=\"49\" description=\"The bash shell allows you to repeat and edit previous commands.\" \/>\n<p>\nThe second advantage is the ability to edit directly any\nof the lines in your <file type=\"\">.bash_history<\/file> file from the <glossary>command line<\/glossary>.\nIf your EDITOR environment <glossary>variable<\/glossary>\nis set to <command>vi<\/command> or you use the <keyinput>set -o vi<keyinput> command, you can edit\nprevious commands using many of the standard vi editing commands.\n<\/p>\n<p>\nTo enter edit mode, press Esc. You can now scroll through the lines of your history\nfile using the <command>vi<\/command> movement keys (h-j-k-l). Once you have found the line you are\nlooking for, you can use other vi commands to delete, add, change, or whatever\nyou need. If you press &#8220;v,&#8221; you are brought into the full-screen version of vi\n(which I found out by accident). For more details, check out the vi or ksh\nman-page or the later section on vi.\n<\/p>\n<concept id=\"\" description=\"By default, the line editing commands for teh bash are similar to the emacs\neditor.\" \/>\n<p>\nNote that by default, the line editing commands are similar to the <\/command>emacs<\/command>\neditor. If vi-mode is activated, you can activate emacs-mode with <keyinput>set -o\nemacs<keyinput>&#8220;. Turning either off can be done with +o emacs or +o vi.\n<\/p>\n<p>\nIf you have been working on the command-line for a while, you may find that the command you input a while ago is no longer in the history. This happens because the shell only stores a certain number of lines (default: 500). If you really need to save more lines, you can increase it by setting the HISTSIZE variable to something like 1000.\n<\/p>\n<p>\nYou can display the entire history simply by inputting the <command name=\"bash\">history<\/command>. Also you can add a number at the end to indicate the shell should only show a specific number of line. For example <commandexample command=\"bash\">history 5<\/commandexample> would show the last 5 commands, like this:\n<\/p>\n<screenoutput>\n  745  mount\n  746  ls \/data2\n  747  \/etc\/init.d\/smb restart\n  748  fdisk -l\n  749  history 5\n<\/screenoutput>\n<p>\nNotice that the history command is also displayed. This is simply because the command is written to the history file before it is executed. This also means that mistakes (such as commands that do no exist) are also written to the history.\n<\/p>\n<p>\nOne exciting thing that <command>bash<\/command> can do is extend the <glossary>command line<\/glossary>\nediting. There are a large number of key combinations to which you can get bash to react. You say that the key combinations are &#8220;bound&#8221; to certain actions. The command you use is bind. To see what keys are\ncurrently bound, use bind -v. This is useful for finding out all the different\nediting commands to which you can bind keys.\n<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Command Line Editing When I first started working in tech support, I was given a csh and once I figured out all it could do, I enjoyed using it. I found the editing to be cumbersome from time to time, &hellip; <a href=\"http:\/\/www.linux-tutorial.info\/?page_id=189\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-189","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/pages\/189","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=189"}],"version-history":[{"count":1,"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/pages\/189\/revisions"}],"predecessor-version":[{"id":522,"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/pages\/189\/revisions\/522"}],"wp:attachment":[{"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=189"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}